初学者问题,望高手指教!
#include "stdafx.h"
#include "iostream.h"class student  
{
    friend ostream & operator<<(ostream &,const student & );
    friend istream & operator>>(istream & ,student & );
public:
    student(student &);
    student(int,string,string);
    int getAge();
    string getName();
    string getClassName();
    void setAge(int);
    void setName(string);
    void setClassName(string);
    virtual ~student();
private:
    int age;
    string name;
    string className;
};student::student(student &stu)
{
    this->age = stu.age;
    this->className = stu.className;
    this->name = stu.name;
}student::student(int age,string name,string className)
{
    this->age = age;
    this->name = name;
    this->className = className;
}int student::getAge(){
    return this->age;
}string student::getName(){
    return this->name;
}string student::getClassName(){
    return this->className;
}void student::setAge(int age){
    this->age = age;
}void student::setName(string name){
    this->name = name;
}void student::setClassName(string className){
    this->className = className;
}
student::~student()
{
}int main(int argc, char* argv[])
{
    return 0;
}ostream & operator<<(ostream &out ,const student &stu){
    out<<"this student info is :\n"
        <<"name :"<<stu.name<<"\n"
        <<"age :"<<stu.age<<"\n"
        <<"className :"<<stu.className<<"\n"<<endl;
}istream &operator>>(istream &in ,student &stu){
    in>>"Please input this student info :\n"
    cout<<"name is :"<<"\n";
    in>>stu.name;
    cout<<"age is:"<<"\n"
    in>>stu.age;
    cout<<"className is:"<<"\n";
    in>>stu.className;
}
这是我的代码,为什么会报错误?
错误为:
stduentT.cpp
H:\Program Files\Microsoft Visual Studio\MyProjects\stduentT\stduentT.cpp(14) : error C2629: unexpected 'class student ('
H:\Program Files\Microsoft Visual Studio\MyProjects\stduentT\stduentT.cpp(14) : error C2238: unexpected token(s) preceding ';'
H:\Program Files\Microsoft Visual Studio\MyProjects\stduentT\stduentT.cpp(16) : error C2146: syntax error : missing ';' before identifier 'getName'
H:\Program Files\Microsoft Visual Studio\MyProjects\stduentT\stduentT.cpp(16) : error C2501: 'string' : missing storage-class or type specifiers
H:\Program Files\Microsoft Visual Studio\MyProjects\stduentT\stduentT.cpp(17) : error C2146: syntax error : missing ';' before identifier 'getClassName'
H:\Program Files\Microsoft Visual Studio\MyProjects\stduentT\stduentT.cpp(17) : error C2501: 'string' : missing storage-class or type specifiers
H:\Program Files\Microsoft Visual Studio\MyProjects\stduentT\stduentT.cpp(19) : error C2061: syntax error : identifier 'string'
H:\Program Files\Microsoft Visual Studio\MyProjects\stduentT\stduentT.cpp(20) : error C2061: syntax error : identifier 'string'
H:\Program Files\Microsoft Visual Studio\MyProjects\stduentT\stduentT.cpp(24) : error C2146: syntax error : missing ';' before identifier 'name'
H:\Program Files\Microsoft Visual Studio\MyProjects\stduentT\stduentT.cpp(24) : error C2501: 'string' : missing storage-class or type specifiers
H:\Program Files\Microsoft Visual Studio\MyProjects\stduentT\stduentT.cpp(24) : error C2501: 'name' : missing storage-class or type specifiers
H:\Program Files\Microsoft Visual Studio\MyProjects\stduentT\stduentT.cpp(25) : error C2146: syntax error : missing ';' before identifier 'className'
H:\Program Files\Microsoft Visual Studio\MyProjects\stduentT\stduentT.cpp(25) : error C2501: 'string' : missing storage-class or type specifiers
H:\Program Files\Microsoft Visual Studio\MyProjects\stduentT\stduentT.cpp(25) : error C2501: 'className' : missing storage-class or type specifiers
H:\Program Files\Microsoft Visual Studio\MyProjects\stduentT\stduentT.cpp(31) : error C2039: 'className' : is not a member of 'student'
        H:\Program Files\Microsoft Visual Studio\MyProjects\stduentT\stduentT.cpp(9) : see declaration of 'student'
H:\Program Files\Microsoft Visual Studio\MyProjects\stduentT\stduentT.cpp(31) : error C2039: 'className' : is not a member of 'student'
        H:\Program Files\Microsoft Visual Studio\MyProjects\stduentT\stduentT.cpp(9) : see declaration of 'student'
H:\Program Files\Microsoft Visual Studio\MyProjects\stduentT\stduentT.cpp(32) : error C2039: 'name' : is not a member of 'student'
        H:\Program Files\Microsoft Visual Studio\MyProjects\stduentT\stduentT.cpp(9) : see declaration of 'student'
H:\Program Files\Microsoft Visual Studio\MyProjects\stduentT\stduentT.cpp(32) : error C2039: 'name' : is not a member of 'student'
        H:\Program Files\Microsoft Visual Studio\MyProjects\stduentT\stduentT.cpp(9) : see declaration of 'student'
H:\Program Files\Microsoft Visual Studio\MyProjects\stduentT\stduentT.cpp(35) : error C2061: syntax error : identifier 'string'
H:\Program Files\Microsoft Visual Studio\MyProjects\stduentT\stduentT.cpp(36) : error C2511: 'student::student' : overloaded member function 'void (int)' not found in 'student'
        H:\Program Files\Microsoft Visual Studio\MyProjects\stduentT\stduentT.cpp(9) : see declaration of 'student'
H:\Program Files\Microsoft Visual Studio\MyProjects\stduentT\stduentT.cpp(46) : error C2143: syntax error : missing ';' before 'tag::id'
H:\Program Files\Microsoft Visual Studio\MyProjects\stduentT\stduentT.cpp(46) : error C2501: 'string' : missing storage-class or type specifiers
H:\Program Files\Microsoft Visual Studio\MyProjects\stduentT\stduentT.cpp(46) : fatal error C1004: unexpected end of file found
执行 cl.exe 时出错.
刚开始学习,就出这样的错误很郁闷,希望大家能帮帮小弟,谢谢!

解决方案 »

  1.   

    你写的是CPP文件还是Win32 Console Application 啊,如果是前者的话,是不可以包含如stdafx.h之类Windows或者MFc的头文件的
      

  2.   

    我个人觉得把你的程序可以这样修改,首先string是一个类,个人觉得您把所有的string换成char*,
    我已经把你的程序使用VC编译了,修改后可以顺利通过,程序如下:
    1.创建一个基于Win32 console的项目,项目名为student1
    2.新建一个student1.h,里面包含了你的student类的定义,
    //define class student
    #include "iostream.h" 
    #include <string>
    class student   

        friend ostream & operator <<(ostream &,const student & ); 
        friend istream & operator>>(istream & ,student & ); 
    public: 
        student(student&); 
        student(int,char*,char*); 
        int getAge(); 
        char* getName(); 
        char* getClassName(); 
        void setAge(int); 
        void setName(char*); 
        void setClassName(char*); 
        virtual ~student(); 
    private: 
        int age; 
        char* name; 
        char* className; 
    }; 
    3.在新建一个student1.cpp,这个文件包括class student类中方法的实现,如下:
    #include "student1.h"
    student::student(student &stu) 

        this->age = stu.age; 
        this->className = stu.className; 
        this->name = stu.name; 
    } student::student(int age,char* name,char* className) 

        this->age = age; 
        this->name = name; 
        this->className = className; 
    } int student::getAge(){ 
        return this->age; 
    } char* student::getName(){ 
        return this->name; 
    } char* student::getClassName(){ 
        return this->className; 
    } void student::setAge(int age){ 
        this->age = age; 
    } void student::setName(char* name){ 
        this->name = name; 
    } void student::setClassName(char* className){ 
        this->className = className; 

    student::~student() 


    int main(int argc, char* argv[]) 

        return 0; 
    } ostream & operator <<(ostream &out ,const student &stu){ 
        out <<"this student info is :\n" 
             <<"name :" <<stu.name <<"\n" 
             <<"age :" <<stu.age <<"\n" 
             <<"className :" <<stu.className <<"\n" <<endl; 
    return out;
    } istream &operator>>(istream &in ,student &stu){ 
        in>>"Please input this student info :\n" ;
        cout<<"name is :" <<"\n"; 
        in>>stu.name; 
        cout <<"age is:" <<"\n" ;
        in>>stu.age; 
        cout <<"className is:" <<"\n"; 
        in>>stu.className; 
    return in;

    4.编译的结果如下:
    Configuration: student1 - Win32 Debug--------------------
    Compiling...
    student1.cpp
    Linking...student1.exe - 0 error(s), 0 warning(s)
    通过以上的修改,程序就可以正常运行。