473,383 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

C++ Templates Book, Link Problems

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
Jul 22 '05 #1
1 1447
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.

Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: SK | last post by:
Could anyone provide me a link to a really useful online link for understanding C++ templates.I know the book by Josuttus is the bible but is currently unavailable to me. Thanks in advance. ...
1
by: SK | last post by:
Hello group, I happened to check the review of C++ Templates & Tools (By Scott Robert Ladd) at http://accu.org. The book is in the "Not recommended" category. I will like to hear if you anyone...
1
by: SK | last post by:
Hi all, I have a doubt in C++ Templates by Nicolai M. Josuttis. On Page 17 there is a line "In general, it is a good idea not to change more than necessary when overloading function templates....
5
by: Christian Christmann | last post by:
Hi, I want to implement an graph using templates. In my header file I define the templates node and edge: template <class NODE> class GNode { NODE *info; public: GraphNode();
6
by: Greg Warren | last post by:
I am writing a container class so that I can reuse my double ended list code. I have a base class CListBase that handles inserts and deletes with a member function addItem, defined as:...
0
by: Audrey Pratt | last post by:
Happy Holidays to you and allow us to play Santa this year with these awsome deals that in anyway you can refuse: 2000 Web Templates for only $18.00 (Savings Over $1,000.00) ...
5
by: ryanoasis | last post by:
Working on a C++ assignment and I cant figure out the problems I am having w/ Templates and Subclasses. I know there are issues with templates and certain compilers so I am not sure what the...
1
by: Rishi Boparai | last post by:
I'm using Dreamweaver to create the layout and manage my ASP.net 2 site. However, all ""code-behind"" is done in Visual Studio 2005 On my ASP.net pages I need to have a different declaration at...
26
by: puzzlecracker | last post by:
Team, C++ has been around since 1986, why templates are still regarded is a new feature by most compiler vendors and not fully supported (for example export feature). Look at other popular...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.