Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operator issues with cout

I have a simple package class which is overloaded so I can output package data simply with cout << packagename. I also have two data types, name which is a string and shipping cost with a double.

protected:
    string name;
    string address;
    double weight;
    double shippingcost;

ostream &operator<<( ostream &output, const Package &package )
{
    output << "Package Information ---------------";
    output << "Recipient: " << package.name << endl;
    output << "Shipping Cost (including any applicable fees): " << package.shippingcost;

The problem is occurring with the 4th line (output << "Recipient:...). I'm receiving the error "no operator "<<" matches these operands". However, line 5 is fine.

I'm guessing this has to do with the data type being a string for the package name. Any ideas?

like image 754
BSchlinker Avatar asked Dec 02 '22 05:12

BSchlinker


2 Answers

You must be including a wrong string header. <string.h> and <string> are two completely different standard headers.

#include <string.h> //or in C++ <cstring>

That's for functions of C-style null-terminated char arrays (like strcpy, strcmp etc). cstring reference

#include <string>

That's for std::string. string reference

like image 93
UncleBens Avatar answered Dec 04 '22 20:12

UncleBens


You are likely missing #include <string>.

like image 45
sbi Avatar answered Dec 04 '22 18:12

sbi