Linker Error !!!!

This is a discussion on Linker Error !!!! within the C++ Programming forums, part of the General Programming Boards category; I had written a program in C++, called Accounts. Here Account is the base class and CA and SA the ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    42

    Unhappy Linker Error !!!!

    I had written a program in C++, called Accounts.
    Here Account is the base class and CA and SA the derived classes.

    A function statement() is a virtual function in the base and redefined in each of the derived classes.

    Code:
    class Account  
    {
    private:
    	int Acc;
    	char name[20];
    public:                                                     
    	Account();
    	Account(int);
    	virtual ~Account();
        virtual void withdrawals(const float&) = 0;
    	virtual float get_interest () const = 0;
    	virtual void statement () const = 0;
    	void print() const;
    };
    Redinitions in CA and SA are
    Code:
    void statement() const;
    The code is different in each case.

    The program compiles properly, but I get a linker error
    Account.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Account::statement(void)const " (?statement@Account@@UBEXXZ)
    Debug/Accounts.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.


    The statement function is as follows

    Code:
    //in derived class CA (Current Account)
    void CA::statement() const  
    {
    	print();
    	float I;
    	I = (*this).get_interest();
    	cout <<"\n Type : Current Account";
    	cout <<"\n Savings = "<<Savings;
    	cout <<"\n Interest earned is "<<I;
    	cout <<"\n Thanks";
    }
    
    //inderived class SA Savings Account
    void SA::statement() const 
    {
    	print();
        float I;
    	I = (*this).get_interest();
    	cout <<"\n Type : Savings Account";
    	cout <<"\n Savings = "<<Savings;
    	cout <<"\n Interest earned is "<<I;
    	cout <<"\n Savings (with Interest) = "<<Savings + I;
        cout <<"\n Thanks";
    }
    The print() function is defined in the base class

    Code:
    //print function in base class Account
    void Account:: print() const
    {
    	cout << "\n A/C No. "<< Acc;
    	cout <<"\n A/C holder: "<<name;
    }
    This print() function is not redefined in any derived class. i am at wits end trying to figure why I am getting this linker error (in compiles perfectly). I am using Microsoft Visual C++ 6.0.

    Could Someone please help ???
    First hear, then understand, and then, leaving all distractions, shut your mind to outside influences and devote yourself to developing the truth within you.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    What is this line?

    Code:
    (*this).get_interest();
    Kuphryn

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    93
    Account.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Account::statement(void)const " (?statement@Account@@UBEXXZ)
    Debug/Accounts.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.


    i don't use Visual C++.. but it looks like u are using multiple files in your project.. is this the case? if so u are #including wrong somewhere..

    easy solution is to add all the files to the project...
    hard solution is manually link the .obj files.....

    i'm too lazy to read ur code... so i may be way off..

    Summary:
    no compiler errors.. so probably linking problem.. good luck trying to fix it..

  4. #4
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    I think you need to post more code for us to help you, especially the derived classes declarations - including the Headers used in those declarations.

    It's saying that it cannot find the implementation of the statement func() in the Account Obj. which is what should happen in Pure Virtuals but something may have gotten screwed up or maybe one of your derived classes is trying to call on that func() which doesn't exist in the base class.

    You should sort through your derived class code - or rewrite your Base Class Account.
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    42

    Smile Linker Error !!! SOLVED

    Thanks .... problem solved.

    I added an implementation of the statement function in the base class (Account).

    Code:
    void Account::statement() const
    { }
    Thanks for helping me understand what the linker was saying.



    Originally posted by OneStiffRod
    I think you need to post more code for us to help you, especially the derived classes declarations - including the Headers used in those declarations.

    It's saying that it cannot find the implementation of the statement func() in the Account Obj. which is what should happen in Pure Virtuals but something may have gotten screwed up or maybe one of your derived classes is trying to call on that func() which doesn't exist in the base class.

    You should sort through your derived class code - or rewrite your Base Class Account.
    First hear, then understand, and then, leaving all distractions, shut your mind to outside influences and devote yourself to developing the truth within you.

  6. #6
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    Good thing!!!

    You could also change your (*this) pointers to the following:
    instead of....
    Code:
    I = (*this).get_interest();
    do this...
    Code:
    I = this->get_interest();
    It's less typing and more clear.

    Also, just make sure that your Derived classes work ok since that was a weird error to show up. Make sure that the derived classes really are calling their implementations of the statement() and not the Base class Accounts version.
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  7. #7
    Unregister5
    Guest

    or..

    or you could just do this

    I = get_interest( );

  8. #8
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Or you could just simply make the function in the base class purely virtual.

    Code:
    class SomeClass{
       SomeClass();
    
      virtual fillMeUp(void) const = 0;
      // =0 makes this pure virtual
     ......
    };

  9. #9
    Shadow12345
    Guest
    virtual fillMeUp(void) const = 0;
    is the const = 0 necessary? I thought that was only used when the function returns a pointer.

  10. #10
    Registered User
    Join Date
    Nov 2002
    Posts
    42
    = 0 is used to make the function a pure virtual.
    First hear, then understand, and then, leaving all distractions, shut your mind to outside influences and devote yourself to developing the truth within you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 02:29 PM
  2. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 12:54 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21