 |
Linux Format forums Help, discussion, magazine feedback and more
|
| View previous topic :: View next topic |
| Author |
Message |
tedius

Joined: Fri Apr 08, 2005 4:20 pm Posts: 83 Location: Cambridge, England
|
Posted: Fri Feb 24, 2006 4:50 pm Post subject: C++ operator-> overloading |
|
|
Has anyone got any examples of overloading the -> operator.
I have writen the code bellow expecting it to print out
| Code: | small child
BIG child |
| Code: | #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: | 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 |
|
| Back to top |
|
 |
TiggerJim
Joined: Sun Jul 24, 2005 12:49 pm Posts: 7
|
Posted: Fri Feb 24, 2006 9:14 pm Post subject: RE: C++ operator-> overloading |
|
|
| One small question about this. What library are u trying to link 2? No hot on gcc, but sounds like a lib problem. |
|
| Back to top |
|
 |
TiggerJim
Joined: Sun Jul 24, 2005 12:49 pm Posts: 7
|
Posted: Fri Feb 24, 2006 9:28 pm Post subject: RE: C++ operator-> overloading |
|
|
I correct myself now. Use 'g++' NOT 'gcc' and it works fine on.
p.s Gentoo with gcc 3.4.? |
|
| Back to top |
|
 |
tedius

Joined: Fri Apr 08, 2005 4:20 pm Posts: 83 Location: Cambridge, England
|
Posted: Mon Feb 27, 2006 9:50 am Post subject: RE: C++ operator-> overloading |
|
|
| Thanks for that. I don't know how I missed that, Thanks |
|
| Back to top |
|
 |
| View previous topic :: View next topic |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|