C++ operator-> overloading

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

C++ operator-> overloading

Postby tedius » Fri Feb 24, 2006 4:50 pm

Has anyone got any examples of overloading the -> operator.

I have writen the code bellow expecting it to print out
Code: Select all
small child
BIG child


Code: Select all
#include <iostream>

using namespace std;

class parent
{
public:
    parent() {}
    virtual ~parent() {}

    virtual parent *operator->() = 0;

    virtual void print() {cout << "parent" << endl;}
};

class smallchild : public parent
{
public:
    smallchild() {}
    virtual ~smallchild() {}

    virtual smallchild *operator->() {return this;}

    virtual void print() {cout << "small child" << endl;}
};

class bigchild : public parent
{
public:
    bigchild() {}
    virtual ~bigchild() {}

    virtual bigchild *operator->() {return this;}

    virtual void print() {cout << "BIG child" << endl;}
};

int main()
{
    parent *p1 = new smallchild();
    parent *p2 = new bigchild();

    p1->print();
    p2->print();

    return 0;
}


but I can't get it to compile. The linker fails every time with this message

Code: Select all
cd ~/tmp/
gcc -o test class.cc
/tmp/ccACQlyY.o(.text+0x16): In function `main':
: undefined reference to `operator new(unsigned int)'
/tmp/ccACQlyY.o(.text+0x54): In function `main':
: undefined reference to `operator delete(void*)'
/tmp/ccACQlyY.o(.text+0x79): In function `main':
: undefined reference to `operator delete(void*)'
/tmp/ccACQlyY.o(.text+0x86): In function `main':
: undefined reference to `operator new(unsigned int)'
/tmp/ccACQlyY.o(.text+0xc4): In function `main':
: undefined reference to `operator delete(void*)'
/tmp/ccACQlyY.o(.text+0xe9): In function `main':
: undefined reference to `operator delete(void*)'
/tmp/ccACQlyY.o(.text+0x140): In function `__static_initialization_and_destruction_0(int, int)':
: undefined reference to `std::ios_base::Init::Init()'
/tmp/ccACQlyY.o(.text+0x16f): In function `__tcf_0':
: undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccACQlyY.o(.gnu.linkonce.t._ZN10smallchildD1Ev+0x30): In function `smallchild::~smallchild()':
: undefined reference to `operator delete(void*)'
/tmp/ccACQlyY.o(.gnu.linkonce.t._ZN10smallchildD0Ev+0x30): In function `smallchild::~smallchild()':
: undefined reference to `operator delete(void*)'
/tmp/ccACQlyY.o(.gnu.linkonce.t._ZN10smallchild5printEv+0xa): In function `smallchild::print()':
: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
/tmp/ccACQlyY.o(.gnu.linkonce.t._ZN10smallchild5printEv+0x17): In function `smallchild::print()':
: undefined reference to `std::cout'
/tmp/ccACQlyY.o(.gnu.linkonce.t._ZN10smallchild5printEv+0x1c): In function `smallchild::print()':
: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/tmp/ccACQlyY.o(.gnu.linkonce.t._ZN10smallchild5printEv+0x25): In function `smallchild::print()':
: undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))'
/tmp/ccACQlyY.o(.gnu.linkonce.t._ZN8bigchildD1Ev+0x30): In function `bigchild::~bigchild()':
: undefined reference to `operator delete(void*)'
/tmp/ccACQlyY.o(.gnu.linkonce.t._ZN8bigchildD0Ev+0x30): In function `bigchild::~bigchild()':
: undefined reference to `operator delete(void*)'
/tmp/ccACQlyY.o(.gnu.linkonce.t._ZN8bigchild5printEv+0xa): In function `bigchild::print()':
: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
/tmp/ccACQlyY.o(.gnu.linkonce.t._ZN8bigchild5printEv+0x17): In function `bigchild::print()':
: undefined reference to `std::cout'
/tmp/ccACQlyY.o(.gnu.linkonce.t._ZN8bigchild5printEv+0x1c): In function `bigchild::print()':
: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/tmp/ccACQlyY.o(.gnu.linkonce.t._ZN8bigchild5printEv+0x25): In function `bigchild::print()':
: undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))'
/tmp/ccACQlyY.o(.gnu.linkonce.d._ZTI10smallchild+0x0): undefined reference to `vtable for __cxxabiv1::__si_class_type_info'
/tmp/ccACQlyY.o(.gnu.linkonce.d._ZTI8bigchild+0x0): undefined reference to `vtable for __cxxabiv1::__si_class_type_info'
/tmp/ccACQlyY.o(.gnu.linkonce.d._ZTV6parent+0x10): undefined reference to `__cxa_pure_virtual'
/tmp/ccACQlyY.o(.gnu.linkonce.t._ZN6parentD2Ev+0x22): In function `parent::~parent()':
: undefined reference to `operator delete(void*)'
/tmp/ccACQlyY.o(.gnu.linkonce.t._ZN6parentD1Ev+0x22): In function `parent::~parent()':
: undefined reference to `operator delete(void*)'
/tmp/ccACQlyY.o(.gnu.linkonce.t._ZN6parentD0Ev+0x22): In function `parent::~parent()':
: undefined reference to `operator delete(void*)'
/tmp/ccACQlyY.o(.gnu.linkonce.t._ZN6parent5printEv+0xa): In function `parent::print()':
: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
/tmp/ccACQlyY.o(.gnu.linkonce.t._ZN6parent5printEv+0x17): In function `parent::print()':
: undefined reference to `std::cout'
/tmp/ccACQlyY.o(.gnu.linkonce.t._ZN6parent5printEv+0x1c): In function `parent::print()':
: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/tmp/ccACQlyY.o(.gnu.linkonce.t._ZN6parent5printEv+0x25): In function `parent::print()':
: undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))'
/tmp/ccACQlyY.o(.gnu.linkonce.d._ZTI6parent+0x0): undefined reference to `vtable for __cxxabiv1::__class_type_info'
/tmp/ccACQlyY.o(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

Compilation exited abnormally with code 1 at Fri Feb 24 16:42:26


Can anyone help :(

PS sorry for the length.
PPS I'm using CentOS 4 and gcc 3.2.3
User avatar
tedius
 
Posts: 84
Joined: Fri Apr 08, 2005 3:20 pm
Location: Cambridge, England

RE: C++ operator-> overloading

Postby TiggerJim » Fri Feb 24, 2006 9:14 pm

One small question about this. What library are u trying to link 2? No hot on gcc, but sounds like a lib problem.
TiggerJim
 
Posts: 7
Joined: Sun Jul 24, 2005 11:49 am

RE: C++ operator-> overloading

Postby TiggerJim » Fri Feb 24, 2006 9:28 pm

I correct myself now. Use 'g++' NOT 'gcc' and it works fine on.

p.s Gentoo with gcc 3.4.?
TiggerJim
 
Posts: 7
Joined: Sun Jul 24, 2005 11:49 am

RE: C++ operator-> overloading

Postby tedius » Mon Feb 27, 2006 9:50 am

Thanks for that. I don't know how I missed that, Thanks
User avatar
tedius
 
Posts: 84
Joined: Fri Apr 08, 2005 3:20 pm
Location: Cambridge, England


Return to Programming

Who is online

Users browsing this forum: No registered users and 3 guests