| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 567 人关注过本帖
标题:C++ template这本书的例子怎么总编译通不过?
取消只看楼主 加入收藏
lsrwan
Rank: 2
等 级:论坛游民
帖 子:31
专家分:17
注 册:2009-10-11
结帖率:90%
收藏
已结贴  问题点数:20 回复次数:1 
C++ template这本书的例子怎么总编译通不过?
在看这本书,也下载了源代码,发现编译经常通不过,不知道什么原因,这个是讲虚函数的,不知道原因,请各位前辈指正.
程序代码:
---------------------------
//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
}

搜索更多相关主题的帖子: template 例子 编译 
2010-06-03 22:21
lsrwan
Rank: 2
等 级:论坛游民
帖 子:31
专家分:17
注 册:2009-10-11
收藏
得分:0 
回复 3楼 迷失的木桶
确实是派生类没有实例化,不知道这套源代码是什么意思,难道是仅仅讲派生,然后省略的实例化的代码,这些代码看起来很很郁闷,还要自己修改.
2010-06-06 21:35
快速回复:C++ template这本书的例子怎么总编译通不过?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.024226 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved