<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Bits in einem Array als L-Value behandeln]]></title><description><![CDATA[<p>Hi Leute.<br />
Ich habe mir eine Klasse geschrieben, die ähnlich wie std::bitset eine Sammlung von Bits darstellt. Dazu habe ich auch den Index-Operator überladen. Allerdings kann ich den Rückgabewert des Indexoperators nicht als L-Value benutzen, da C++ ja minimal bytes adressieren kann. Also habe ich versucht, einen Wrapper dafür zu schreiben:</p>
<pre><code class="language-cpp">class ref;

class bitarray
{
    unsigned int bits[5]; //hält 160 bits

   public:

    bool operator [] (int pos) const
    {
        return check_bit(pos);
    }

    ref operator [] (int pos) //Index Operator als L-Value
    {
        return ref(*this, pos);
    }

    bool check_bit(int pos) const
    {
        //...
    }

    void set_bit(int pos)
    {
        //...
    }
};

class ref
{
    bitarray* obj;
    int pos;

   public:

    bool operator = (bool b)
    {
        obj-&gt;set_bit(b);
        return b;
    }

    ref(bitarray&amp; a, int b) : obj(&amp;a), pos(b) {}

    operator bool() const
    {
        return obj-&gt;check_bit(pos);
    }
};

int main()
{
    bitarray a;
    a[4]=true;
}
</code></pre>
<p>Allerdings erhalte ich drei Fehlermeldungen, wenn ich versuche, das zu kompilieren:</p>
<pre><code>15: error: return type 'struct ref' is incomplete
16: error: invalid use of undefined type 'struct ref'
1 : error: forward declaration of 'struct ref'
</code></pre>
<p>Was mache ich falsch? Und vor Allem: Wie mache ich es richtig?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/263865/bits-in-einem-array-als-l-value-behandeln</link><generator>RSS for Node</generator><lastBuildDate>Sat, 05 Sep 2026 13:45:08 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/263865.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 29 Mar 2010 00:45:41 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Bits in einem Array als L-Value behandeln on Mon, 29 Mar 2010 00:45:41 GMT]]></title><description><![CDATA[<p>Hi Leute.<br />
Ich habe mir eine Klasse geschrieben, die ähnlich wie std::bitset eine Sammlung von Bits darstellt. Dazu habe ich auch den Index-Operator überladen. Allerdings kann ich den Rückgabewert des Indexoperators nicht als L-Value benutzen, da C++ ja minimal bytes adressieren kann. Also habe ich versucht, einen Wrapper dafür zu schreiben:</p>
<pre><code class="language-cpp">class ref;

class bitarray
{
    unsigned int bits[5]; //hält 160 bits

   public:

    bool operator [] (int pos) const
    {
        return check_bit(pos);
    }

    ref operator [] (int pos) //Index Operator als L-Value
    {
        return ref(*this, pos);
    }

    bool check_bit(int pos) const
    {
        //...
    }

    void set_bit(int pos)
    {
        //...
    }
};

class ref
{
    bitarray* obj;
    int pos;

   public:

    bool operator = (bool b)
    {
        obj-&gt;set_bit(b);
        return b;
    }

    ref(bitarray&amp; a, int b) : obj(&amp;a), pos(b) {}

    operator bool() const
    {
        return obj-&gt;check_bit(pos);
    }
};

int main()
{
    bitarray a;
    a[4]=true;
}
</code></pre>
<p>Allerdings erhalte ich drei Fehlermeldungen, wenn ich versuche, das zu kompilieren:</p>
<pre><code>15: error: return type 'struct ref' is incomplete
16: error: invalid use of undefined type 'struct ref'
1 : error: forward declaration of 'struct ref'
</code></pre>
<p>Was mache ich falsch? Und vor Allem: Wie mache ich es richtig?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1874864</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1874864</guid><dc:creator><![CDATA[Bitter]]></dc:creator><pubDate>Mon, 29 Mar 2010 00:45:41 GMT</pubDate></item><item><title><![CDATA[Reply to Bits in einem Array als L-Value behandeln on Mon, 29 Mar 2010 01:53:46 GMT]]></title><description><![CDATA[<p>Nach einigem Rumprobieren habe ich das Programm zum Laufen gebracht. Wenn ich aber versuche, das Problem auf mein &quot;richtiges&quot; Projekt zu übertragen, bekomme ich einen Fehler:</p>
<pre><code class="language-cpp">template&lt;unsigned int N&gt;
class bitarray; //forward declaration

template&lt;unsigned int N&gt;
class ref_bitarray
{
	bitarray&lt;N&gt;* const obj;

	unsigned int bit;

	ref_bitarray();

   public:
	~ref_bitarray();

	bool operator=(bool);

	ref_bitarray(bitarray&lt;N&gt;&amp;, unsigned int);

	operator bool() const;
};

template&lt;unsigned int N&gt;
class bitarray
{
    unsigned int bits [ N/32 ];

   public:
    bool operator[](unsigned int pos) const
    {
        return check_bit(pos);
    }

    template&lt;unsigned int T&gt;
    ref_bitarray&lt;N&gt; operator[](unsigned int pos) //Index Operator als L-Value
    {
        return ref_bitarray&lt;N&gt;(*this, pos);
    }

    bool check_bit(unsigned int pos) const
    {
        //...
    }

    void set_bit (unsigned int pos, bool value)
    {
        //...
    }
};

template&lt;unsigned int N&gt;
ref_bitarray&lt;N&gt;::~ref_bitarray(){}

template&lt;unsigned int N&gt;
bool ref_bitarray&lt;N&gt;::operator=(bool b)
{
	obj-&gt;set_bit(bit, b);
	return b;
}

template&lt;unsigned int N&gt;
ref_bitarray&lt;N&gt;::ref_bitarray(bitarray&lt;N&gt;&amp; a, unsigned int i)
	: obj(&amp;a), bit(i) {}

template&lt;unsigned int N&gt;
ref_bitarray&lt;N&gt;::operator bool() const
{
	return obj-&gt;check_bit(bit);
}

int main()
{
    bitarray&lt;64&gt; a;
    a[4]=true;
}
</code></pre>
<pre><code>78: error: non l-value in assignment
</code></pre>
<p>Aber sollte das so nicht funktionieren? Wenn ich das ganze komplett ohne templates schreibe, kompiliert es ohne Fehlermeldungen...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1874868</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1874868</guid><dc:creator><![CDATA[Bitter]]></dc:creator><pubDate>Mon, 29 Mar 2010 01:53:46 GMT</pubDate></item><item><title><![CDATA[Reply to Bits in einem Array als L-Value behandeln on Mon, 29 Mar 2010 07:10:08 GMT]]></title><description><![CDATA[<p>Meinst Du nicht (N+31)/32 statt N/32 ?<br />
Was soll denn das template&lt;unsigned int T&gt; beim operator[] ?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1874888</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1874888</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Mon, 29 Mar 2010 07:10:08 GMT</pubDate></item></channel></rss>