Reputation: 23
I have seen numerous other questions with the same error but those are from people who haven't overloaded the >> operator. I am having this issue with a few other programs I have written all very similar to each other as they are practice questions.
I have also looked into my textbook and compared my program with their example programs and can't see where I'm going wrong.
The full error is (in main.cpp)
line 17: error: no match for 'operator>>' (operand types are 'std::ifstream {aka std::basic_ifstream}' and 'Suitcase()')|
Any and all advice is greatly appreciated.
My Header is
//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
My implementation is
//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;
}
and finally my main program is.
//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;
}
Upvotes: 2
Views: 431
Reputation: 12909
In C++ when you have to initialize object without parameters, you must not put the ()
because it could be parsed as an object definition with an empty initializer or a function declaration the language standard specifies that the ambiguity is always resolved in favour of the function declaration. so instead of
Suitcase current_suitcase(), best_suitcase();
use
Suitcase current_suitcase, best_suitcase;
Note: empty initializer can be called with curly brackets in this way Suitcase current_suitcase{};
Also, just in case:
note that bool operator>(const Suitcase s1,const Suitcase s2);
the const qualifier on the params is applied to an object, and those objects are been created by the copy constructor, so the const qualifier is useless in this case, but instead of taken out the const
to the params, change those params to alias of objects, so declare the operator in this way bool operator>(const Suitcase& s1,const Suitcase& s2);
if you want to explicit an empty destructor use this sintax Suitcase::~Suitcase() = default;
but take in account that C++ has already a default destructor that will do the same thing of an empty destructor like your
it's not a good trend using using namespace std;
: instead just declare the function you want to use of that namespace, like so using std::cout; using std::cin;
Upvotes: 1
Reputation: 2754
This line
Suitcase current_suitcase(), best_suitcase();
declares two functions that return Suitcase
instead of defining two variables. This is called the Most vexing parse rule. Removing the superfluous parentheses solves the problem:
Suitcase current_suitcase, best_suitcase;
Also, don't do using namespace std.
Upvotes: 4