Leaning C++, annoying compile/link error
-
I've managed to compile and run all the samples in the book so far, but all the sudden this code comes up and gives me nasty headaches. When i'm trying to compile the code below everything goes fine until the compiler tries to link it. Here's the error i get:
cd /Users/summer/Documents/C++/xcodeproject/ex /Developer/usr/bin/g++-4.0 -o /Users/summer/Documents/C++/xcodeproject/ex/build/Debug/ex -L/Users/summer/Documents/C++/xcodeproject/ex/build/Debug -F/Users/summer/Documents/C++/xcodeproject/ex/build/Debug -filelist /Users/summer/Documents/C++/xcodeproject/ex/build/ex.build/Debug/ex.build/Objects-normal/i386/ex.LinkFileList -arch i386 -mmacosx-version-min=10.5 -isysroot /Developer/SDKs/MacOSX10.5.sdk Undefined symbols: "vtable for Kvadrat", referenced from: __ZTV7Kvadrat$non_lazy_ptr in main.o ld: symbol(s) not found collect2: ld returned 1 exit status "vtable for Kvadrat", referenced from: __ZTV7Kvadrat$non_lazy_ptr in main.o ld: symbol(s) not found collect2: ld returned 1 exit status
And here's the code:#include
using namespace std;class Form
{
public:
Form() {}
virtual ~Form() {}
virtual long HentAreal() { return -1; }
virtual long HentOmkreds() { return -1; }
virtual void Tegn() {}
};class Rektangel : public Form
{
public:
Rektangel(int laengde, int bredde):
densLaengde(laengde), densBredde(bredde) {}
virtual ~Rektangel() {}
virtual long HentAreal() { return densLaengde * densBredde; }
virtual long HentOmkreds() { return 2*densLaengde + 2*densBredde; }
virtual int HentLaengde() { return densLaengde; }
virtual int HentBredde() { return densBredde; }
virtual void Tegn();
private:
int densBredde;
int densLaengde;
};void Rektangel::Tegn()
{
for (int i = 0;i < densLaengde;i++)
{
for (int j = 0;j < densBredde;j++)
cout << "x";cout << "\\n"; }
}
class Kvadrat : public Rektangel
{
public:
Kvadrat(int laengde);
Kvadrat(int laengde, int bredde);
~Kvadrat();
long HentOmkreds() { return 4 * HentLaengde(); }
};Kvadrat::Kvadrat(int laengde):
Rektangel(laengde, laengde)
{}int main()
{
cout << "Do stuff";
return 0;
}When i remove
Kvadrat::Kvadrat(int laengde): Rektangel(laengde, laengde) {}
It compiles without problems. But i want to create a rectangel upon creation of a square! (rektangel == rectangel && kvadrat == square in danish) -
I've managed to compile and run all the samples in the book so far, but all the sudden this code comes up and gives me nasty headaches. When i'm trying to compile the code below everything goes fine until the compiler tries to link it. Here's the error i get:
cd /Users/summer/Documents/C++/xcodeproject/ex /Developer/usr/bin/g++-4.0 -o /Users/summer/Documents/C++/xcodeproject/ex/build/Debug/ex -L/Users/summer/Documents/C++/xcodeproject/ex/build/Debug -F/Users/summer/Documents/C++/xcodeproject/ex/build/Debug -filelist /Users/summer/Documents/C++/xcodeproject/ex/build/ex.build/Debug/ex.build/Objects-normal/i386/ex.LinkFileList -arch i386 -mmacosx-version-min=10.5 -isysroot /Developer/SDKs/MacOSX10.5.sdk Undefined symbols: "vtable for Kvadrat", referenced from: __ZTV7Kvadrat$non_lazy_ptr in main.o ld: symbol(s) not found collect2: ld returned 1 exit status "vtable for Kvadrat", referenced from: __ZTV7Kvadrat$non_lazy_ptr in main.o ld: symbol(s) not found collect2: ld returned 1 exit status
And here's the code:#include
using namespace std;class Form
{
public:
Form() {}
virtual ~Form() {}
virtual long HentAreal() { return -1; }
virtual long HentOmkreds() { return -1; }
virtual void Tegn() {}
};class Rektangel : public Form
{
public:
Rektangel(int laengde, int bredde):
densLaengde(laengde), densBredde(bredde) {}
virtual ~Rektangel() {}
virtual long HentAreal() { return densLaengde * densBredde; }
virtual long HentOmkreds() { return 2*densLaengde + 2*densBredde; }
virtual int HentLaengde() { return densLaengde; }
virtual int HentBredde() { return densBredde; }
virtual void Tegn();
private:
int densBredde;
int densLaengde;
};void Rektangel::Tegn()
{
for (int i = 0;i < densLaengde;i++)
{
for (int j = 0;j < densBredde;j++)
cout << "x";cout << "\\n"; }
}
class Kvadrat : public Rektangel
{
public:
Kvadrat(int laengde);
Kvadrat(int laengde, int bredde);
~Kvadrat();
long HentOmkreds() { return 4 * HentLaengde(); }
};Kvadrat::Kvadrat(int laengde):
Rektangel(laengde, laengde)
{}int main()
{
cout << "Do stuff";
return 0;
}When i remove
Kvadrat::Kvadrat(int laengde): Rektangel(laengde, laengde) {}
It compiles without problems. But i want to create a rectangel upon creation of a square! (rektangel == rectangel && kvadrat == square in danish)You need a body for
Kvadrat
's destructor."Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
I've managed to compile and run all the samples in the book so far, but all the sudden this code comes up and gives me nasty headaches. When i'm trying to compile the code below everything goes fine until the compiler tries to link it. Here's the error i get:
cd /Users/summer/Documents/C++/xcodeproject/ex /Developer/usr/bin/g++-4.0 -o /Users/summer/Documents/C++/xcodeproject/ex/build/Debug/ex -L/Users/summer/Documents/C++/xcodeproject/ex/build/Debug -F/Users/summer/Documents/C++/xcodeproject/ex/build/Debug -filelist /Users/summer/Documents/C++/xcodeproject/ex/build/ex.build/Debug/ex.build/Objects-normal/i386/ex.LinkFileList -arch i386 -mmacosx-version-min=10.5 -isysroot /Developer/SDKs/MacOSX10.5.sdk Undefined symbols: "vtable for Kvadrat", referenced from: __ZTV7Kvadrat$non_lazy_ptr in main.o ld: symbol(s) not found collect2: ld returned 1 exit status "vtable for Kvadrat", referenced from: __ZTV7Kvadrat$non_lazy_ptr in main.o ld: symbol(s) not found collect2: ld returned 1 exit status
And here's the code:#include
using namespace std;class Form
{
public:
Form() {}
virtual ~Form() {}
virtual long HentAreal() { return -1; }
virtual long HentOmkreds() { return -1; }
virtual void Tegn() {}
};class Rektangel : public Form
{
public:
Rektangel(int laengde, int bredde):
densLaengde(laengde), densBredde(bredde) {}
virtual ~Rektangel() {}
virtual long HentAreal() { return densLaengde * densBredde; }
virtual long HentOmkreds() { return 2*densLaengde + 2*densBredde; }
virtual int HentLaengde() { return densLaengde; }
virtual int HentBredde() { return densBredde; }
virtual void Tegn();
private:
int densBredde;
int densLaengde;
};void Rektangel::Tegn()
{
for (int i = 0;i < densLaengde;i++)
{
for (int j = 0;j < densBredde;j++)
cout << "x";cout << "\\n"; }
}
class Kvadrat : public Rektangel
{
public:
Kvadrat(int laengde);
Kvadrat(int laengde, int bredde);
~Kvadrat();
long HentOmkreds() { return 4 * HentLaengde(); }
};Kvadrat::Kvadrat(int laengde):
Rektangel(laengde, laengde)
{}int main()
{
cout << "Do stuff";
return 0;
}When i remove
Kvadrat::Kvadrat(int laengde): Rektangel(laengde, laengde) {}
It compiles without problems. But i want to create a rectangel upon creation of a square! (rektangel == rectangel && kvadrat == square in danish)you forgot:
Rektangel
class default constructor (declaration & definition).Kvadrat(int laengde, int bredde)
constructor definition.Kvadrat
destructor definition
:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
You need a body for
Kvadrat
's destructor."Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
wow.. that was too simple. i feel stupid. thanks!
-
you forgot:
Rektangel
class default constructor (declaration & definition).Kvadrat(int laengde, int bredde)
constructor definition.Kvadrat
destructor definition
:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain ClarkeYou beat me to it, you addict. :)
Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP
-
You beat me to it, you addict. :)
Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP
Rajesh R Subramanian wrote:
You beat me to it, you addict.
Shhhhhhhhhh, don't spread such rumors. ;)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke