Click to See Complete Forum and Search --> : polymorphism by dynamic binding


ptg
May 19th, 2008, 10:48 PM
im working on a toy hierarchy but im getting a linkage error
error LNK2019: unresolved external symbol "public: __thiscall StuffedToy::StuffedToy(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,double)" (??0StuffedToy@@QAE@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0N@Z) referenced in function _main

fatal error LNK1120: 1 unresolved externals

i dont know why im getting this can anyone help?

#include<iostream>
#include <string>
using namespace std;

class Toy{
public:
Toy(const string &,double=0.0);
void setName(const string &);
string getName()const;
void setID(double);
double getID()const;

virtual void print() const;

};
class StuffedToy : public Toy{
public:
StuffedToy(const string&,const string&,double=0.0);
void setColor(const string &);
string getColor()const;

virtual void print() const;

};

int main(){

StuffedToy stuffedToy("bear","brown",123);
StuffedToy *stuffedToyPtr = 0;

stuffedToyPtr = &stuffedToy;
cout<<"dynamic binding: ";
stuffedToyPtr->print();

cout<<endl;

return 0;
}

laserlight
May 19th, 2008, 10:58 PM
It looks like you forgot to implement the functions in your excitement :)

ptg
May 19th, 2008, 11:00 PM
It looks like you forgot to implement the functions in your excitement :)
not quite sure what you mean by that

Plasmator
May 19th, 2008, 11:05 PM
not quite sure what you mean by thatHe means this:
void Foo();

int main()
{
Foo(); //The compiler's happy, but the linker's not.
}
You can't possibly expect that to link. The same applies to the snippet in your OP.

ptg
May 19th, 2008, 11:15 PM
He means this:
You can't possibly expect that to link. The same applies to the snippet in your OP.
so i have to make the functions after the main
like
void Toy::setName(const string& name)
{
toyname=name;
}

laserlight
May 19th, 2008, 11:41 PM
so i have to make the functions after the main
Yes, or implement them inline with the class definition, but what you want to do is typically better for separation into a header and a source file.

ptg
May 19th, 2008, 11:43 PM
Yes, or implement them inline with the class definition, but what you want to do is typically better for separation into a header and a source file.
whats implementing them inline with the class definition mean

laserlight
May 19th, 2008, 11:58 PM
whats implementing them inline with the class definition mean
class Toy{
public:
// ...
void setName(const string& name)
{
toyname=name;
}
// ...
};

Lindley
May 20th, 2008, 12:12 AM
Put simply: The compiler only needs to know, "What do I do?". Your code answers this well enough.

But the linker needs to also know, "How do I do that?". Your code doesn't answer this question.

ptg
May 20th, 2008, 01:32 PM
well i figured it out now. took me a while to get it but i finally got it to work