[C++] overloaden vd operator<<

Pagina: 1
Acties:
  • 27 views sinds 30-01-2008

  • mandroid
  • Registratie: Juli 2002
  • Laatst online: 01-02 07:50
ik heb een klasse hostname geschreven met daarin een string, en een poging tot overloaden van <<,
ik heb me gebaseerd op dit voorbeeldje : http://msdn.microsoft.com..._for_your_own_classes.asp

ziehier hostname.h
code:
1
2
3
4
5
6
7
8
9
10
11
12
class hostname{
private:
    std::string host;
public:
    hostname(std::string h);
    friend std::ostream& operator<< ( std::ostream& os, hostname& hostn );
};

std::ostream& operator<< ( std::ostream& os, hostname& hostn){
   os << hostn.host;
   return os;
}


als ik dit compileer, krijg ik geen problemen, als ik het echter link met de main :
code:
1
2
3
4
5
6
7
8
9
#include "hostname.h" 
#include <string> 
int main(){
std::string s1("test");
    hostname n1(s1);
    std::cout << n1 << std::endl;

    return 0;
}


dan krijg ik volgende errors :
code:
1
2
3
oef2 error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class hostname &)" (??6@$$FYAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@AAVhostname@@@Z) already defined in hostname.obj
oef2 error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class hostname &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@AAVhostname@@@Z) already defined in hostname.obj
oef2 fatal error LNK1169: one or more multiply defined symbols found


ik vermoed dat hij hier wil zeggen dat hij twee definities van de << gevonden heeft, en er niet goed mee kan werken,

is er nu iets mis met mn overloading, of moet ik het eldes zoeken ?

  • farlane
  • Registratie: Maart 2000
  • Laatst online: 22-05 16:53
Als je em in je .h file zet moet je nog ff zeggen dattie ge-inlined moet worden. Als je em niet inline wilt moet je de definitie naar je cpp file verhuizen. ( define once regel )

Somniferous whisperings of scarlet fields. Sleep calling me and in my dreams i wander. My reality is abandoned (I traverse afar). Not a care if I never everwake.


  • mandroid
  • Registratie: Juli 2002
  • Laatst online: 01-02 07:50
dat was het

thx !! _/-\o_

  • .oisyn
  • Registratie: September 2000
  • Laatst online: 22-05 23:07

.oisyn

Moderator Devschuur®

Demotivational Speaker

Mja, dat is wel erg basic hoor :)

Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.


Dit topic is gesloten.