473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

error: uninitialized reference member

Hi everybody,

I am having great problems with the following classes. I just can't
figure out why I keep getting the error message:

Building file: ../edge.cc
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -oedge.o ../edge.cc
.../edge.cc: In constructor `Edge::Edge(Vertex&, Vertex&)':
.../edge.cc:4: error: uninitialized reference member `Edge::v0'
.../edge.cc:4: error: uninitialized reference member `Edge::v1'
Here are the classes: (header file)

class Vertex : public Object
{
public:
typedef unsigned int Number;
protected:
Number number;
public:
Vertex() {};
Vertex (Number _number) : number(_number) {};
Number getNumber() {return number;};
};

class Edge {
protected:
Vertex& v0;
Vertex& v1;
public:
Edge(Vertex&, Vertex&);
virtual Vertex& V0 () const;
virtual Vertex& V1 () const;
virtual Vertex& Mate (Vertex const&) const;
};

/* possible types of a transition/ arc */
enum transitionType {sending, receiving};

class IGEdge : public Edge {
protected:
char * label; /* label of the arc (usually the name of the event)
*/
transitionType type; /* type of the arc (sending, receiving) */

public:

IGEdge (Vertex&, Vertex&, char *, transitionType);
virtual ~IGEdge () {delete label;};
char * getLabel() {return label;};
transitionType getType() {return type;};
Vertex& V0 () {return v0;};
Vertex& V1 () {return v1;};
Vertex& Mate (Vertex const&) {return v0;}; // TODO!
};
implementation:

#include "edge.h"
#include "vertex.h"

Edge::Edge(Vertex& _v0, Vertex& _v1) {
v0 = _v0;
v1 = _v1;
}
IGEdge::IGEdge(Vertex& _v0, Vertex& _v1, char * _label, transitionType
_type) : Edge(_v0, _v1) {
type = _type;
label = _label;
}

Is there anyone out there who can help me fix this problem?

Thanks a lot in advance!

CQ.

Nov 22 '05 #1
7 29389

su*******@gmx.net wrote:
Edge::Edge(Vertex& _v0, Vertex& _v1) {
v0 = _v0;
v1 = _v1;
}


References cannot be assigned. They must be _initialized_. Use
constructor initialization list instead -

Edge::Edge(Vertex& _v0, Vertex& _v1) : v0( _v0 ) , v1( _v1) { }

Nov 22 '05 #2

su*******@gmx.net skrev:
Hi everybody,

I am having great problems with the following classes. I just can't
figure out why I keep getting the error message:

Building file: ../edge.cc
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -oedge.o ../edge.cc
../edge.cc: In constructor `Edge::Edge(Vertex&, Vertex&)':
../edge.cc:4: error: uninitialized reference member `Edge::v0'
../edge.cc:4: error: uninitialized reference member `Edge::v1'
Here are the classes: (header file)

class Vertex : public Object
{ [snip] };

class Edge {
protected:
Vertex& v0;
Vertex& v1;
public:
Edge(Vertex&, Vertex&); [snip] };
[snip] Edge::Edge(Vertex& _v0, Vertex& _v1) {
v0 = _v0;
v1 = _v1;
}
Edge::Edge(Vertex& _v0, Vertex& _v1):v0(_v0),v1(_v1) {}

You should strive to always construct members and base-classes directly
as the method you're using causes the default construction of these
elements. This is inefficient and - for references - downright illegal
(references must always be explicitly constructed).

[snip] CQ.


/Peter

Nov 22 '05 #3
su*******@gmx.net wrote:
[..]
Edge::Edge(Vertex& _v0, Vertex& _v1) {
v0 = _v0;
v1 = _v1;
}

Since 'v0' and 'v1' are _references_, you can't let them go uninitialised.
You _must_ put them in the initialiser list:

Edge::Edge(Vertex& _v0, Vertex& _v1) : v0(_v0), v1(_v1) {}

V
Nov 22 '05 #4
Thanks for your answer. I have tried this and I get the following error
messages:

../edge.o: In function `_ZN4EdgeC2ER6VertexS1_':
/cygdrive/d/Daten/projekte/eclipse/workspace/graphs/Debug/../edge.cc:4:
undefined reference to `vtable for Edge'
../edge.o: In function `_ZN4EdgeC1ER6VertexS1_':
/cygdrive/d/Daten/projekte/eclipse/workspace/graphs/Debug/../edge.cc:4:
undefined reference to `vtable for Edge'
../edge.o: In function `_ZN6IGEdgeD1Ev':
/cygdrive/d/Daten/projekte/eclipse/workspace/graphs/Debug/../edge.h:(.rdata$_ZTV6IGEdge[vtable
for IGEdge]+0x8): undefined reference to `Edge::V0() const'
/cygdrive/d/Daten/projekte/eclipse/workspace/graphs/Debug/../edge.h:(.rdata$_ZTV6IGEdge[vtable
for IGEdge]+0xc): undefined reference to `Edge::V1() const'

What is wrong with my code?

CQ.

Nov 22 '05 #5
supach...@gmx.net wrote:
Thanks for your answer. I have tried this and I get the following error
messages:

./edge.o: In function `_ZN4EdgeC2ER6VertexS1_':
/cygdrive/d/Daten/projekte/eclipse/workspace/graphs/Debug/../edge.cc:4:
undefined reference to `vtable for Edge'
./edge.o: In function `_ZN4EdgeC1ER6VertexS1_':
/cygdrive/d/Daten/projekte/eclipse/workspace/graphs/Debug/../edge.cc:4:
undefined reference to `vtable for Edge'
./edge.o: In function `_ZN6IGEdgeD1Ev':
/cygdrive/d/Daten/projekte/eclipse/workspace/graphs/Debug/../edge.h:(.rdata$_ZTV6IGEdge[vtable
for IGEdge]+0x8): undefined reference to `Edge::V0() const'
/cygdrive/d/Daten/projekte/eclipse/workspace/graphs/Debug/../edge.h:(.rdata$_ZTV6IGEdge[vtable
for IGEdge]+0xc): undefined reference to `Edge::V1() const'

What is wrong with my code?

You either need to define virtual function even in base class, or
declare them pure virtual by saying

virtual Vertex& V0() const = 0;

Any virtual function which is not pure needs an implementation in the
base class.

As an aside, since g++ also puts the vtable in that object file in
which first non-inline virtual function is defined, and since you have
not defined your first non inline virtual function (V0), hence compiler
complains about "undefined reference to vtable for Edge". Once you
define V0 and V1 in Edge (or make them pure virtual) the error will go
away.

HTH.

CQ.


Nov 22 '05 #6
Thanks a lot! I have made those functions purely virtual and that was
it! All errors are gone!

CQ.

Nov 22 '05 #7
Neelesh Bodas wrote:
supach...@gmx.net wrote:
I get the following error messages:
undefined reference to `Edge::V0() const'


You either need to define virtual function even in base class, or
declare them pure virtual by saying

virtual Vertex& V0() const = 0;

Any virtual function which is not pure needs an implementation in
the base class.


Note that in the OP code:
class IGEdge : public Edge {
Vertex& V0 () {return v0;};
Vertex& V1 () {return v1;};
}


these V0 and V1 functions are NOT overrides of the V0 and V1 in
the base class, because the base class ones are const and these
ones aren't. They are just normal overloads. If you call V0
through a base class pointer, you will get the base class
function (which will cause undefined behaviour if it is pure
virtual).

Nov 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Wayno | last post by:
My php logs are coming up empty. I have done all I can think of, and all that made sense to me. Can someone take a look at my php.ini please and tell me what you think may be the problem. I...
4
by: Peter Olcott | last post by:
Yesterday I found that an invocation of a member function that lacked the required "()" pararenthesis did not generate an error message. This was an invocation of a member function that is part of...
3
by: julien | last post by:
Hello, Is it possible if a boolean was initialized or not? For other types of variable, I usually check if it is null. But this not possible for a boolean. Thank you Julien
69
by: fieldfallow | last post by:
Hello all, Before stating my question, I should mention that I'm fairly new to C. Now, I attempted a small demo that prints out the values of C's numeric types, both uninitialised and after...
7
by: The|Godfather | last post by:
Hi everybody, I read Scotte Meyer's "Effective C++" book twice and I know that he mentioned something specific about constructors and destructors that was related to the following...
6
by: Kinbote | last post by:
Hi, I'm trying to make a function that opens a file, reads it in line by line, puts each line into an malloc'd array, and returns the array. I suspect I'm going about it in an atypical fashion, as...
2
by: pnsreee | last post by:
Hi All, I have the following code. The array @sub_data will contain integers or a string"NO". I have to validate if the array contain integer. If it contain "NO" then no need to validate. ...
4
by: Andre | last post by:
Hi all, Can someone point out what is wrong with this tiny piece of code? I'm compiling it with "g++ ./Main.cpp", and get the error message "error: request for member 'konnect' in 'theSocket',...
4
by: Montezuma's Daughter | last post by:
Hi all it is my first application. I wrote a class libraray with functions and a simple class with form, textbox, buttons and a label. I added reference to the second class so I would read the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...

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.