polymorphism by dynamic binding
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?
Code:
#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;
}
Re: polymorphism by dynamic binding
It looks like you forgot to implement the functions in your excitement :)
Re: polymorphism by dynamic binding
Quote:
Originally Posted by laserlight
It looks like you forgot to implement the functions in your excitement :)
not quite sure what you mean by that
Re: polymorphism by dynamic binding
Quote:
Originally Posted by ptg
not quite sure what you mean by that
He means this:
Code:
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.
Re: polymorphism by dynamic binding
Quote:
Originally Posted by Plasmator
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;
}
Re: polymorphism by dynamic binding
Quote:
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.
Re: polymorphism by dynamic binding
Quote:
Originally Posted by laserlight
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
Re: polymorphism by dynamic binding
Quote:
whats implementing them inline with the class definition mean
Code:
class Toy{
public:
// ...
void setName(const string& name)
{
toyname=name;
}
// ...
};
Re: polymorphism by dynamic binding
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.
Re: polymorphism by dynamic binding
well i figured it out now. took me a while to get it but i finally got it to work