| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 721 人关注过本帖
标题:求助,才学C++(不知程序错哪了)
取消只看楼主 加入收藏
weiweiqiao
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2005-7-29
收藏
 问题点数:0 回复次数:2 
求助,才学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
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.013802 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved