问题重载提取运算符 [英] Problem overloading extraction operator

查看:64
本文介绍了问题重载提取运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Visual Studio .Net我已经实现了一个类:


#ifndef CONTRIBUTOR_H

#define CONTRIBUTOR_H

enum性别{男= 1,女,未知};


#include< iostream>

#include< iomanip>

#include< string>

使用命名空间std;


class Contributor

{

朋友ostream&运算符<< (ostream& output,Contributor& RHS);

friend istream& operator>(istream& input,Contributor& RHS);


public:

Contributor();

Contributor(string名称,双重贡献,性别MF,int IDKey);

贡献者(const贡献者和复制品);

贡献者& operator =(const Contributor& RHS);

bool operator ==(const Contributor& RHS);

~Idetributor(){cout<< 在贡献者析构函数中 << endl;}


私人:

std :: string姓名;

双倍贡献;

性别MF;

int IDKey;

};


然后我试图重置提取操作符:


istream&运算符>(istream& input,Contributor& RHS)

{

cout<< \ n输入新的贡献者的名字:" ;;

std :: getline(cin,RHS.Name);

cout<< 输入他们的贡献金额:;

cin> RHS.Contribution;

cout<< 贡献者的性别是什么? " ;;

cin> RHS.MF;

cout<< 为贡献者分配ID密钥:" ;;

cin> RHS.IDKey;


返回输入;

}


但我一直收到这个错误:错误C2679:二进制''>>'':没有运算符

找到了一个正确的 - 性别类型的手操作数(或者

没有可接受的转换)


我在构造函数中定义了性别以及超载

实现。我不明白为什么我得到这个错误。可以

有人帮忙吗?


非常感谢。

解决方案

< blockquote> EM********@gmail.com 写道:


在Visual Studio .Net上工作我已经实现了一个类:


#ifndef CONTRIBUTOR_H

#define CONTRIBUTOR_H


enum性别{男= 1,女,未知};


#include< iostream>

#包括< iomanip>

#include< string>

using namespace std;



Do * NOT * put using namespace std;到一个头文件。


>

class Contributor

{

朋友ostream&运算符<< (ostream& output,Contributor& RHS);

friend istream& operator>(istream& input,Contributor& RHS);


public:

Contributor();

Contributor(string名称,双重贡献,性别MF,int IDKey);

贡献者(const贡献者和复制品);

贡献者& operator =(const Contributor& RHS);

bool operator ==(const Contributor& RHS);

~Idetributor(){cout<< 在贡献者析构函数中 << endl;}


私人:

std :: string姓名;

双倍贡献;

性别MF;

int IDKey;

};


然后我试图重置提取操作符:


istream&运算符>(istream& input,Contributor& RHS)

{

cout<< \ n输入新的贡献者的名字:" ;;

std :: getline(cin,RHS.Name);

cout<< 输入他们的贡献金额:;

cin> RHS.Contribution;

cout<< 贡献者的性别是什么? " ;;

cin> RHS.MF;



您的错误在这里。


cout<< 为贡献者分配ID密钥:" ;;

cin> RHS.IDKey;


返回输入;

}


但我一直收到这个错误:错误C2679:二进制''>>'':没有运算符

找到了一个正确的 - 性别类型的手操作数(或者

没有可接受的转换)


我在构造函数中定义了性别以及超载

实现。我不明白为什么我得到这个错误。可以

有人帮忙吗?



你的编译器告诉你到底出了什么问题。作为

运算符>>(istream&,Contributor&)的一部分,您在Gender类型的对象(特别是RHS.MF)上使用提取运算符

。没有为该枚举定义提取

运算符。

定义它(和插入运算符),一切都会很好。


* EM********@gmail.com


在Visual Studio .Net上工作我已经实现了一个类:


#ifndef CONTRIBUTOR_H

#define CONTRIBUTOR_H


enum性别{男= 1,女,未知};



目前为止确定。


#include< iostream>

#include< iomanip>



考虑使用轻量级的

< iosfwdin头文件,而不是这些重量级的标题。


#include< string>

using namespace std;



永远不要使用using namespace std在头文件中。


class Contributor

{

friend ostream&运算符<< (ostream& output,Contributor& RHS);

friend istream&运算符>(istream& input,Contributor& RHS);



为宏保留全部大写。看看这个小组的常见问题解答以及

Bjarne Stroustrup的常见问题解答。


public:

贡献者();

贡献者(字符串名称,双重贡献,性别MF,int IDKey);



与C ++无关但是,贡献者是否有一个阴茎,一个阴道,或者什么都没有,这有什么关系?我会删除它。除非

性器官真的对贡献很重要(不管它是什么)。如果,例如,

,性别信息用于在计算机生成的感谢信中建立一个合适的

敬意,那就太多了

更好的设计方式来存储适当的敬意。它还会产生更简单的代码;见下文。


Contributor(const Contributor& copy);

Contributor& operator =(const Contributor& RHS);

bool operator ==(const Contributor& RHS);

~Idetributor(){cout<< 在贡献者析构函数中 << endl;}


私人:

std :: string姓名;

双倍贡献;

性别MF;

int IDKey;

};



注意:使用数据成员,您无需定义副本

构造函数和赋值运算符;编译器生成的将很好地执行



然后我尝试重载提取操作符:


istream&运算符>(istream& input,Contributor& RHS)

{

cout<< \ n输入新的贡献者的名字:" ;;

std :: getline(cin,RHS.Name);

cout<< 输入他们的贡献金额:;

cin> RHS.Contribution;

cout<< 贡献者的性别是什么? " ;;

cin> RHS.MF;

cout<< 为贡献者分配ID密钥:" ;;

cin> RHS.IDKey;


返回输入;

}



考虑如何使用文件中的准备数据测试你的类。

考虑如何在一个文件中使用它批处理作业(无声,无用户交互)。


但我一直收到此错误:错误C2679:二进制''>>'':没有运营商

找到了一个性别类型的右手操作数(或者

没有可接受的转换)


我已经在我的构造函数中定义了性别以及超载

实现。我不明白为什么我得到这个错误。可以

有人帮忙吗?



性别被定义为枚举类型。你可以为它提供一个提取器。

该函数(下面的例子)/定义/隐含地,性别的文本

表示,这里是简单的十进制整数:


istream& operator>>(istream& input,Gender& g)

{

int genderVal;

input> genderVal;

if(input.fail()){throw std :: runtime_error(" Ouch ouch"); }

//这里可能检查值并抛出异常,如果不行。

//假设没问题:

g = static_cast<性别> ;(genderVal);

返回输入;

}


请注意,存储首选项会非常容易

敬称(字符串)而不是性别(枚举)。


Hth。,


- Alf


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么会这样是坏事吗?

A:热门发布。

问:usenet和电子邮件中最烦人的是什么?


5月7日上午11:08,Alf P. Steinbach < a ... @ start.nowrote:


* EM.Bate ... @ gmail.com:



贡献者(字符串名称,双重贡献,性别MF,int IDKey);



与C ++无关但是,贡献者是否有一个阴茎,一个阴道,或者什么都没有,这有什么关系?我会删除它。



我是一个人,我希望看不到政治上的正确性

在这里咆哮。我在国家赞助的媒体上得到了足够的信息。


istream& operator>>(istream& input,Gender& g)

{

int genderVal;

input> genderVal;

if(input.fail()){throw std :: runtime_error(" Ouch ouch"); }

//这里可能检查值并抛出异常,如果不行。

//假设没问题:

g = static_cast<性别> ;(genderVal);

返回输入;

}



此代码将允许将值分配给'g''无效

枚举成员。


Working on Visual Studio .Net I''ve implemented a class:

#ifndef CONTRIBUTOR_H
#define CONTRIBUTOR_H

enum Gender {male=1, female, unk};

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

class Contributor
{
friend ostream& operator << (ostream& output, Contributor& RHS);
friend istream& operator >(istream& input, Contributor& RHS);

public:
Contributor();
Contributor(string Name, double Contribution, Gender MF, int IDKey);
Contributor(const Contributor &copy);
Contributor& operator=(const Contributor& RHS);
bool operator== (const Contributor& RHS);
~Contributor() {cout << "In Contributor Destructor" << endl;}

private:
std::string Name;
double Contribution;
Gender MF;
int IDKey;
};

Then I tried to overload the extraction operator thus:

istream& operator >(istream& input, Contributor& RHS)
{
cout << "\nEnter a new contributor''s name: ";
std::getline (cin, RHS.Name);
cout << "Enter the amount of their contribution: ";
cin >RHS.Contribution;
cout << "What is the gender of the contributor? ";
cin >RHS.MF;
cout << "Assign an ID Key to the contributor: ";
cin >RHS.IDKey;

return input;
}

But I keep getting this error: error C2679: binary ''>>'' : no operator
found which takes a right-hand operand of type ''Gender'' (or there is
no acceptable conversion)

I''ve got gender defined in my constructors as well as the overloading
implementation. I don''t understand why I''m getting this error. Can
someone help?

Many thanks.

解决方案

EM********@gmail.com wrote:

Working on Visual Studio .Net I''ve implemented a class:

#ifndef CONTRIBUTOR_H
#define CONTRIBUTOR_H

enum Gender {male=1, female, unk};

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

Do *NOT* put using namespace std; into a header file.

>
class Contributor
{
friend ostream& operator << (ostream& output, Contributor& RHS);
friend istream& operator >(istream& input, Contributor& RHS);

public:
Contributor();
Contributor(string Name, double Contribution, Gender MF, int IDKey);
Contributor(const Contributor &copy);
Contributor& operator=(const Contributor& RHS);
bool operator== (const Contributor& RHS);
~Contributor() {cout << "In Contributor Destructor" << endl;}

private:
std::string Name;
double Contribution;
Gender MF;
int IDKey;
};

Then I tried to overload the extraction operator thus:

istream& operator >(istream& input, Contributor& RHS)
{
cout << "\nEnter a new contributor''s name: ";
std::getline (cin, RHS.Name);
cout << "Enter the amount of their contribution: ";
cin >RHS.Contribution;
cout << "What is the gender of the contributor? ";
cin >RHS.MF;

Your error is here.

cout << "Assign an ID Key to the contributor: ";
cin >RHS.IDKey;

return input;
}

But I keep getting this error: error C2679: binary ''>>'' : no operator
found which takes a right-hand operand of type ''Gender'' (or there is
no acceptable conversion)

I''ve got gender defined in my constructors as well as the overloading
implementation. I don''t understand why I''m getting this error. Can
someone help?

Your compiler is telling you exactly what is wrong. As part of
operator>>(istream&,Contributor&), you are using the extraction operator
on an object of type Gender (specifically, RHS.MF). The extraction
operator is not defined for that enumeration.

Define it (and the insertion operator), and all will be well.


* EM********@gmail.com:

Working on Visual Studio .Net I''ve implemented a class:

#ifndef CONTRIBUTOR_H
#define CONTRIBUTOR_H

enum Gender {male=1, female, unk};

OK so far.

#include <iostream>
#include <iomanip>

Instead of these heavy-weight headers, consider using the light-weight
<iosfwdin a header file.

#include <string>
using namespace std;

Never have "using namespace std" in a header file.

class Contributor
{
friend ostream& operator << (ostream& output, Contributor& RHS);
friend istream& operator >(istream& input, Contributor& RHS);

Reserve all uppercase for macros. See this group''s FAQ as well as
Bjarne Stroustrup''s FAQs.

public:
Contributor();
Contributor(string Name, double Contribution, Gender MF, int IDKey);

Unrelated to C++ but, what does it matter whether a Contributor has a
penis, a vagina, or perhaps nothing? I''d remove that. Unless the
sexual organs really matter for the contribution (whatever it is). If,
for example, the gender information is used to establish a proper
honorific in a computer generated thank-you letter, it would be much
better design-wise to store the proper honorific. It would also yield
simpler code; see below.

Contributor(const Contributor &copy);
Contributor& operator=(const Contributor& RHS);
bool operator== (const Contributor& RHS);
~Contributor() {cout << "In Contributor Destructor" << endl;}

private:
std::string Name;
double Contribution;
Gender MF;
int IDKey;
};

Note: with the data members you have there''s no need to define a copy
constructor and assignment operator; the compiler generated ones will do
nicely.

Then I tried to overload the extraction operator thus:

istream& operator >(istream& input, Contributor& RHS)
{
cout << "\nEnter a new contributor''s name: ";
std::getline (cin, RHS.Name);
cout << "Enter the amount of their contribution: ";
cin >RHS.Contribution;
cout << "What is the gender of the contributor? ";
cin >RHS.MF;
cout << "Assign an ID Key to the contributor: ";
cin >RHS.IDKey;

return input;
}

Consider how you would test your class with prepared data in a file.
Consider how you''d use it in a batch job (silent, no user interaction).

But I keep getting this error: error C2679: binary ''>>'' : no operator
found which takes a right-hand operand of type ''Gender'' (or there is
no acceptable conversion)

I''ve got gender defined in my constructors as well as the overloading
implementation. I don''t understand why I''m getting this error. Can
someone help?

Gender is defined as an enum type. You can provide an extractor for it.
That function (example below) /defines/, implicitly, the textual
representation of genders, here as simple decimal integers:

istream& operator>>( istream& input, Gender& g )
{
int genderVal;
input >genderVal;
if( input.fail() ) { throw std::runtime_error( "Ouch ouch" ); }
// Here perhaps check the value and throw exception if not OK.
// Assuming it is OK:
g = static_cast<Gender>( genderVal );
return input;
}

Note that this would be very much easier it you stored a preferred
honorific (string) instead of a gender (enum).

Hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


On May 7, 11:08 am, "Alf P. Steinbach" <a...@start.nowrote:

* EM.Bate...@gmail.com:


Contributor(string Name, double Contribution, Gender MF, int IDKey);


Unrelated to C++ but, what does it matter whether a Contributor has a
penis, a vagina, or perhaps nothing? I''d remove that.

I for one, would appreciate not seeing political correctness
ranting here. I get enough of it in the state-sponsored media.

istream& operator>>( istream& input, Gender& g )
{
int genderVal;
input >genderVal;
if( input.fail() ) { throw std::runtime_error( "Ouch ouch" ); }
// Here perhaps check the value and throw exception if not OK.
// Assuming it is OK:
g = static_cast<Gender>( genderVal );
return input;
}

This code will allow values to be assigned to ''g'' that are not valid
members of the enum.


这篇关于问题重载提取运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆