C++ Templates Book, Link Problems

D

Don Kim

I'm trying to compile the following code from "C++ Templates" Book by
Josuttis and Vandervoorde, and I keep getting link errors:

//coord.hpp
#include <cstdlib>
class Coord {
private:
int x, y;
public:
Coord (int i1, int i2) : x(i1), y(i2) {
}
friend Coord operator - (Coord const& c1, Coord const& c2) {
return Coord(c1.x-c2.x, c1.y-c2.y);
}
Coord abs() {
return Coord(std::abs(x),std::abs(y));
}
};
--------------------------------------
//dynahier.hpp
#include "coord.hpp"

// common abstract base class GeoObj for geometric objects
class GeoObj {
public:
// draw geometric object:
virtual void draw() const = 0;
// return center of gravity of geometric object:
virtual Coord center_of_gravity() const = 0;
//...
};

// concrete geometric object class Circle
// - derived from GeoObj
class Circle : public GeoObj {
public:
virtual void draw() const;
virtual Coord center_of_gravity() const;
//...
};

// concrete geometric object class Line
// - derived from GeoObj
class Line : public GeoObj {
public:
virtual void draw() const;
virtual Coord center_of_gravity() const;
//...
};
//...
--------------------------------------
//dynapoly.cpp
#include "dynahier.hpp"
#include <vector>

// draw any GeoObj
void myDraw (GeoObj const& obj)
{
obj.draw(); // call draw() according to type of object
}

// process distance of center of gravity between two GeoObjs
Coord distance (GeoObj const& x1, GeoObj const& x2)
{
Coord c = x1.center_of_gravity() - x2.center_of_gravity();
return c.abs(); // return coordinates as absolute values
}

// draw heterogeneous collection of GeoObjs
void drawElems (std::vector<GeoObj*> const& elems)
{
for (unsigned i=0; i<elems.size(); ++i) {
elems->draw(); // call draw() according to type of element
}
}

int main()
{
Line l;
Circle c, c1, c2;

myDraw(l); // myDraw(GeoObj&) => Line::draw()
myDraw(c); // myDraw(GeoObj&) => Circle::draw()

distance(c1,c2); // distance(GeoObj&,GeoObj&)
distance(l,c); // distance(GeoObj&,GeoObj&)

std::vector<GeoObj*> coll; // heterogeneous collection
coll.push_back(&l); // insert line
coll.push_back(&c); // insert circle
drawElems(coll); // draw different kinds of GeoObjs
}

When I run it through the compiler, VC7.1, I get the following link errors:

/out:dynapoly.exe
dynapoly.obj
dynapoly.obj : error LNK2001: unresolved external symbol "public: virtual
class Coord __thiscall Line::center_of_gravity(void)const "
(?center_of_gravity@Line@@UBE?AVCoord@@XZ)
dynapoly.obj : error LNK2001: unresolved external symbol "public: virtual
void __thiscall Line::draw(void)const " (?draw@Line@@UBEXXZ)
dynapoly.obj : error LNK2001: unresolved external symbol "public: virtual
class Coord __thiscall Circle::center_of_gravity(void)const "
(?center_of_gravity@Circle@@UBE?AVCoord@@XZ)
dynapoly.obj : error LNK2001: unresolved external symbol "public: virtual
void __thiscall Circle::draw(void)const " (?draw@Circle@@UBEXXZ)
dynapoly.exe : fatal error LNK1120: 4 unresolved externals

Anyone have any ideas? The code looks right and is right out of the book.
The templated example has the same errors.

-Don Kim
 
G

Gianni Mariani

Don said:
I'm trying to compile the following code from "C++ Templates" Book by
Josuttis and Vandervoorde, and I keep getting link errors:

....

// - derived from GeoObj
class Circle : public GeoObj {
public:
virtual void draw() const;
virtual Coord center_of_gravity() const;

Circle::draw() and Circle::center_of_gravity() are missing definitions
//...
};

// concrete geometric object class Line
// - derived from GeoObj
class Line : public GeoObj {
public:
virtual void draw() const;
virtual Coord center_of_gravity() const;

Line::draw() and Line::center_of_gravity() are missing definitions
 

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

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top