【问题标题】:keep getting the error ' no match for 'operator>>' ;不断收到错误 ' no match for 'operator>>' ;
【发布时间】:2019-11-18 13:38:37
【问题描述】:

我已经看到许多其他具有相同错误的问题,但这些问题来自没有重载 >> 运算符的人。我编写的其他一些程序都遇到了这个问题,因为它们都是练习题。

我还查看了我的教科书,并将我的程序与他们的示例程序进行了比较,但看不出我哪里出错了。

完整的错误是(在 main.cpp 中)

第 17 行:错误:'operator>>' 不匹配(操作数类型为 'std::ifstream {aka std::basic_ifstream}' 和 'Suitcase()')|

非常感谢任何和所有建议。

我的标题是

//suitcase.h
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#ifndef SUITCASE_H
#define SUITCASE_H
using namespace std;

class Suitcase
{
public:
    Suitcase();
    Suitcase(double p,double w,double v,string s,string b);
    ~Suitcase();
    double calc_ratio();
    friend bool operator>(const Suitcase s1,const Suitcase s2);
    friend ostream& operator<<(ostream & out,const Suitcase& s);
    friend istream& operator >>(istream& in,Suitcase& s);
private:
    double price,weight,volume;
    string brand,shop;
};
#endif // SUITCASE_H

我的实现是

//suitcaseimplement.cpp
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include "suitcase.h"
using namespace std;

Suitcase::Suitcase()
{
    price=0;
    weight=0;
    volume=0;
    shop="";
    brand="";
}
Suitcase::Suitcase(double p,double w,double v,string s,string b)
{
    price=p;
    weight=w;
    volume=v;
    shop=s;
    brand=b;
}
Suitcase::~Suitcase()
{

}
double Suitcase::calc_ratio()
{
    return (volume/weight);
}
bool operator>(Suitcase s1,Suitcase s2)
{
    if(s1.calc_ratio()>s2.calc_ratio())
        return true;
}
ostream& operator<<(ostream & out,const Suitcase& s)
{
    out<<"The suitcase with the highest ratio is the "<<s.brand<<endl;
    out<<"It is available at "<<s.shop<<endl;
    out<<"It weighs "<<s.weight<<"kgs, has a volume of "<<s.volume<<"and costs R"<<s.price<<endl;

    return out;
}
istream& operator >>(istream& in,Suitcase& s)
{
    in>>s.price>>s.weight>>s.volume>>s.brand>>s.shop;
    return in;
}

最后我的主程序是。

//main.cpp
#include <iostream>
#include <string>
#include <cstdlib>
#include <fstream>
#include "suitcase.h"
using namespace std;

int main()
{
    ifstream infile;
    infile.open("Suitcase.txt");
    if(infile.fail())
        exit(1);

Suitcase current_suitcase(), best_suitcase();
infile>>best_suitcase;
while(infile>>current_suitcase)
{
    if(current_suitcase>best_suitcase)
    {
        current_suitcase=best_suitcase;
    }
}
infile.close();
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout<<best_suitcase;
return 0;
}

【问题讨论】:

  • 非常抱歉您可能发现的其他错误和错误。
  • 查找“最令人头疼的解析”以了解正在发生的事情。
  • 我未能以更简洁的minimal reproducible exampleMCVE on Compiler Explorer 重现您的问题。显然,我错过了Suitcase current_suitcase(), best_suitcase(); 的实际问题。 ;-)

标签: c++ c++11 file-io operator-overloading


【解决方案1】:

这一行

Suitcase current_suitcase(), best_suitcase();

声明两个函数返回Suitcase,而不是定义两个变量。这称为Most vexing parse 规则。去掉多余的括号即可解决问题:

Suitcase current_suitcase, best_suitcase;

另外,不要using namespace std

【讨论】:

  • 哇,非常感谢。您刚刚解决了 4 个其他项目问题。我知道我怎么会犯这个错误。是的,我知道命名空间问题,但这是他们希望我在入门课程中这样做的方式,所以我坚持下去。
【解决方案2】:

在 C++ 中,当你必须初始化不带参数的对象时,你不能放 (),因为它可能被解析为一个带有空初始化器的对象定义或语言标准指定的函数声明,歧义总是在赞成函数声明。所以而不是

Suitcase current_suitcase(), best_suitcase();

使用

Suitcase current_suitcase, best_suitcase;

注意:空初始化器可以通过这种方式用大括号调用Suitcase current_suitcase{};

还有,以防万一:

  • 请注意bool operator&gt;(const Suitcase s1,const Suitcase s2); params 上的 const 限定符应用于对象,并且这些对象是由复制构造函数创建的,因此 const 限定符在这种情况下是无用的,而是取出const 为参数,将这些参数更改为对象的别名,因此以这种方式声明运算符bool operator&gt;(const Suitcase&amp; s1,const Suitcase&amp; s2);

  • 使用using namespace std; 不是一个好趋势:而只需声明要使用该命名空间的函数,就像using std::cout; using std::cin;

【讨论】:

  • 如果你想显式地默认初始化,可能值得一提的是你可以Suitcase current_suitcase{};
猜你喜欢
  • 2012-05-20
  • 1970-01-01
  • 2021-08-23
  • 1970-01-01
  • 1970-01-01
  • 2020-07-12
  • 1970-01-01
  • 2021-12-16
  • 1970-01-01
相关资源
最近更新 更多