Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > C++ Templates Book, Link Problems

Reply
Thread Tools

C++ Templates Book, Link Problems

 
 
Don Kim
Guest
Posts: n/a
 
      01-18-2005
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[i]->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


 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      01-18-2005
Don Kim wrote:
> 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

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to Specializations of function Templates or Overloading Function templates with Templates ? recover C++ 2 07-25-2006 02:55 AM
Monster Templates - Question about Submitting Templates Fred HTML 1 09-26-2005 01:09 AM
RE: Link Link Link =?Utf-8?B?REw=?= Windows 64bit 0 05-17-2005 12:15 PM
Re: Link Link Link DANGER WILL ROBINSON!!! Kevin Spencer ASP .Net 0 05-17-2005 10:41 AM
Templates templates templates JKop C++ 3 07-21-2004 11:44 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57