CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Mar 2008
    Posts
    55

    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;
    }

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: polymorphism by dynamic binding

    It looks like you forgot to implement the functions in your excitement
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Mar 2008
    Posts
    55

    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

  4. #4
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    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.

  5. #5
    Join Date
    Mar 2008
    Posts
    55

    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;
    }

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Mar 2008
    Posts
    55

    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

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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;
        }
        // ...
    };
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  10. #10
    Join Date
    Mar 2008
    Posts
    55

    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
  •  





Click Here to Expand Forum to Full Width

Featured