By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,503 Members | 1,608 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,503 IT Pros & Developers. It's quick & easy.

error: uninitialized reference member

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a

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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
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

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

CQ.

Nov 22 '05 #7

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.