tags:

views:

95

answers:

2

Sorry it's lame but I can't figure it out:

class Address {

public:

    uint32_t addr;
    uint16_t port;

public:
    Address();
    Address(uint32_t addr, uint16_t port);
    Address(const Address & src);
    Address& operator=(const Address &src);
    bool isNull();

    friend  std::ostream& operator<<(std::ostream& os, const Address& addr);
    friend  std::ostream& operator<<( const Address& addr, std::ostream& os);
};


 std::ostream& operator<<( std::ostream& os, const Address& addr){
    return os << " ( " << addr.addr << " : " << addr.port << " ) ";
}

 std::ostream& operator<<( const Address& addr, std::ostream& os){

    return os << addr;
 }

says:

../src/streamShare/types.h: In function ‘std::ostream& streamShare::operator<<(std::ostream&, const streamShare::Address&)’:
../src/streamShare/types.h:46: error: no match for ‘operator<<’ in ‘os << " ( "’
../src/streamShare/types.h:45: note: candidates are: std::ostream& streamShare::operator<<(std::ostream&, const streamShare::Address&)

Maybe it's just that I'm in sunday hangover... but hey ostream& << "oihoih" should work !!!

+1  A: 

The following code compiles fine for me on gcc 4.3.2. (I defined the constructor to get it to link properly.)

#include <iostream>

class Address 
{
 public:

    uint32_t addr;
    uint16_t port;

 public:
    Address() : addr(0), port(0) { }

    Address(uint32_t addr, uint16_t port);
    Address(const Address & src);
    Address& operator=(const Address &src);
    bool isNull();

    friend  std::ostream& operator<<(std::ostream& os, const Address& addr);
    friend  std::ostream& operator<<( const Address& addr, std::ostream& os);
};

std::ostream& operator<<( std::ostream& os, const Address& addr)
{
 return os << " ( " << addr.addr << " : " << addr.port << " ) ";
}

int main()
{
 Address a;
 std::cout << a << std::endl;
}

This outputs:

 ( 0 : 0 )

See if this works for you, and if it does, just retrace your steps to see what you're doing differently.

Charles Salvia
A: 

Well, if I include

#include <cstdint>
#include <iostream>

at the beginning of your code, I can compile it using g++ -c -std=c++0x Account.cpp (g++ 4.4.1).

mkluwe