编译通过,链接报错!求解答!

lming_08 2011-10-13 01:12:52
代码如下所示:
#include<iostream>
using namespace std;
class Shape
{
public:
float area();//面积
float per();//周长
void show();
};
class Circle:virtual public Shape
{
float x,y,r;
public:
Circle(float xx,float yy,float rr)
{
x=xx;y=yy;r=rr;
}
float area()
{
return 3.14159*r*r;
}
float per()
{
return 2*3.14159*r;
}
void show()
{
cout<<"cir.area="<<area()<<",cir.per="<<per()<<endl;
}
};
class rectangle:virtual public Shape
{
float x,y,length,width;
public:
rectangle(float xx,float yy,float l,float w)
{
x=xx;y=yy;
length=l;
width=w;
}
float area()
{
return length*width;
}
float per()
{
return 2*(length+width);
}
void show()
{
cout<<"rec.area="<<area()<<",rec.per="<<per()<<endl;
}
};
void main()
{
Circle cir(2,3,5);
rectangle rec(2,3,4,5);
cout<<"cir.area="<<cir.area()<<endl<<"rec.area="<<rec.area()<<endl;
Shape *a[10];
a[0]=new Circle(2,3,5);
a[1]=new rectangle(2,3,4,5);
for(int i=0;i<2;i++)
{
a[i]->show();
}
}
...全文
83 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lming_08 2011-10-13
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 mougaidong 的回复:]

引用 5 楼 lming_08 的回复:

引用 3 楼 mougaidong 的回复:

#include<iostream>
using namespace std;
class Shape
{
public:
float area();//面积
float per();//周长
void show(){};// 空实现也要实现!
};
class Circle:v……
[/Quote]再次感谢!
turing-complete 2011-10-13
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 lming_08 的回复:]

引用 3 楼 mougaidong 的回复:

#include<iostream>
using namespace std;
class Shape
{
public:
float area();//面积
float per();//周长
void show(){};// 空实现也要实现!
};
class Circle:virtual public Shape
{
……
[/Quote]

调用就错了

编译能通过,是因为现在的编译采用的都是“独立编译技术”
lming_08 2011-10-13
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 mougaidong 的回复:]

#include<iostream>
using namespace std;
class Shape
{
public:
float area();//面积
float per();//周长
void show(){};// 空实现也要实现!
};
class Circle:virtual public Shape
{
float x,y,……
[/Quote]嗯,谢谢!纠结了好几天啊。
但是为什么要实现呢?area()和per()也都没实现啊。是否因为最后有调用a[i]->show()?
newfarmerchi 2011-10-13
  • 打赏
  • 举报
回复

class Shape
{
public:
float area();//面积
float per();//周长
virtual void show(){};//<------------------here
};



turing-complete 2011-10-13
  • 打赏
  • 举报
回复
#include<iostream>
using namespace std;
class Shape
{
public:
float area();//面积
float per();//周长
void show(){};// 空实现也要实现!
};
class Circle:virtual public Shape
{
float x,y,r;
public:
Circle(float xx,float yy,float rr)
{
x=xx;y=yy;r=rr;
}
float area()
{
return 3.14159*r*r;
}
float per()
{
return 2*3.14159*r;
}
void show()
{
cout<<"cir.area="<<area()<<",cir.per="<<per()<<endl;
}
};
class rectangle:virtual public Shape
{
float x,y,length,width;
public:
rectangle(float xx,float yy,float l,float w)
{
x=xx;y=yy;
length=l;
width=w;
}
float area()
{
return length*width;
}
float per()
{
return 2*(length+width);
}
void show()
{
cout<<"rec.area="<<area()<<",rec.per="<<per()<<endl;
}
};
void main()
{
Circle cir(2,3,5);
rectangle rec(2,3,4,5);
cout<<"cir.area="<<cir.area()<<endl<<"rec.area="<<rec.area()<<endl;
Shape *a[10];
a[0]=new Circle(2,3,5);
a[1]=new rectangle(2,3,4,5);
for(int i=0;i<2;i++)
{
a[i]->show();
}
}
lming_08 2011-10-13
  • 打赏
  • 举报
回复
[Quote=引用楼主 lming_08 的回复:]
代码如下所示:
#include<iostream>
using namespace std;
class Shape
{
public:
float area();//面积
float per();//周长
void show();
};
class Circle:virtual public Shape
{
float x,y,r;
public:
Circle(f……
[/Quote]
错误是:test.obj : error LNK2001: unresolved external symbol "public: void __thiscall Shape::show(void)" (?show@Shape@@QAEXXZ)
qq120848369 2011-10-13
  • 打赏
  • 举报
回复
什么错?

64,683

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧