小弟初学c++ 遇到一个问题 请好心的大哥帮忙指导下

wangliuyun 2007-10-04 09:18:59
#include<iostream>
using namespace std;

class myclass
{
private:
int a;
public:
void set_a(int num);
int get_a();
};
void myclass::set_a(int num=10)
{
a=num;

}
int myclass::get_a();
{ missing function header (old-style formal list? return a;
}
int main()
{
int a;
cout<<a<<endl;
return 0;
}
...全文
265 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangliuyun 2007-11-01
  • 打赏
  • 举报
回复
谢谢大家 其实我想要知道的是 怎么在主函数里面调用类的方法
yuyunliuhen 2007-10-08
  • 打赏
  • 举报
回复
#include <iostream >
using namespace std;

class myclass
{
private:
int a;
public:
void set_a(int num);
int get_a();
};
void myclass::set_a(int num=10)
{
a=num;

}
int myclass::get_a()
{
//missing function header (old-style formal list?
return a;
}
int main()
{
int a;
cout<<a<<endl;
return 0;
}
guzhilei1986 2007-10-08
  • 打赏
  • 举报
回复
顶,类没有在main中实例化。main和类没有什么关系。还有a没有赋值就输出可能会有问题。
#include <iostream >
using namespace std;

class myclass
{
private:
int a;
public:
void set_a(int num);
int get_a();
};
void myclass::set_a(int num=10)
{
a=num;

}
int myclass::get_a()
{
return a;
}
int main()
{
int a;
cout<<a<<endl;
system("pause");
return 0;
}
这段代码我在vs2003中调试时会出错,一位a没有赋值就要输出。但是在devcpp中程序没有什么错误输出一个2.可能是编译器不同造成了这种结果。
starwalker 2007-10-08
  • 打赏
  • 举报
回复
多了个分号
int myclass::get_a(); 
zhaoyue12365 2007-10-08
  • 打赏
  • 举报
回复
int myclass::get_a();小弟,多了个分号,你这里写的类一点用也没有,可能你是想练习语法吧,哈哈
silen_1988 2007-10-08
  • 打赏
  • 举报
回复
不知道你想实现什么功能,main函数是程序的入口,但是你写的MAIN函数里面的东西和类没什么关系~~~
jlu3389 2007-10-08
  • 打赏
  • 举报
回复
int myclass::get_a();
^
多了个分号。这属于语法错误。
jsjacky1101 2007-10-08
  • 打赏
  • 举报
回复
抱歉,还没学到.^_^
我们一起努力吧
whm123m 2007-10-08
  • 打赏
  • 举报
回复
还需要努力啊!
ssss_1111 2007-10-08
  • 打赏
  • 举报
回复
int myclass::get_a();

这里怎么能有逗号?
maozi1012 2007-10-08
  • 打赏
  • 举报
回复
头文件就错了吧??
LS的有空格也不报错的。
patricia1 2007-10-05
  • 打赏
  • 举报
回复
你想要实现一个什么呢,main函数里没怎么看太懂你是什么意思?单纯的定义int a;和上面的类没有什么关联啊
你想要得到结果10吗?
int main()
{
myclass test;
test.set_a();
cout <<test.get_a()<<endl;
return 0;
}
那这样可以吗?
saul2006 2007-10-05
  • 打赏
  • 举报
回复
int myclass::get_a()//这儿没有;
{
return a;
}
int main()
{
int a;
cout <<a <<endl; //两个<中间不要有空格.
return 0;
}
兄弟和我的水平差不多,大家一起加油吧.
chenhuach 2007-10-05
  • 打赏
  • 举报
回复
int myclass::get_a(); {
return a;
} 你的意思应是这样吧!可不用加这段代码也达到同样效果!
geniuscaobo 2007-10-04
  • 打赏
  • 举报
回复
up
AlwaysSLH 2007-10-04
  • 打赏
  • 举报
回复
int myclass::get_a(); //这儿不应该加;吧
{
return a; missing function header (old-style formal list?
}
wangliuyun 2007-10-04
  • 打赏
  • 举报
回复
#include <iostream >
using namespace std;

class myclass
{
private:
int a;
public:
void set_a(int num);
int get_a();
};
void myclass::set_a(int num=10)
{
a=num;

}
int myclass::get_a();
{
return a; missing function header (old-style formal list?
}
int main()
{
int a;
cout < <a < <endl;
return 0;
}

64,683

社区成员

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

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