我想在构造函数中用cin>>来初始化对象,在VisualC++ 6.0中老是出错。VC中不能在构造函数中使用cin>>吗?如果是的,是因为构造函数不能有返回值吗?我的源码是:
#include "iostream.h"
class Student
{
private:
int num;
public:
Student(int num0);
void Stu_print();
};Student::Student()
{
cin>>num;
}
void Student::Stuprint()
{
cout<<"学号:"<<num<<endl;
}
void main()
{
int num1=2;
Student astu(5);
astu.Stu_print();
}错误提示是:
F:\MyPrograms\constructor_dnt_rtn\dnt_return.cpp(12) : error C2511: 'Student::Student' : overloaded member function 'void (void)' not found in 'Student'
        F:\MyPrograms\constructor_dnt_rtn\dnt_return.cpp(3) : see declaration of 'Student'
F:\MyPrograms\constructor_dnt_rtn\dnt_return.cpp(15) : error C2039: 'Stuprint' : is not a member of 'Student'
        F:\MyPrograms\constructor_dnt_rtn\dnt_return.cpp(3) : see declaration of 'Student'
F:\MyPrograms\constructor_dnt_rtn\dnt_return.cpp(17) : error C2065: 'num' : undeclared identifier
Error executing cl.exe.

解决方案 »

  1.   

    非也,class Student
    {
    private:
    int num;
    public:
             Student();        // 加入这个
    Student(int num0);
    void Stu_print();
    };Student::Student() // 在类的声明中没有
    {
    cin>>num;
    }
    void Student::Stuprint() // 和你声明中的一样呀。
    {
    cout<<"学号:"<<num<<endl;
    }注意基础知识。
      

  2.   

    谢谢!!
    请教ttzzgg_80713:
        小弟现在刚开始学C++,考虑到我现在有C的基础,而且还可以,面向对象又有一点概念,所以找了一本C++简明教程,我想节省些时间尽快进入MFC的学习,这样行吗?如果我现在看的简明教程不好,能介绍几本适合我的现状的好书吗?(如果有电子版的最好)。