| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1882 人关注过本帖
标题:初学者问题,望高手指教!
只看楼主 加入收藏
fly_woniu
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2008-5-3
收藏
 问题点数:0 回复次数:22 
初学者问题,望高手指教!
#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]]
搜索更多相关主题的帖子: 初学者 指教 
2008-05-03 18:31
flyingcloude
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:6
帖 子:598
专家分:1512
注 册:2008-1-13
收藏
得分:0 
//#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环境下编译的
2008-05-03 19:01
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
#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;
}
}

学习需要安静。。海盗要重新来过。。
2008-05-03 19:03
zjl138
Rank: 1
等 级:新手上路
威 望:1
帖 子:788
专家分:0
注 册:2007-11-12
收藏
得分:0 
这些错误还是自已试着改一下吧,少了分号,少了括号等等。
另外:你的MAIN()什么都没做,这才是致命错误:
int main(int argc, char* argv[])
{
    return 0;
}
你自已再修改一下吧。

i like linux...
2008-05-03 19:05
zjl138
Rank: 1
等 级:新手上路
威 望:1
帖 子:788
专家分:0
注 册:2007-11-12
收藏
得分:0 
问二楼三楼,程序能正常运行吗?
这个main()函数这样都行,我没用过,不知道,呵呵!!!

i like linux...
2008-05-03 19:09
fly_woniu
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2008-5-3
收藏
得分:0 
能不能说清楚一些啊,我看了好长时间都没有发现问题,可以具体点吗?谢谢了!~
2008-05-03 19:11
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
我想他只是为了写个类..所以我没帮他加mn()...呵呵main()

[[it] 本帖最后由 sunkaidong 于 2008-5-3 19:31 编辑 [/it]]

学习需要安静。。海盗要重新来过。。
2008-05-03 19:13
fly_woniu
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2008-5-3
收藏
得分:0 
头文件"iostream.h"应该可以啊?请问一下我的错误倒地处在什么地方?我想知道原因,可以指导一下吗?谢谢了啊!
2008-05-03 19:18
风的声音
Rank: 1
等 级:新手上路
帖 子:128
专家分:0
注 册:2007-3-27
收藏
得分:0 
那么多错误提示,怎么看不到吗

一念心清净,莲花处处开。 一花一净土,一土一如来。
2008-05-03 19:20
zjl138
Rank: 1
等 级:新手上路
威 望:1
帖 子:788
专家分:0
注 册:2007-11-12
收藏
得分:0 
iostream.h可以,但建义用iostream.你的程序错误还算少了,你根据错误提示改一下啊。

i like linux...
2008-05-03 19:24
快速回复:初学者问题,望高手指教!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.036707 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved