注册 登录
编程论坛 C++教室

C++刚刚看到类 照着书里的程序写了一遍 不知道怎么有错误。

一只失语的猫 发布于 2011-12-07 14:32, 802 次点击
求大虾们解释一下。 那里出错了。

//Studenr61_1.h
#include<iostream.h>
#include<string.h>

class student
{
private:
    unsigned int code;
    string name;
public:
    void SetCode(unsigned int code)
    {
        this->code=code;
    }
    int GetCode()
    {
        return code;
    }
    void SetName(string  name)
    {
        this->name=name;
    }
    string GetName()
    {
        return name;
    }
    void shoe();
};
void Students::show()
{
    cout<<"编号:"<<code<<"\t"<<"姓名"<<name<<"\t";
}



--------------------Configuration: jj - Win32 Debug--------------------
Compiling...
jj.cpp
C:\Documents and Settings\Administrator\桌面\编程文件存档\jj.cpp(9) : error C2146: syntax error : missing ';' before identifier 'name'
C:\Documents and Settings\Administrator\桌面\编程文件存档\jj.cpp(9) : error C2501: 'string' : missing storage-class or type specifiers
C:\Documents and Settings\Administrator\桌面\编程文件存档\jj.cpp(9) : error C2501: 'name' : missing storage-class or type specifiers
C:\Documents and Settings\Administrator\桌面\编程文件存档\jj.cpp(11) : error C2059: syntax error : 'constant'
C:\Documents and Settings\Administrator\桌面\编程文件存档\jj.cpp(19) : error C2061: syntax error : identifier 'string'
C:\Documents and Settings\Administrator\桌面\编程文件存档\jj.cpp(23) : error C2146: syntax error : missing ';' before identifier 'GetName'
C:\Documents and Settings\Administrator\桌面\编程文件存档\jj.cpp(23) : error C2501: 'string' : missing storage-class or type specifiers
C:\Documents and Settings\Administrator\桌面\编程文件存档\jj.cpp(26) : warning C4183: 'GetName': member function definition looks like a ctor, but name does not match enclosing class
C:\Documents and Settings\Administrator\桌面\编程文件存档\jj.cpp(29) : error C2653: 'Students' : is not a class or namespace name
C:\Documents and Settings\Administrator\桌面\编程文件存档\jj.cpp(31) : error C2065: 'code' : undeclared identifier
C:\Documents and Settings\Administrator\桌面\编程文件存档\jj.cpp(31) : error C2065: 'name' : undeclared identifier
执行 cl.exe 时出错.

jj.obj - 1 error(s), 0 warning(s)


急。。。。
3 回复
#2
一只失语的猫2011-12-07 14:34
   void shoe();
};
void Students::show()
{
    cout<<"编号:"<<code<<"\t"<<"姓名"<<name<<"\t";
}

  这里的show改过了还是错。
#3
cosam2011-12-07 15:29
我用vs2008。
程序代码:
#include<iostream>
#include<string>

class student
{
private:
    unsigned int code;
    std::string name;
public:
    void SetCode(unsigned int code)
    {
        this->code=code;
    }
    int GetCode()
    {
        return code;
    }
    void SetName(std::string name)
    {
        this->name=name;
    }
    std::string GetName()
    {
        return name;
    }
    void show();
};
void student::show()//student跟类名一样,不要大写了
{
    std::cout<<"编号:"<<code<<"\t"<<"姓名"<<name<<"\t";
}
int main()//写上入口函数吧
{
    return 0;
}

 
#4
jj74125302011-12-08 00:37
类名是student    为什么在类外写成员函数体的时候却是students呢
1