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?
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
You are likely missing #include <string>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With