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

一个小问题,字符串就是不“出来”?请教一下。

cookies5000 发布于 2009-10-26 21:45, 384 次点击
#include<iostream>
using namespace std;

class StudentID{

int value;
public:
    StudentID(){
    static int nextStudentID=0;
    value=++nextStudentID;
    cout<<"Assigning student id "<<value<<"\n";
    }
};
//
class Student{
    string name;
    StudentID id;
public:
    Student(string n="noName"){
    cout<<"Constructing student "+n+"\n";        //line 20
    name=n;
    }
};
//
int main()
{
    Student s("Randy");
    return 0;
}
正常结果应该是:
Assigning student id  1

Constructing student  Randy

可是总是报错 :line 20  2个错误.实在是看不出来了。呵呵,请高手指导。

3 回复
#2
qlc002009-10-26 22:53
Student(string n="noName"){
    cout<<"Constructing student "+n+"\n";        //line 20
    name=n;
    }
红色的地方你的表示法错了,如果你改为cout<<"Constructing student "<<endl;程序就不会有错误,而你想显示名字name的话,最好再定义一个display()函数用来获取name的值,在构造函数中是无法传递的!
#3
cookies50002009-10-26 22:57
谢谢qlc00,佩服。
#4
qlc002009-10-26 23:10
你的头文件中还少了一个#include<string>不然输不出来
1