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_gravi ty() const = 0;
//...
};
// concrete geometric object class Circle
// - derived from GeoObj
class Circle : public GeoObj {
public:
virtual void draw() const;
virtual Coord center_of_gravi ty() const;
//...
};
// concrete geometric object class Line
// - derived from GeoObj
class Line : public GeoObj {
public:
virtual void draw() const;
virtual Coord center_of_gravi ty() const;
//...
};
//...
--------------------------------------
//dynapoly.cpp
#include "dynahier.h pp"
#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_gr avity() - x2.center_of_gr avity();
return c.abs(); // return coordinates as absolute values
}
// draw heterogeneous collection of GeoObjs
void drawElems (std::vector<Ge oObj*> 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<Geo Obj*> 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.ex e
dynapoly.obj
dynapoly.obj : error LNK2001: unresolved external symbol "public: virtual
class Coord __thiscall Line::center_of _gravity(void)c onst "
(?center_of_gra vity@Line@@UBE? AVCoord@@XZ)
dynapoly.obj : error LNK2001: unresolved external symbol "public: virtual
void __thiscall Line::draw(void )const " (?draw@Line@@UB EXXZ)
dynapoly.obj : error LNK2001: unresolved external symbol "public: virtual
class Coord __thiscall Circle::center_ of_gravity(void )const "
(?center_of_gra vity@Circle@@UB E?AVCoord@@XZ)
dynapoly.obj : error LNK2001: unresolved external symbol "public: virtual
void __thiscall Circle::draw(vo id)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
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_gravi ty() const = 0;
//...
};
// concrete geometric object class Circle
// - derived from GeoObj
class Circle : public GeoObj {
public:
virtual void draw() const;
virtual Coord center_of_gravi ty() const;
//...
};
// concrete geometric object class Line
// - derived from GeoObj
class Line : public GeoObj {
public:
virtual void draw() const;
virtual Coord center_of_gravi ty() const;
//...
};
//...
--------------------------------------
//dynapoly.cpp
#include "dynahier.h pp"
#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_gr avity() - x2.center_of_gr avity();
return c.abs(); // return coordinates as absolute values
}
// draw heterogeneous collection of GeoObjs
void drawElems (std::vector<Ge oObj*> 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<Geo Obj*> 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.ex e
dynapoly.obj
dynapoly.obj : error LNK2001: unresolved external symbol "public: virtual
class Coord __thiscall Line::center_of _gravity(void)c onst "
(?center_of_gra vity@Line@@UBE? AVCoord@@XZ)
dynapoly.obj : error LNK2001: unresolved external symbol "public: virtual
void __thiscall Line::draw(void )const " (?draw@Line@@UB EXXZ)
dynapoly.obj : error LNK2001: unresolved external symbol "public: virtual
class Coord __thiscall Circle::center_ of_gravity(void )const "
(?center_of_gra vity@Circle@@UB E?AVCoord@@XZ)
dynapoly.obj : error LNK2001: unresolved external symbol "public: virtual
void __thiscall Circle::draw(vo id)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
Comment