各位大哥大姐帮忙看看这个程序,,很基础的,可是我不知道哪里错了,郁闷

时间:2008-05-10 05:02:36   来源:论坛整理  作者:  编辑:chinaitzhe
#include <iostream.h>
#include <string>
class Student; //类Student的提前引用声明
class Teacher
{public:
Teacher(){num=0;name=" ";term=0;sex='m';}
Teacher(int n,string nam,int t,char s)
{num=n;name=nam;term=t;sex=s;}
friend Teacher operator=(Teacher t,Student s);
void display()
{cout < <"num is:" < <num < <endl;
cout < <"name is:" < <name < <endl;
cout < <"term is:" < <term < <endl;
cout < <"sex is:" < <sex < <endl;
}
private:
int num;
string name;
int term;
char sex;
};
class Student
{public:
Student(int n,string nam,char s,float sco)
{numl=n;namel=nam;sexl=s;score=sco;}
friend Teacher::Teacher operator =(Teacher t,Student s);
public:
int numl;
string namel;
char sexl;
float score;
};
Teacher operator =(Teacher t,Student s)
{t.num=s.numl;
t.name=s.namel;
t.sex=s.sexl;
t.term=0;
return t;
}
int main()
{Teacher t;
Student s(101,"徐静蕾",'m',95);
t=s;
t.display();
return 0;
}


错误显示如下:E:\练习\a\a.cpp(7) : error C2629: unexpected 'class Teacher ('
E:\练习\a\a.cpp(7) : error C2334: unexpected token(s) preceding '{'; skipping apparent function body
E:\练习\a\a.cpp(9) : error C2801: 'operator =' must be a <Unknown> member
E:\练习\a\a.cpp(18) : error C2146: syntax error : missing ';' before identifier 'name'
E:\练习\a\a.cpp(18) : error C2501: 'string' : missing storage-class or type specifiers
E:\练习\a\a.cpp(18) : error C2501: 'name' : missing storage-class or type specifiers
E:\练习\a\a.cpp(24) : error C2629: unexpected 'class Student ('
E:\练习\a\a.cpp(24) : error C2334: unexpected token(s) preceding '{'; skipping apparent function body
E:\练习\a\a.cpp(26) : error C2143: syntax error : missing ';' before '='
E:\练习\a\a.cpp(26) : error C2433: '__ctor' : 'friend' not permitted on data declarations
E:\练习\a\a.cpp(26) : error C2063: 'Teacher::Teacher::Teacher' : not a function
E:\练习\a\a.cpp(26) : error C2804: binary 'operator =' has too many parameters
E:\练习\a\a.cpp(29) : error C2146: syntax error : missing ';' before identifier 'namel'
E:\练习\a\a.cpp(29) : error C2501: 'string' : missing storage-class or type specifiers
E:\练习\a\a.cpp(29) : error C2501: 'namel' : missing storage-class or type specifiers
E:\练习\a\a.cpp(34) : error C2801: 'operator =' must be a <Unknown> member
E:\练习\a\a.cpp(34) : error C2248: 'num' : cannot access private member declared in class 'Teacher'
E:\练习\a\a.cpp(17) : see declaration of 'num'
E:\练习\a\a.cpp(35) : error C2039: 'name' : is not a member of 'Teacher'
E:\练习\a\a.cpp(5) : see declaration of 'Teacher'
E:\练习\a\a.cpp(35) : error C2039: 'namel' : is not a member of 'Student'
E:\练习\a\a.cpp(23) : see declaration of 'Student'
E:\练习\a\a.cpp(36) : error C2248: 'sex' : cannot access private member declared in class 'Teacher'
E:\练习\a\a.cpp(20) : see declaration of 'sex'
E:\练习\a\a.cpp(37) : error C2248: 'term' : cannot access private member declared in class 'Teacher'
E:\练习\a\a.cpp(19) : see declaration of 'term'
E:\练习\a\a.cpp(42) : error C2078: too many initializers
E:\练习\a\a.cpp(42) : error C2440: 'initializing' : cannot convert from 'const int' to 'class Student'
No constructor could take the source type, or constructor overload resolution was ambiguous
E:\练习\a\a.cpp(43) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class Student' (or there is no acceptable conversion)
执行 cl.exe 时出错.

a.exe - 1 error(s), 0 warning(s)

网友回复:#include <string.h>

网友回复:#include <iostream.h>
#include <string>
using namespace std;
class Student; //类Student的提前引用声明

friend Teacher::Teacher operator =(Teacher t,Student s);
这个重载运算符函数有问题,建议重新考虑重写函数

网友回复:
引用 1 楼 sd9phoenix 的回复:
#include <string.h>

这个早就试过了,不是那里的问题
网友回复:
C/C code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/





class Student;      //类Student的提前引用声明 

class Teacher 

{public: 

    Teacher(){num=0;name=" ";term=0;sex='m';} 

    Teacher(int n,string nam,int t,char s) 

    {num=n;name=nam;term=t;sex=s;} 

    Teacher& operator =(const Student& s); 

    void display() 

    {

        cout <<"num is:"<<num <<endl; 

        cout <<"name is:"<<name <<endl; 

        cout <<"term is:"<<term <<endl; 

        cout <<"sex is:"<<sex <<endl; 

    } 

private: 

    int num; 

    string name; 

    int term; 

    char sex; 

}; 

class Student 

{

public: 

    Student(int n,string nam,char s,float sco) 

    {numl=n;namel=nam;sexl=s;score=sco;} 

public: 

    int numl; 

    string namel; 

    char sexl; 

    float score; 

}; 

Teacher& Teacher::operator =(const Student& s) 

{

    num=s.numl; 

    name=s.namel; 

    sex=s.sexl; 

    term=0; 

    return *this;

} 

int main() 

{

    Teacher t; 

    Student s(101,"徐静蕾",'m',95); 

    t=s; 

    t.display(); 

    return 0; 

} 




网友回复:
引用 2 楼 jie__wang 的回复:
#include <iostream.h>
#include <string>
using namespace std;
class Student; //类Student的提前引用声明

friend Teacher::Teacher operator =(Teacher t,Student s);
这个重载运算符函数有问题,建议重新考虑重写函数


忘记说了,我用的是VC6.0,成员函数重载为友元函数,必须将#include <iostream> 和using namespace std;改为#include <iostream.h>
网友回复:#include <iostream>
#include <string>
using namespace std;

class Student
{public:
Student(int n,string nam,char s,float sco)
{numl=n;namel=nam;sexl=s;score=sco;}

public:
int numl;
string namel;
char sexl;
float score;
};
class Teacher
{public:
Teacher(){num=0;name=" ";term=0;sex='m';}
Teacher(int n,string nam,int t,char s)
{num=n;name=nam;term=t;sex=s;}
Teacher operator=(Student s)
{num=s.numl;
name=s.namel;
sex=s.sexl;
term=0;
return *this;
}
void display()
{cout < <"num is:" < <num < <endl;
cout < <"name is:" < <name < <endl;
cout < <"term is:" < <term < <endl;
cout < <"sex is:" < <sex < <endl;
}
private:
int num;
string name;
int term;
char sex;
};

int main()
{Teacher t;
Student s(101,"徐静蕾",'m',95);
t=s;
t.display();
return 0;
}

网友回复:6楼的编译通过了,请问知道我哪里错了吗
网友回复:赋值操作符(=)重载,必须是成员函数,而且是非静态的。
网友回复:四楼的编译不行吗? 我运行的挺好的呀.
C/C code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/





加上:#include <string.h>

    #include <iostream.h>

    or

    #include <iostream>

    #include <string>

    using namespace std;




网友回复:
C/C code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/





#include  <iostream> 

#include  <string> 

using namespace std; 



class Student 

{

public: 

    Student(int n,string nam,char s,float sco) 

    {

        numl=n;

        namel=nam;

        sexl=s;

        score=sco;

    } 



public: 

    int numl; 

    string namel; 

    char sexl; 

    float score; 

}; 



class Teacher 

{

public: 

    Teacher()

    {

        num=0;

        name=" ";

        term=0;

        sex='m';

    } 



    Teacher(int n,string nam,int t,char s) 

    {

        num=n;

        name=nam;

        term=t;

        sex=s;

    }

     

    //成员函数,非静态

     Teacher operator=(Student s) 

    {

        num=s.numl; 

        name=s.namel; 

        sex=s.sexl; 

        term=0; 

        return *this; 

    }

     

    void display() 

    {

        cout  < <"num is:" < <num  < <endl; 

        cout  < <"name is:"  < <name  < <endl; 

        cout  < <"term is:"  < <term  < <endl; 

        cout  < <"sex is:"  < <sex  < <endl; 

    } 

private: 

    int num; 

    string name; 

    int term; 

    char sex; 

}; 



int main() 

{

    Teacher t; 

    Student s(101,"徐静蕾",'m',95); 

    t=s; 

    t.display(); 

    return 0; 

} 




网友回复:
引用 8 楼 yuzl32 的回复:
赋值操作符(=)重载,必须是成员函数,而且是非静态的。

网友回复:
引用 8 楼 yuzl32 的回复:
赋值操作符(=)重载,必须是成员函数,而且是非静态的。

网友回复:谢谢各位,明白,
关键字:大哥,大姐,帮忙,看看,程序,

文章评论

共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面