专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > C++

【询问】这是咋回事? 多谢

发布时间:2011-06-28 15:10:55 文章来源:www.iduyao.cn 采编人员:星星草
【询问】这是怎么回事? 谢谢


//代码 : 
#include <iostream>
#include <string>
using namespace std;
int main ()
{
class Student
{
public :
void display ();
private : 
int num;
string name;
char sex;
};
void  Student :: display ()
{
cout << "num : " << num << endl;
cout << "name : " << name << endl;
cout << "sex : " << sex << endl; 
}
Student stud1, stud2;

return 0;
}


应该怎么改?

------解决方案--------------------
函数作用域,是不开放的,内部可以定义类型,包括类类型。
但是,不可以,把类的成员函数定义在,类外部---- 函数内部;
当然,也不可以,定义在函数外部;
只能,在类内部,直接定义,不能分开定义。

有以下两个办法,解决这个问题:


//1)类在函数外定义

//代码 : 
 #include <iostream>
 #include <string>
 using namespace std; 
 
 class Student{
 public :
     void display ();
 private : 
     int num;
     string name;
     char sex;
 };
 
void  Student :: display (){
    cout << "num : " << num << endl;
    cout << "name : " << name << endl;
    cout << "sex : " << sex << endl; 
}
int main (){
    Student stud1, stud2;
    return 0;




//2) 函数内部的定义的类,类的成员函数, 全部定义在类的内部,不要分开来声明和定义。
int main (){
    class Student{
    public :
        void display (){
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: