我正在研究一个程序并调试了很多我在做错的事情。我只有一个错误,并在堆栈和其他论坛上阅读类似的问题。但是,他们试图一遍又一遍地重复这种做法而没有任何积极的结果。
错误显示在标题代码中,它是:
二进制 '>>'没有找到哪个操作符采用'过载函数'类型的右手操作数(或者没有可接受的转换)
#ifndef MONTH_H
#define MONTH_H
#include <iostream>
#include <string>
using namespace std;
class Month{
private:
static string name;
static int monthNumber;
public:
//Default Constructor
Month(){
setName("January");
setNumber(1);
}
//Input of month only constructor and assigning month number from input of name
Month(string n){
name = n;
setName(n);
if(name == "January" || name == "january"){
setNumber(1);
}
else if(name == "February" || name == "february"){
setNumber(2);
}
else if(name == "March" || name == "march"){
setNumber(3);
}
else if(name == "April" || name == "april"){
setNumber(4);
}
else if(name == "May" || name == "may"){
setNumber(5);
}
else if(name == "June" || name == "june"){
setNumber(6);
}
else if(name == "July" || name == "july"){
setNumber(7);
}
else if(name == "August" || name == "august"){
setNumber(8);
}
else if(name == "September" || name == "september"){
setNumber(9);
}
else if(name == "October" || name == "october"){
setNumber(10);
}
else if(name == "November" || name == "november"){
setNumber(11);
}
else if(name == "December" || name == "december"){
setNumber(12);
}
}
//Input of month number only and assigning month name to that number
Month(int n){
setNumber(n);
}
Month(int n, string m){
monthNumber = n;
name = m;
}
//Set the name of the month
void setName(string n){
name = n;
}
//Set the monthes number (and name for increment and decrement)
void setNumber(int n){
monthNumber = n;
switch(monthNumber){
case 1:
setName("January");
break;
case 2:
setName("February");
break;
case 3:
setName("March");
break;
case 4:
setName("April");
break;
case 5:
setName("May");
break;
case 6:
setName("June");
break;
case 7:
setName("July");
break;
case 8:
setName("August");
break;
case 9:
setName("September");
break;
case 10:
setName("October");
break;
case 11:
setName("November");
break;
case 12:
setName("December");
break;
}
}
//Return the name of the month
string getName(){
return name;
}
//Return the month number
int getNumber(){
return monthNumber;
}
//Overload the -- Operator
Month Month::operator--(int){
if(monthNumber == 1)
{
setNumber(12);
setName("December");
}
else {
monthNumber++;
setNumber(monthNumber);
}
}
Month Month::operator--(){
if(monthNumber == 1)
{
setNumber(12);
setName("December");
}
else {
monthNumber++;
setNumber(monthNumber);
}
}
//Overload the ++ operator
Month Month::operator++(int){
if(monthNumber == 12)
{
setNumber(1);
setName("January");
}
else {
monthNumber++;
setNumber(monthNumber);
}
}
Month Month::operator++(){
if(monthNumber == 12)
{
setNumber(1);
setName("January");
}
else {
monthNumber++;
setNumber(monthNumber);
}
}
//Overloading << and >>
friend ostream &operator<<(ostream &strm, const Month &obj){
strm << "# of Month : " << obj.name << " This Corresponds to the month : " << obj.monthNumber << endl;
return strm;
}
//Error here
//-------------------------------------------------------------------
//binary'>>' no operator found which takes a right-hand operand of
//the type'overloaded-function'(or there is no acceptable conversion)
//-------------------------------------------------------------------
friend istream &operator>>(istream &strm, Month &obj) {
strm >> obj.setNumber >> obj.setName;
return strm;
}
};
#endif
#include "Month.h"
#include <cstdlib>
#include <iostream>
using namespace std;
int main(){
Month coolio, first(1), second("June");
coolio++;
second--;
++first;
cout << "Enter the month number: " << endl;
cin >> coolio;
cout << coolio;
cout << second;
cout << first;
return 0;
}
答案 0 :(得分:4)
您无法从输入流中读取数据,并将其与方法调用相结合。您需要将数据读入临时int
和临时string
,然后使用这些int
和string
来调用setter,如下所示:
friend istream &operator>>(istream &strm, Month &obj) {
int number;
string name;
strm >> number >> name;
obj.setNumber(number);
obj.setName(name);
return strm;
}
此外,由于>>
被声明为friend
,因此您可以直接阅读obj
成员变量。
答案 1 :(得分:1)
以下是有关如何简化程序的一些建议(因此我们其他人不必涉及数百行代码):
您无需在setName
方法中致电setNumber
直接访问name
变量。
您的代码将因案件错误而失败
在比较之前,您可以将月份名称转换为全部小写或全部大写。这会使您的if-else
阶梯缩小一半
在网上搜索&#34; c ++转换toupper&#34;。
将映射月份名称用于数字是一种奇特的技巧,但我们不会使用它。您可以使用数组。让数组索引代表月份数:
static const char month_number_to_name[13] =
{
"Nothing",
"january", "february", "march", "april", "may", "june",
"july", "august", "september", "october", "november", "december",
};
// ...
std::cout << "The name of month 10 is: "
<< month_number_to_name[10]
<< "\n";
无需保存月份名称和号码。使用上面的翻译数组,您可以在名称和数字之间进行转换 我建议保持月份数变量。如果需要名称,使用月号来获取名称,进程名称。
在编程行业中有很多研究表明程序越长,它的缺陷概率就越大。只考虑打字。输入更多字母会增加拼写错误的机会。
使用数据结构有利于减少程序大小。