首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

编译没有关问题,链接异常如何解?

2013-11-11 
编译没问题,链接错误怎么解??#includeiostream#includestringusing namespace stdvoid error(const s

编译没问题,链接错误怎么解??
#include<iostream>
#include<string>
using namespace std;
void error(const string& msg);
class bird
{
public:
bird():_type(0)
{};
virtual void fly();
private:
int _type;
};

class penguin:public bird
{
public:
penguin():bird(),_type_(1)
{};
virtual void fly()
{
error("program is wrong!");
}
private:
int _type_;
};

void error(const string& msg)
{
cout<<msg<<endl;
}

int main()
{
penguin qq;
qq.fly();
return 0;
}
item32.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall bird::fly(void)" (?fly@bird@@UAEXXZ)
Debug/item32.exe : fatal error LNK1120: 1 unresolved externals
执行 link.exe 时出错.

求达人指点??我的目的是想证明类中的成员函数可以调用非成员函数,就写了这个代码,网上找了下,不知道错误怎么解决。
[解决办法]

#include <iostream>
#include <string>

using namespace std;

void error(const string& msg);

class bird {
 public:
  bird():_type(0) {}; 

  virtual void fly() = 0;

  virtual ~bird() {}

 private:
  int _type;
};

class penguin : public bird {
 public:
  penguin() : bird(), _type_(1) {}; 

  virtual void fly() {
    error("program is wrong!");
  }

  virtual ~penguin() {}

 private:
  int _type_;
};

void error(const string& msg) {
  cout << msg << endl;
}

int main(int argc, char* argv[]) {
  penguin qq; 
  qq.fly();
  return 0;
}

[解决办法]
这个好办,在bird的fly函数后边添加花括弧。即:
virtual void fly(){}

热点排行