471,049 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,049 software developers and data experts.

MSVC++.NET 2002--> problems with overloaded constructor reference declarations

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

Jun 11 '06 #1
2 1785
raylopez99 wrote:
I'm having problems compiling complex reference declarations in
MSVC++.NET 2002 IDE.
What is a "complex reference declaration" btw?

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;
}
You need a left brace (ie "{") immediately after "class Foo"; and a
semicolon (ie ";") at the end of the class definition.

// -------- 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...")
Add what to where? AFAICS "the following" is nothing much but an empty
block {} along with three lines of comments preceding it.

// this fails in Foo.h

// Foo(void);

// this fails in Foo.cpp

// Foo(void): refFoo1(Foo_1){}
{
}
I am guessing you are trying to do this:

class Foo
{
Foo& refFoo1;

public:
Foo(void);
Foo(Foo&);
};

Foo::Foo(void):refFoo1(Foo_1){} // line of error
Foo::Foo(Foo& Foo_1):refFoo1(Foo_1){}

And the above failed to compile because the line of error refers to a
name "Foo_1" which didn't show up in the parameter list (ie, undeclared.)

However, this doesn't look like the error message you were getting. Post
a complete code in a single source file that reproduces your problem
please. Help us help you.

////////////

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.
You just got me lost again.

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).
It is unlikely, though possible. I would put up a bet the compiler did
it right anyway. Post the *real* code from your IDE!

RL


Regards,
Ben
Jun 11 '06 #2
benben:

Shiitte! I had a long response and the server timed out, losing my
missive. So I'll make it shorter this time.

1. This benben remark is the truth, period:
Foo::Foo(void):refFoo1(Foo_1){} // line of error
Foo::Foo(Foo& Foo_1):refFoo1(Foo_1){}

And the above failed to compile because the line of error refers to a
name "Foo_1" which didn't show up in the parameter list (ie, undeclared.)
2. Complex reference declarations involving user-defined non-primitive
data types (e.g. classes) always fail to compile on my IDE due to #1
above. However, if refFoo1 and Foo_1 in the "// line of error" are
primitive data types, then the above DOES compile. Bizarre.

3. After careful consideration I've deemed that pointers are better
than references for most stuff (except that business with the Left Hand
Side assignment and the 'this' pointer that you can only do easily with
references). For one thing, you can have multiple pointers pointing to
the same thing (though also you can with references I found out,
contrary to what some books say); but more importantly you only need to
forward reference a pointer found in another class whereas with
references you have to "#include" the header file.

RL

benben wrote: raylopez99 wrote:
I'm having problems compiling complex reference declarations in
MSVC++.NET 2002 IDE.


What is a "complex reference declaration" btw?

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;
}


You need a left brace (ie "{") immediately after "class Foo"; and a
semicolon (ie ";") at the end of the class definition.

// -------- 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...")


Add what to where? AFAICS "the following" is nothing much but an empty
block {} along with three lines of comments preceding it.

// this fails in Foo.h

// Foo(void);

// this fails in Foo.cpp

// Foo(void): refFoo1(Foo_1){}
{
}


I am guessing you are trying to do this:

class Foo
{
Foo& refFoo1;

public:
Foo(void);
Foo(Foo&);
};

Foo::Foo(void):refFoo1(Foo_1){} // line of error
Foo::Foo(Foo& Foo_1):refFoo1(Foo_1){}

And the above failed to compile because the line of error refers to a
name "Foo_1" which didn't show up in the parameter list (ie, undeclared.)

However, this doesn't look like the error message you were getting. Post
a complete code in a single source file that reproduces your problem
please. Help us help you.

////////////

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.


You just got me lost again.

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).


It is unlikely, though possible. I would put up a bet the compiler did
it right anyway. Post the *real* code from your IDE!

RL


Regards,
Ben


Jun 12 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Michael Wild | last post: by
3 posts views Thread by Julius Fuchs | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.