| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 721 人关注过本帖
标题:求助,才学C++(不知程序错哪了)
只看楼主 加入收藏
weiweiqiao
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2005-7-29
收藏
 问题点数:0 回复次数:5 
求助,才学C++(不知程序错哪了)
才学C++,练习类的构造和析构函数 ,出现错误:error C2512: 'cat' : no appropriate default constructor available(错误在main()加粗--cat A;),谢谢 #include<iostream> using namespace std; class cat { public: cat(int x); ~cat(); int putage(); void getage(int age); void print(int c); private: int age; }; cat::cat(int x) { age=x; } cat::~cat() { } int cat::putage() //----定义private age的输出函数 { return (age); }; void cat::print(int c) { int i; c=age; for(i=0;i<c;i++) { cout<<"love you!\t" <<endl; } }; int main() { cat A; int i; cin>>i; A.getage(i); cout<<"---------" <<A.putage() <<endl; A.print(i); return 0; }
搜索更多相关主题的帖子: 才学 
2005-08-03 19:35
Teamwolves
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2005-5-2
收藏
得分:0 
你的getage(int age)在哪定义的?

http://ruiquan.
2005-08-05 12:18
指向指针的指针
Rank: 1
等 级:新手上路
帖 子:339
专家分:0
注 册:2004-8-8
收藏
得分:0 
cat(int x):age(x){};
inline cat(int x){age=x};
这两个哪个执行效率高?

/sign.png" border="0" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://www./sign.png');}" onmousewheel="return imgzoom(this);" alt="" />
2005-08-05 16:22
sbsbsb8888
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2004-9-28
收藏
得分:0 
析构函数在初始化应该有值,如果要没有错就把 cat A 改成cat A(12),编译就可以通过!其中原因就明白了

2005-08-05 18:32
weiweiqiao
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2005-7-29
收藏
得分:0 
多谢几位。
回一楼:getage(int age)没贴过来,getage(int age)定义的是访问函数。既然己经有构造函数,再有个访问函数,是不是多余了?
问二楼:cat(int x):age(x){};  ???。inline cat(int x){age=x}; 构造函数也可以内嵌么?如果可以,你的格式有问题吧。我觉得应当为inline cat::cat(int x){age=x;}  huoz

maCr.Qw
2005-08-05 19:28
weiweiqiao
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2005-7-29
收藏
得分:0 
或者class cat{ cat(){age=x; } .......};
回四楼:你的方法是正确的,我试过了。谢谢:)

maCr.Qw
2005-08-05 19:32
快速回复:求助,才学C++(不知程序错哪了)
数据加载中...
 
   



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

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