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

初学者问题,望高手指教!

fly_woniu 发布于 2008-05-03 18:31, 1882 次点击
#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 时出错.
刚开始学习,就出这样的错误很郁闷,希望大家能帮帮小弟,谢谢!

[[it] 本帖最后由 fly_woniu 于 2008-5-3 18:35 编辑 [/it]]
22 回复
#2
flyingcloude2008-05-03 19:01
//#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;

class student  
{
    friend ostream & operator<<(ostream &,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 ,student &stu){
    out<<"this student info is :\n"
        <<"name :"<<stu.getName()<<"\n"
        <<"age :"<<stu.getAge()<<"\n"
        <<"className :"<<stu.getClassName()<<"\n"<<endl;
    return out;
}

istream &operator>>(istream &in ,student &stu){
    int age;string name,classname;
    in>>"Please input this student info :\n";
    cout<<"name is :"<<"\n";
    in>>name;
    cout<<"age is:"<<"\n";
    in>>age;
    cout<<"className is:"<<"\n";
    in>>classname;
    return in;
}

在VC6环境下编译的
#3
sunkaidong2008-05-03 19:03
#include <iostream>
#include<string>
using namespace std;
namespace demo
{
class student  
{
    friend ostream & operator<<(ostream &,const student & );
    friend istream & operator>>(istream & ,student & );
public:
    student(int,string,string);
    student(student &);
    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;
    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
zjl1382008-05-03 19:05
这些错误还是自已试着改一下吧,少了分号,少了括号等等。
另外:你的MAIN()什么都没做,这才是致命错误:
int main(int argc, char* argv[])
{
    return 0;
}
你自已再修改一下吧。
#5
zjl1382008-05-03 19:09
问二楼三楼,程序能正常运行吗?
这个main()函数这样都行,我没用过,不知道,呵呵!!!
#6
fly_woniu2008-05-03 19:11
能不能说清楚一些啊,我看了好长时间都没有发现问题,可以具体点吗?谢谢了!~
#7
sunkaidong2008-05-03 19:13
我想他只是为了写个类..所以我没帮他加mn()...呵呵main()

[[it] 本帖最后由 sunkaidong 于 2008-5-3 19:31 编辑 [/it]]
#8
fly_woniu2008-05-03 19:18
头文件"iostream.h"应该可以啊?请问一下我的错误倒地处在什么地方?我想知道原因,可以指导一下吗?谢谢了啊!
#9
风的声音2008-05-03 19:20
那么多错误提示,怎么看不到吗
#10
zjl1382008-05-03 19:24
iostream.h可以,但建义用iostream.你的程序错误还算少了,你根据错误提示改一下啊。
#11
fly_woniu2008-05-03 19:33
我用了三楼的代码,可是出现了
test.cpp(98) : fatal error C1010: unexpected end of file while looking for precompiled header directive
这样的错误这是怎么回事?
#12
sunkaidong2008-05-03 19:35
我用vc6.0调好给你的..加个main()
#13
fly_woniu2008-05-03 19:39
对啊,我刚才也用vc6.0把你的代码复制进去调试的,可是就报上面的错误,不知道为什么,我刚学就遇到这样的麻烦,真有点郁闷
#14
zjl1382008-05-03 19:56
你刚学就写这种程序,会不会夸张了一点,你知道你写得程序是什么意思吗?如果不懂,就算程序能运行也没什么意义。
#15
fly_woniu2008-05-03 19:59
知道啊,不过我这种错误却解决不了,哎。。。帮帮忙吧,我有些头大了
#16
zjl1382008-05-03 20:05
你真的懂吗?你看一下你的main()函数:
int main(int argc, char* argv[])
{
    return 0;
}
什么都没有,能不错吗?你若想输出学生的信息,你要调用他的函数啊。
#17
sunkaidong2008-05-03 20:06
你如果是初学,这个程序是相当好了
#include <iostream>
#include<string>
using namespace std;
namespace demo
{
class student  
{
    friend ostream & operator<<(ostream &,const student & );
    friend istream & operator>>(istream & ,student & );
public:
    student(int,string,string);
    student(student &);
    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;
    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;
}
}
int main(void)
{
    demo::student s(14,"王二","一班");
    cout<<s;
    return 0;
}
#18
fly_woniu2008-05-03 20:15
谢谢啊,不过错了,哎。。。
#19
sunkaidong2008-05-03 20:16
你的程序很有点c#的味道..呵呵..
#20
fly_woniu2008-05-03 20:20
恩,你给我的程序我在头部加了一个#include "stdafx.h"
就可以了,不过请问为什么要用using namespace语句?我刚才的那个#include "stdafx.h" 和#include "iostream.h"不可以吗?还有就是我本来是写在三个文件中的,student.h定义类,student.cpp实现类定义,另一个文件中定义main函数,我没有用using namespace std,我一直在用 #include "stdafx.h" 和#include "iostream.h"为什么会出错?请明示好吗?谢谢了!
#21
fly_woniu2008-05-03 20:21
厉害,我一直在学c#,mobile平台的
#22
sunkaidong2008-05-03 20:25
用命名空间不一定就好..因为会有空间污染问题...如果不是后来用一个新的空间隔开,没办法通过编译..#include "stdafx.h"这个文件是c下面一个文件替代..你找找看
#23
yangyangfeng2008-05-04 09:54
#include <iostream>
#include<string>
using namespace std;
namespace demo
{
class student  
{
    friend ostream & operator<<(ostream &,const student & );
    friend istream & operator>>(istream & ,student & );
public:
    student(int,string,string);
    student(student &);
    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;
    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;
}
}
1