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

书上的题目,我改了一下就报错了(不知道怎么去改)求帮助

Theblueman 发布于 2018-05-23 16:42, 1750 次点击
(Dev-C++上跑了一下)
程序代码:

#include<iostream>
#include<cstring>
using namespace std;

const int L=100;
class Person {
    private:
        char name[L],id[L];
    public:
        Person()    {}
        Person(char* N,char* I)    {
            strcpy(name,N);
            strcpy(id,I);
        }
        friend istream& operator >>(istream& is,Person& pe);
        friend ostream& operator <<(ostream& os,Person& pe);
};
istream& operator >>(istream& is,Person& pe)    {
    cout<<"Please input the name:";
    is>>pe.name;
    cout<<"Please input the id:";
    is>>pe.id;
    return is;
}
ostream& operator <<(ostream& os,Person& pe)    {
    cout<<"Name:"<<pe.name<<'\n';
    cout<<"Id:"<<pe.id<<endl;
    return os;
}
int main()    {
    Person object_1(),object_2("John","3012256");
    cin>>object_1;
    cout<<object_1<<endl;
    cout<<object_2<<endl;
    return 0;   
}

报错是:[Error] cannot bind 'std::istream {aka std::basic_istream<char>}' lvalue to 'std::basic_istream<char>&&'

2 回复
#2
rjsp2018-05-23 16:59
Person object_1()
我怀疑你其实想要的是 Person object_1
即,定义一个Person类型的变量object_1

Person object_1() 是什么?
它是一个函数声明,即 Person object_1( void ),返回类型是Person,参数列表为空,函数名为object_1
#3
Theblueman2018-05-23 19:57
回复 楼主 Theblueman
,这么简单我居然搞错了,谢谢大佬提醒。
1