Hve a problem linking

This is a discussion on Hve a problem linking within the C++ Programming forums, part of the General Programming Boards category; Hi, I hve a question, regarding linkin' ... i'm bulidin' a bank account ... my program does'nt give any error ...

  1. #1
    cooleeze
    Guest

    Question Hve a problem linking

    Hi,
    I hve a question, regarding linkin' ... i'm bulidin' a bank account ... my program does'nt give any error while complilin' .... but when i make an exe its gives me this error:

    Lab 5.obj : error LNK2001: unresolved external symbol "public: int __thiscall BankAccount::withdraw(double)" (?withdraw@BankAccount@@QAEHN@Z)

    Can anybody help me figure out wat this problem is ?
    Thanxs...

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    It means the linker can't find the BankAccount::withdraw() implementation. Make sure that the return type and parameters match your declaration.
    zen

  3. #3
    cooleeze
    Guest

    Exclamation linking problem .......

    Hi,
    Here is my header file , n my c code .... why is this error comin' ? can anybody help ?

    #include "BankAccount.h"
    #include "CAcct.h"
    #include "SAcct.h"
    #include <iostream.h>
    #include <iomanip.h>


    //driver

    //creating variables for program
    int choice = 0; //choice var
    double amount; //amount var
    int invalnum = 0; //invalid number var
    int status; //used to test return values of functions
    int makeTransfer (BankAccount& fromAcct, BankAccount& toAcct); //transfer method
    void deposErr ( void ); //deposit error message method
    void withdErr ( void ); //withdraw error message method
    void transferErr ( void ); //transfer error message method
    void GoodTrans ( void ); //Good transaction message method
    int AcctType = 0; //bank Account TYPE
    char WhatType [41] = "What type of account would you like for "; //cuz i'm lazy




    //entering main
    int main ( void )
    {

    BankAccount Array [3];
    BankAccount *aPtr = Array;
    CAcct *cPtr;
    SAcct *sPtr;

    // ---------------------------------
    // prompt user for type of Account 1
    // ---------------------------------
    cout << WhatType << "account 1?\n"
    << "1. Checking\n"
    << "2. Savings"
    << endl;
    cin >> AcctType; //accept value

    // -------------------------------
    // test value of input for invalid
    // -------------------------------
    if ( AcctType != 1 && AcctType != 2 )
    {
    cout << "Invalid response.\n"
    << "Account 1 is set to Checking."
    << endl;
    //CAcct Acct1; //if invalid, set Acct1 to Checking

    //just to remind myself to fix this
    cout << "I will fix this part later." <<endl;
    }
    else
    {
    if ( AcctType == 1)
    {
    //Acct1 is set to checking
    CAcct *Acct1 = new CAcct();
    cPtr = static_cast< CAcct *>( Acct1 );
    aPtr[1] = *cPtr;

    delete Acct1;
    }
    else if ( AcctType == 2)
    {
    //Acct1 is set to savings
    SAcct *Acct1 = new SAcct();
    sPtr = static_cast< SAcct *>( Acct1 );
    aPtr[1] = *sPtr;

    delete Acct1;
    }
    }

    // ---------------------------------
    // prompt user for type of Account 2
    // ---------------------------------
    cout << WhatType << "account 2?\n"
    << "1. Checking\n"
    << "2. Savings"
    << endl;
    cin >> AcctType; //accept value

    // -------------------------------
    // test value of input for invalid
    // -------------------------------
    if ( AcctType != 1 && AcctType != 2 )
    {
    cout << "Invalid response.\n"
    << "Account 1 is set to Checking."
    << endl;
    //CAcct Acct2; //if invalid, set Acct2 to checking

    //just to remind myself to fix this
    cout << "I will fix this part later." <<endl;
    }
    else
    {
    if ( AcctType == 1)
    {
    //Acct2 is set to checking
    CAcct *Acct2 = new CAcct();
    cPtr = static_cast< CAcct *>( Acct2 );
    aPtr[2] = *cPtr;

    delete Acct2;
    }
    else if ( AcctType == 2)
    {
    //Acct2 is set to savings
    SAcct *Acct2 = new SAcct();
    sPtr = static_cast< SAcct *>( Acct2 );
    aPtr[2] = *sPtr;

    delete Acct2;
    }
    }




    //enter loop to return to main menu when function is complete
    //and choice does NOT equal 9
    while (choice != 9)
    {
    //display main meny
    cout << "\n" << endl;
    cout << "PLEASE MAKE A SELECTION BELOW\n" << endl;
    cout << "1. Show Account 1 Balance" <<endl;
    cout << "2. Show Account 2 Balance" <<endl;
    cout << "3. Deposit to Account 1" <<endl;
    cout << "4. Deposit to Account 2" <<endl;
    cout << "5. Withdraw from Account 1" <<endl;
    cout << "6. Withdraw from Account 2" <<endl;
    cout << "7. Transfer from Account 1 to Account 2" <<endl;
    cout << "8. Transfer from Account 2 to Account 1" <<endl;
    cout << "9. QUIT\n\n" <<endl;
    cout << "Enter Selection Number Here:";
    cin >> choice; //accept value



    //test input for valid menu choice
    if ( choice <1 || choice >9 )
    {
    invalnum = 1;
    }

    else
    {

    //enters switch/case struction
    switch (choice)
    {

    case 1:
    //prints Account 1 balance
    aPtr[1].print();
    break;


    case 2:
    //prints Account 2 balance
    aPtr[2].print();
    break;


    case 3:
    //accepts/tests amount and deposits in to checking
    cout << "Please enter the amount you wish to deposit: $";
    cin >> amount; //accept amount
    status = aPtr[1].deposit( amount );
    //test for invalid numbers
    if (status != 1)
    {
    deposErr(); //prints error message
    }
    else
    {
    GoodTrans(); //prints GoodTransaction message
    }
    aPtr[1].print();
    break;

    case 4:
    cout << "Please enter the amount you wish to deposit: $";
    cin >> amount;
    status = aPtr[2].deposit( amount );
    if (status != 1)
    {
    deposErr();
    }
    else
    {
    GoodTrans();
    }
    aPtr[2].print();
    break;

    case 5:
    cout << "Please enter the amount you wish to withdraw: $";
    cin >> amount;
    status = aPtr[1].withdraw( amount );
    if (status != 1)
    {
    withdErr();
    }
    else
    {
    GoodTrans();
    }
    aPtr[1].print();
    break;

    case 6:
    cout << "Please enter the amount you wish to withdraw: $";
    cin >> amount;
    status = aPtr[2].withdraw( amount );
    if (status != 1)
    {
    withdErr();
    }
    else
    {
    GoodTrans();
    }
    aPtr[2].print();
    break;


    case 7:
    //calls makeTransfer function
    status = makeTransfer ( aPtr[1], aPtr[2] );
    //tests return value for errors
    if (status != 1)
    {
    transferErr(); //prints error message
    }
    else
    {
    GoodTrans(); //prints GoodTrans message
    }
    aPtr[1].print();
    aPtr[2].print();
    break;

    case 8:
    status = makeTransfer ( aPtr[2], aPtr[1] );
    if (status != 1)
    {
    transferErr();
    }
    else
    {
    GoodTrans();
    }
    aPtr[2].print();
    aPtr[1].print();
    break;
    }
    }
    }
    //just wanted to put something
    cout << "Be well." <<endl;
    return 1;

    }

    //makeTransfer fuction - calls transfer method of fromAcct class
    int makeTransfer ( BankAccount& fromAcct, BankAccount& toAcct)
    {
    cout << "Please enter the amount you wish to transfer: $";
    cin >> amount; //accepts value

    //assigns status the return value of transfer method
    status = fromAcct.transfer ( toAcct, amount);
    //tests status for errors - returns number to MAIN
    if (status != 1)
    {
    return 0;
    }
    else
    {
    return 1;
    }
    }


    //deposit error message function
    void deposErr ( void )
    {
    cout << "That was an invalid number." << endl;
    cout << "Your deposit was not completed." << endl;
    }


    //withdrawal error message function
    void withdErr ( void )
    {
    cout << "You do not have enough funds." << endl;
    cout << "Your withdrawal was not completed." << endl;
    }


    //transfer error message function
    void transferErr ( void )
    {
    cout << "Your transfer was not completed due to either" <<endl;
    cout << "lack of funds or the input of an invalid number." <<endl;
    }


    //Good Transaction message function
    void GoodTrans ( void )
    {
    cout << "Your transaction was completed." << endl;
    }

    ----------------------------------
    Header File:

    #ifndef BANKACCOUNT_H
    #define BANKACCOUNT_H

    class BankAccount
    {
    public:
    BankAccount( void );
    BankAccount( double );
    int withdraw( double );
    int deposit( double );
    int transfer( BankAccount &, double );
    void print(void);


    private:
    double balance;


    }; // end BankAccount class

    #endif


    ------------------------------------


    ERROR:

    Lab 5.obj : error LNK2001: unresolved external symbol "public: int __thiscall BankAccount::withdraw(double)" (?withdraw@BankAccount@@QAEHN@Z)

  4. #4
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    As I stated in my last post, it's probably your implementation of the BankAccount::withdraw() function, you'll have to post that. As your code compiles the rest should be ok.
    zen

  5. #5
    cooleeze
    Guest

    Question linking problem......

    Here is the function zen.....

    #include "BankAccount.h"
    #include <iostream.h>

    BankAccount::BankAccount( void )
    {
    balance = 0;
    } // end default BankAccoutn constructor

    BankAccount::BankAccount( double amount )
    {
    if ( amount >= 0 )
    {
    balance = amount;
    }
    else
    {
    balance = 0;
    }
    } // end amount BankAccount Constructor

    int BankAccount::withdraw( double amount )
    {
    if ( amount > 0 && amount <= balance )
    {
    balance -= amount;
    return 1;
    }
    return 0;
    } // end withdrawal method

    int BankAccount::deposit( double amount )
    {
    if ( amount > 0 )
    {
    balance += amount;
    return 1;
    }
    return 0;
    } // end deposit method



    //transfer method i created
    BankAccount::transfer (BankAccount& toAcct, double amount)
    {
    if (this->withdraw ( amount ) != 1)
    {
    amount = 0;
    return 0;
    }
    else
    {
    toAcct.deposit ( amount );
    return 1;
    }
    }



    void BankAccount:rint( void )
    {
    cout << "Your Current Balance is: " << balance << endl;
    } // end print method

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    79
    If you're using a command line linker, the problem might be that you're not including all of your obj files on the command line.
    All generalizations are false

  7. #7
    cooleeze
    Guest

    Exclamation

    I don'nt know , i hve included all my obj files ...

    All i'm using is 3 header files, it should work ... all my other program works ...

    Here r the errors again :

    linking...
    bank.obj : error LNK2001: unresolved external symbol "public: int __thiscall BankAccount::withdraw(double)" (?withdraw@BankAccount@@QAEHN@Z)

    bank.obj : error LNK2001: unresolved external symbol "public: int __thiscall BankAccount::deposit(double)" (?deposit@BankAccount@@QAEHN@Z)

    bank.obj : error LNK2001: unresolved external symbol "public:
    __thiscall SAcct::SAcct(void)" (??0SAcct@@QAE@XZ)

    bank.obj : error LNK2001: unresolved external symbol "public: __thiscall CAcct::CAcct(void)" (??0CAcct@@QAE@XZ)

    bank.obj : error LNK2001: unresolved external symbol "public: __thiscall BankAccount::BankAccount(void)" (??0BankAccount@@QAE@XZ)

    bank.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall BankAccount:rint(void)" (?print@BankAccount@@UAEXXZ)

    bank.obj : error LNK2001: unresolved external symbol "public: int __thiscall BankAccount::transfer(class BankAccount &,double)" (?transfer@BankAccount@@QAEHAAV1@N@Z)

    Debug/bank.exe : fatal error LNK1120: 7 unresolved externals
    Error executing link.exe.

    bank.exe - 8 error(s), 0 warning(s)

    Need help ... plz
    Thanxs.

  8. #8
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Is the implemenation file part of your project? Make sure it's in the Source Files folder.
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with linking files
    By slippy in forum C Programming
    Replies: 2
    Last Post: 11-23-2007, 10:35 PM
  2. Linking problem
    By Agent89 in forum C++ Programming
    Replies: 4
    Last Post: 03-27-2005, 02:03 PM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  4. Long file linking problem
    By hypertension in forum C Programming
    Replies: 3
    Last Post: 10-15-2002, 09:55 PM
  5. Linking problem...
    By BrianK in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2002, 04:13 PM

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