error: uninitialized reference member

S

supachamp

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

Neelesh Bodas

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) { }
 
V

Victor Bazarov

[..]
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
 
P

peter koch

(e-mail address removed) 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]

/Peter
 
S

supachamp

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

Neelesh Bodas

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

supachamp

Thanks a lot! I have made those functions purely virtual and that was
it! All errors are gone!

CQ.
 
O

Old Wolf

Neelesh said:
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:

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,169
Latest member
ArturoOlne
Top