C++ Templates Book, Link Problems

Discussion in 'C++' started by Don Kim, Jan 18, 2005.

  1. Don Kim

    Don Kim Guest

    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
    Don Kim, Jan 18, 2005
    #1
    1. Advertising

  2. 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.
    Gianni Mariani, Jan 18, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Spencer

    Re: Link Link Link DANGER WILL ROBINSON!!!

    Kevin Spencer, May 17, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    815
    Kevin Spencer
    May 17, 2005
  2. Fred
    Replies:
    1
    Views:
    595
    Neredbojias
    Sep 26, 2005
  3. John Harrison

    using templates in templates

    John Harrison, Jul 31, 2003, in forum: C++
    Replies:
    8
    Views:
    380
    Torsten Curdt
    Jul 31, 2003
  4. JKop
    Replies:
    3
    Views:
    467
  5. recover
    Replies:
    2
    Views:
    799
    recover
    Jul 25, 2006
Loading...

Share This Page