Connecting Tech Pros Worldwide Forums | Help | Site Map

error: uninitialized reference member

supachamp@gmx.net
Guest
 
Posts: n/a
#1: Nov 22 '05
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.


Neelesh Bodas
Guest
 
Posts: n/a
#2: Nov 22 '05

re: error: uninitialized reference member



supachamp@gmx.net wrote:
[color=blue]
> Edge::Edge(Vertex& _v0, Vertex& _v1) {
> v0 = _v0;
> v1 = _v1;
> }
>[/color]

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

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

peter koch
Guest
 
Posts: n/a
#3: Nov 22 '05

re: error: uninitialized reference member



supachamp@gmx.net skrev:
[color=blue]
> 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
> {[/color]
[snip][color=blue]
> };
>
> class Edge {
> protected:
> Vertex& v0;
> Vertex& v1;
> public:
> Edge(Vertex&, Vertex&);[/color]
[snip][color=blue]
> };
>[/color]
[snip][color=blue]
> Edge::Edge(Vertex& _v0, Vertex& _v1) {
> v0 = _v0;
> v1 = _v1;
> }[/color]

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][color=blue]
> CQ.[/color]

/Peter

Victor Bazarov
Guest
 
Posts: n/a
#4: Nov 22 '05

re: error: uninitialized reference member


supachamp@gmx.net wrote:[color=blue]
> [..]
> Edge::Edge(Vertex& _v0, Vertex& _v1) {
> v0 = _v0;
> v1 = _v1;
> }[/color]


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
supachamp@gmx.net
Guest
 
Posts: n/a
#5: Nov 22 '05

re: error: uninitialized reference member


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.

Neelesh Bodas
Guest
 
Posts: n/a
#6: Nov 22 '05

re: error: uninitialized reference member


supach...@gmx.net wrote:[color=blue]
> 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?
>[/color]

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.

[color=blue]
> CQ.[/color]

supachamp@gmx.net
Guest
 
Posts: n/a
#7: Nov 22 '05

re: error: uninitialized reference member


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

CQ.

Old Wolf
Guest
 
Posts: n/a
#8: Nov 22 '05

re: error: uninitialized reference member


Neelesh Bodas wrote:
[color=blue]
> supach...@gmx.net wrote:[color=green]
>> I get the following error messages:
>> undefined reference to `Edge::V0() const'[/color]
>
> 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.
>[/color]

Note that in the OP code:
[color=blue][color=green]
>> class IGEdge : public Edge {
>> Vertex& V0 () {return v0;};
>> Vertex& V1 () {return v1;};
>> }[/color][/color]

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

Closed Thread