R
raylopez99
I'm having problems compiling complex reference declarations in
MSVC++.NET 2002 IDE.
Here is an example:
// --Foo.h--
#include "Bar.h"
class Bar; //forward decl. to a class Bar in another file, not used
here
class Foo
Foo(Foo& Foo1);
// Foo (void); // comment out since will fail to compile, see
below
~Foo(void);
private:
Foo &refFoo1;
}
// -------- Foo.cpp --------
#include "foo.h"
Foo::Foo(Foo &Foo_1):refFoo1(Foo_1)
// Foo(void): refFoo1(Foo_1){} // comment out as fails to compile
{
}
////////////////////////
Now the above compiles, BUT, if I try overloading the normal
constructor along the lines of adding, in both Foo.h and Foo. cpp the
following, you get the infamous compiler errors C2758 and C2530 ('You
must initialize a reference when it is declared, unless...")
// this fails in Foo.h
// Foo(void);
// this fails in Foo.cpp
// Foo(void): refFoo1(Foo_1){}
{
}
////////////
Any ideas as to why? BTW, if I try referencing a primitive data type,
like an int, "int &intREF", the above 'overloaded' normal constructor
_DOES_ compile (!). But complex assignments fail.
I am curious but for my purposes even having a single constructor is
OK, but just curious as to why this fails. I've read that references
were in a state of flux as late as 1999, so perhaps this is a
bug/feature of the MSVC++ IDE (2002).
RL
MSVC++.NET 2002 IDE.
Here is an example:
// --Foo.h--
#include "Bar.h"
class Bar; //forward decl. to a class Bar in another file, not used
here
class Foo
Foo(Foo& Foo1);
// Foo (void); // comment out since will fail to compile, see
below
~Foo(void);
private:
Foo &refFoo1;
}
// -------- Foo.cpp --------
#include "foo.h"
Foo::Foo(Foo &Foo_1):refFoo1(Foo_1)
// Foo(void): refFoo1(Foo_1){} // comment out as fails to compile
{
}
////////////////////////
Now the above compiles, BUT, if I try overloading the normal
constructor along the lines of adding, in both Foo.h and Foo. cpp the
following, you get the infamous compiler errors C2758 and C2530 ('You
must initialize a reference when it is declared, unless...")
// this fails in Foo.h
// Foo(void);
// this fails in Foo.cpp
// Foo(void): refFoo1(Foo_1){}
{
}
////////////
Any ideas as to why? BTW, if I try referencing a primitive data type,
like an int, "int &intREF", the above 'overloaded' normal constructor
_DOES_ compile (!). But complex assignments fail.
I am curious but for my purposes even having a single constructor is
OK, but just curious as to why this fails. I've read that references
were in a state of flux as late as 1999, so perhaps this is a
bug/feature of the MSVC++ IDE (2002).
RL