gpt4 book ai didi

c++ - 运算符重载插入

转载 作者:行者123 更新时间:2023-11-30 04:53:52 26 4
gpt4 key购买 nike

这里有什么问题。它没有按预期工作。

我希望 <<(插入)同时适用于 cout 和 cin。

#include<iostream>

using namespace std;

class a
{
private:
string name;
int age;
unsigned long int salary;
public:
friend ostream& operator << (ostream& ,a );
friend istream& operator << (istream& ,a );
};

ostream& operator << (ostream& dout,a a1){
cout<<"Name = "<< a1.name<<"Age = "<<a1.age<<"Salary = "<<a1.salary<<end;
return dout;
}
istream& operator << (istream& din,a& a1){
cout<<"Enter Your Name , Age , Salary .....Press Enter To Seperate New Value"<<end;
cin>>a1.name>>a1.age>>a1.salary;
}
main(int argc, char const *argv[])
{
a a1;

cin<<a1;
cout<<a1;
return 0;
}

错误太长。-> Error ScreenShot

最佳答案

我不确定你为什么要这样做,但这是可能的。请记住,仅仅因为你可以做某事并不意味着你应该做那件事(参见 C++ Faq Law of Least Surprise .
除了违反最小意外法则之外,您可以做您想做的事,您的代码中只有几个简单的编译错误,一旦修复就可以正常工作(有关工作示例,请参见 here)。

以下是使其编译的更改:

friend istream& operator << (istream& ,a& ); // Note the addition of the &

// Here the variabe is dout, so change to dout. I also added some spacing
ostream& operator << (ostream& dout,a a1){
dout<<"Name = "<< a1.name<<" Age = "<<a1.age<<" Salary = "<<a1.salary<<endl;
return dout;
}

// Here you are using din, so you need to change to din, also you had end instead of endl
istream& operator << (istream& din,a& a1){
cout<<"Enter Your Name , Age , Salary .....Press Enter To Seperate New Value"<<endl;
din>>a1.name>>a1.age>>a1.salary;
return din;
}

这样一来,完整的代码也放在一个方便的地方。这是您的整个程序,其中包含使其编译的更改。

#include<iostream>

using namespace std;

class a
{
private:
string name;
int age;
unsigned long int salary;
public:
friend ostream& operator << (ostream& ,a );
friend istream& operator << (istream& ,a& );
};

ostream& operator << (ostream& dout,a a1){
dout << "Name = "<< a1.name <<" Age = "<< a1.age <<" Salary = "<< a1.salary << endl;
return dout;
}
istream& operator << (istream& din,a& a1){
cout <<"Enter Your Name , Age , Salary .....Press Enter To Seperate New Value" << endl;
din >> a1.name >> a1.age >> a1.salary;
return din;
}
main(int argc, char const *argv[])
{
a a1;

cin<<a1;
cout<<a1;
return 0;
}

现在,如果我们想遵循最小惊奇法则,那么我们将更改 istream使用运算符重载 >>而不是 <<并将控制台文本移出 >>运算符重载并在读取值之前将其呈现给用户。

#include<iostream>

using namespace std;

class a
{
private:
string name;
int age;
unsigned long int salary;
public:
friend ostream& operator << (ostream& ,a );
friend istream& operator >> (istream& ,a& );
};

// Note - Changed variable 'dout' to 'os' for clarity
ostream& operator << (ostream& os, a a1){
os << "Name = " << a1.name << " Age = " << a1.age << " Salary = "<< a1.salary << endl;
return os;
}

// Changed variable from 'din' to 'is' for clarity
istream& operator >> (istream& is,a& a1){
is >> a1.name >> a1.age >> a1.salary;
return is;
}

main(int argc, char const *argv[])
{
a a1;

cout << "Enter Your Name , Age , Salary .....Press Enter To Seperate New Value" << endl;
cin >> a1;
cout << a1;
return 0;
}

关于c++ - 运算符重载插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53781349/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com