473,749 Members | 2,432 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1958
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
1736
by: Philippe Bertrand | last post by:
Is this a bug in the C# compiler or CLR runtime? enum MyEnum { ZERO = 0, ONE = 1, TWO = 2 } class Foo { public Foo(string,object) { ... } public Foo(string,MyEnum) { ... } } Foo f = new Foo("", 0); // Uses Foo(string,MyEnum) constructor instead of Foo(string,object)
3
3318
by: mblatch | last post by:
This is probably a C#-101 type question, but I've struck out on finding an answer. I am setting up a class with a few overloaded constructors. Inside those constructors, I would like to call the "default" zero-argument constructor. The compiler gives me an error "PC_StreamBuffer is a type but is used like a variable". I've tried adding "this." in front of references to PC_StreamBuffer(), but that gives another compiler error. A...
2
3999
by: Michael Wild | last post by:
hi everybody i suppose this question has been answered a countless times, but i can't seem to find the answer... how do i call an overloaded version of a constructor from a constructor? suppose something like this: class A {
3
1591
by: Julius Fuchs | last post by:
Hi, if I create a Person, a Vehicle is created; that's fine. The problem is that if I create a Child the constuctor of Person is called before and a Vehicle is created then a Bike is created and the variable is overwritten. How can I prevent this? class Person {
0
944
by: afterwave | last post by:
I have written a component called PlotPanel which inherits from System.Windows.Forms.Panel. When the control is used in the design window the default constructor "this.plotPanelReal = new PlotPanel();" is generated in "Windows Form Designer generated code". I want to use an overloaded constructor instead: "this.plotPanelReal = new SEA.PlotPanel(nbXMin, nbXMax, nbYMin, nbYMax);" (nbXXXX is number boxes which inherits from TextBox). If I just...
8
1956
by: shanakard | last post by:
I m a beginner programmer and new to this site so i m not quite sure whether this is the correct place to ask this question. I saw the 2 articles published here about this question but can't seem to understand it. i have a class like this withe some overloaded constructors class Abcd{ public: //sorry for forgetting this earlier Abcd();
5
1675
by: __PPS__ | last post by:
Hello everybody, I'm not sure but I thought that sometime ago I had class with two overloaded methods: void func(const char*); and template<int N>func(const char (&arr)); so, that whenever I call func("some text") then size of the text will be known at compile time instead of doing strlen() I'm sure that long time ago I got it working somehow, but now when I needed and implemented a similar solution I see that it doesn't work.
1
3013
by: mersinli | last post by:
Hi, I have got seven error and I dont find why compiler find error.. //commission class definition #ifndef COMMISSION_H #define COMMISSION_H #include <string> using std::string;
1
1302
Natasha26
by: Natasha26 | last post by:
I came across some strange declarations and am trying to get the hang of it. Any comments on how to read and use such decl. will be most welcomed. 1) Not sure if i understand this one. It could be that function f can be used to increment the address of a pointer. So to use f(...) give it an argument of type: int* void f(int*& i) { i++; } 2) I got this one from pg35 of "C++ in a nutshell," i've put arrows to indicate where i got...
0
8833
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9389
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8257
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6801
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6079
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4709
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4881
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3320
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2218
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.