Re: error: value-initialization of reference

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



What do you expect/want the copy constructor of Class_With_Ref to do?

I'm not quite sure of the exact C++ standard meaning of

Wrapper t = Wrapper();

(what constructors and/or operator=() does it, call under what conditions.)
so this issue may not be involved. But in any case it seems likely that some point in your code is invoking a copy constructor on Class_With_Ref and if you don't define that copy constructor, the compiler will and the one the compiler will define will be wrong.

The source of the copy constructor would be a const Class_With_Ref& so I think there is a problem initializing the int&ref in the object being constructed from the int&ref in the const source.

David Hammarwall wrote:
Hi,

I've got a problem using a library where I get the compiler error
"value-initialization of reference" (using gcc4.3.2). I've isolated
the problem to the following dummy example:

int DEFAULT_OBJ = 0;

class Class_With_Ref
{
public:
  Class_With_Ref() : ref(DEFAULT_OBJ) {}
  Class_With_Ref& operator=(const Class_With_Ref &v) {return *this;}
private:
  int &ref;
};

struct Wrapper
{
  // Wrapper(){}  //Everything will work if this line is un-commented
  Class_With_Ref test;
};

int main(int argc, char **argv) {
  Wrapper t = Wrapper();
  return 1;
}

Everything will work if I explicitly declare (the empty) default
constructor in the Wrapper struct, but this is a pain considering that
there are a plethora of structs that contains a Class_With_Ref
objects. Moreover, I did not have this issue with gcc 3. Also, the
assignment, Wrapper t = Wrapper(), is implemented in the library so I
cannot change it to e.g.,

Wrapper t,t2;
t=t2;

which works perfectly in the above example.

Does anybody have any insight here?

BR,
David



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux