|
-
May 19th, 2008, 10:48 PM
#1
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;
}
-
May 19th, 2008, 10:58 PM
#2
Re: polymorphism by dynamic binding
It looks like you forgot to implement the functions in your excitement
-
May 19th, 2008, 11:00 PM
#3
Re: polymorphism by dynamic binding
 Originally Posted by laserlight
It looks like you forgot to implement the functions in your excitement 
not quite sure what you mean by that
-
May 19th, 2008, 11:05 PM
#4
Re: polymorphism by dynamic binding
 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.
-
May 19th, 2008, 11:15 PM
#5
Re: polymorphism by dynamic binding
 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;
}
-
May 19th, 2008, 11:41 PM
#6
Re: polymorphism by dynamic binding
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.
-
May 19th, 2008, 11:43 PM
#7
Re: polymorphism by dynamic binding
 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
-
May 19th, 2008, 11:58 PM
#8
Re: polymorphism by dynamic binding
whats implementing them inline with the class definition mean
Code:
class Toy{
public:
// ...
void setName(const string& name)
{
toyname=name;
}
// ...
};
-
May 20th, 2008, 12:12 AM
#9
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.
-
May 20th, 2008, 01:32 PM
#10
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|