这个小程序有几个问题,帮忙找出来下说明原因好吗?

summerYe 2010-01-02 10:07:31
#include <iostream>
using namespace std;
class Student
{
private:
int num; //学号
public:
Student(); //默认构造函数
Student(int n); //构造函数
void show()const;
}
Student::Student()
{ //error: constructors not allowed a return type
num = 0;
}
Student::Student(int n)
{
num = n;
}
void show()const
{ //error: show:modifiers not allowed on nonmember functions
cout<<num;
}

int main()
{
Student s1;//error 怎么错了?
s1.show();
Student s2(1);
s2.show();
return 0;
}
...全文
93 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
lengjiankehappy 2010-01-02
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;
class Student
{
private:
int num; //学号
public:
Student(); //默认构造函数
Student(int n); //构造函数
void show()const;
};
Student::Student()
{ //error: constructors not allowed a return type
;
}
Student::Student(int n)
{
num = n;
}
void Student::show()const
{ //error: show:modifiers not allowed on nonmember functions
cout <<num;
}

Student s1;//error 怎么错了?

int main()
{
s1.show();
Student s2(1);
s2.show();
return 0;
}
帮你改了一下
把对象s1定义为全局对象 这样会被自动初始化为零因为只有全局和静态变量可被自动初始化
默认构造函数是什么都不做的 你不能让它又返回值
lengjiankehappy 2010-01-02
  • 打赏
  • 举报
回复
你怎么能显示的写两个构造函数呢 初始化的时候调用哪个编译区器可不知道
kouwenlong 2010-01-02
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 summerye 的回复:]
我把代码改成这样就又不行了,是缺少默认构造函数的原因吗?
#include <iostream>
#include <cstring>
using namespace std;
class Student
{
private:
int num; //学号
char name[30];
public:
Student(); //默认构造函数
Student(int n,char *na); //构造函数
void show()const;
};

Student::Student(int n,char *na)
{
num = n;
strncpy(name,na,28);
name[29]='\0';
}
void Student::show()const
{             
cout < <num < <endl;
cout < <name < <endl;
}

int main()
{
Student s1;
s1.show();
Student s2(1,"hu");
s2.show();
return 0;
}



[/Quote]
你定义对象实例s1的时候,会调用没有参数的构造函数。而你的没有参数的构造函数只是声明了,没有实现。所以无法编译通过。
这样就行了:
Student::Student()
{}
summerYe 2010-01-02
  • 打赏
  • 举报
回复
我把代码改成这样就又不行了,是缺少默认构造函数的原因吗?
#include <iostream>
#include<cstring>
using namespace std;
class Student
{
private:
int num; //学号
char name[30];
public:
Student(); //默认构造函数
Student(int n,char *na); //构造函数
void show()const;
};

Student::Student(int n,char *na)
{
num = n;
strncpy(name,na,28);
name[29]='\0';
}
void Student::show()const
{
cout<<num<<endl;
cout<<name<<endl;
}

int main()
{
Student s1;
s1.show();
Student s2(1,"hu");
s2.show();
return 0;
}


taifeng123 2010-01-02
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;
class Student
{
private:
int num; //学号
public:
Student(); //默认构造函数
Student(int n); //构造函数
void show()const;
}; 分行
Student::Student()
{ //error: constructors not allowed a return type
num = 0;
}
Student::Student(int n)
{
num = n;
}
void Student::show()const //Student::show
{
cout<<num;
}

int main()
{
Student s1;
s1.show();
Student s2(1);
s2.show();
return 0;
}
wanmeiluck123 2010-01-02
  • 打赏
  • 举报
回复
自己不提供,系统会提供一个什么也不做的默认构造函数
kouwenlong 2010-01-02
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 summerye 的回复:]
还有就是想问下自己不提供显式的默认构造函数系统会不会提供一个呢?提供出来的num是0吗?要是我的类里面有一个char name[30],我没有默认构造函数的话,系统要是提供默认值那是会是什么呢?
[/Quote]
如果你提供了一个构造函数,系统就不在提供。否则系统会提供一个默认构造函数。
我不敢肯定,绝大多数情况下是0,不过我相信是随机的,因为我们都要初始化,有时候我们不小心会得到意料不到的结果。
cx510158746 2010-01-02
  • 打赏
  • 举报
回复
学习,学习...
pengzhixi 2010-01-02
  • 打赏
  • 举报
回复

#include <iostream>
using namespace std;
class Student
{
private:
int num; //学号
public:
Student(); //默认构造函数
Student(int n); //构造函数
void show()const;
};
Student::Student()
{ //error: constructors not allowed a return type
num = 0;
}
Student::Student(int n)
{
num = n;
}
void Student::show()const
{ //error: show:modifiers not allowed on nonmember functions
std::cout<<num;
}

int main()
{
Student s1;//error 怎么错了?
s1.show();
Student s2(1);
s2.show();
system("pause");
return 0;
}

昵称很不好取 2010-01-02
  • 打赏
  • 举报
回复
不好意思,脑子进水了,那个show函数不应该改
#include <iostream> 
using namespace std;

class Student
{
private:
int num; //学号
public:
Student();
Student(int n);
void show()const;
};

Student::Student()
{
num = 0;
}

Student::Student(int n)
{
num = n;
}
void Student::show()const
{
cout <<num<<endl;
}

int main()
{
Student s1;
s1.show();
Student s2(1);
s2.show();
return 0;
}
summerYe 2010-01-02
  • 打赏
  • 举报
回复
这个问题能帮我解决吗?[Quote=引用 2 楼 summerye 的回复:]
还有就是想问下自己不提供显式的默认构造函数系统会不会提供一个呢?提供出来的num是0吗?要是我的类里面有一个char name[30],我没有默认构造函数的话,系统要是提供默认值那是会是什么呢?
[/Quote]
昵称很不好取 2010-01-02
  • 打赏
  • 举报
回复
改了几处,代码如下:
#include <iostream> 
using namespace std;

class Student
{
private:
int num; //学号
public:
Student(); //默认构造函数
Student(int n); //构造函数
const void show(); //改变下这个函数的写法
}; //这里漏了一个分号

Student::Student()
{ //error: constructors not allowed a return type
num = 0;
}

Student::Student(int n)
{
num = n;
}
const void Student::show()
{ //error: show:modifiers not allowed on nonmember functions
cout <<num<<endl;
}

int main()
{
Student s1;//error 怎么错了?
s1.show();
Student s2(1);
s2.show();
return 0;
}
summerYe 2010-01-02
  • 打赏
  • 举报
回复
分号写了可是还是不对,那几个错误还是在那里
kouwenlong 2010-01-02
  • 打赏
  • 举报
回复
#include <iostream> 
using namespace std;
class Student
{
private:
int num; //学号
public:
Student(); //默认构造函数
Student(int n); //构造函数
void show()const;
};//注意分号
Student::Student()
{ //error: constructors not allowed a return type
num = 0;
}
Student::Student(int n)
{
num = n;
}
void Student::show()const//注意域
{ //error: show:modifiers not allowed on nonmember functions
cout <<num;
}

int main()
{
Student s1;//error 怎么错了?
s1.show();
Student s2(1);
s2.show();
return 0;
}
summerYe 2010-01-02
  • 打赏
  • 举报
回复
还有就是想问下自己不提供显式的默认构造函数系统会不会提供一个呢?提供出来的num是0吗?要是我的类里面有一个char name[30],我没有默认构造函数的话,系统要是提供默认值那是会是什么呢?
stardust20 2010-01-02
  • 打赏
  • 举报
回复
class Student
{
private:
int num; //学号
public:
Student(); //默认构造函数
Student(int n); //构造函数
void show()const;
};//类的最后个}要有分号

64,636

社区成员

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

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