views:

162

answers:

4

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?

+3  A: 

You are likely missing #include <string>.

sbi
I had #include <string.h> instead of #include <string>.
BSchlinker
A: 

use this to output the string..

output << "Recipient: " << package.name.c_str() << endl;

Jujjuru
No, that shouldn't be neccessary - overloads for `std::string` are provided by the standard library.
Georg Fritzsche
You actually don't need `c_str()` unless you want a `char*`, possibly to pass it to a C-style function (e.g. `printf`). -1
xtofl
Got it! Sorry didn't know this. Thanks!
Jujjuru
But- something like this could/would highlight if the OP didn't have the `string` definition that was intended.
dash-tom-bang
+2  A: 

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

UncleBens
+1  A: 

Try declaring operator<< as a friend in your class declaration:

struct Package
{
public:
    // Declare {external} function "operator<<" as a friend
    // to give it access to the members.
    friend std::ostream& operator<<(std::ostream&, const Package& p);

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

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

By the way, it is very bad form to use variable names that have the same name as the data type, excepting different case. This wreaks havoc with search and analysis tools. Also, typos can have some fun side-effects too.

Thomas Matthews
I don't agree that variable names shouldn't be named the same as the class, indeed, if it's an instance of the class it's a perfectly fine name! What search and analysis tools are there that don't accept the fact that most programming languages are case sensitive?
dash-tom-bang