14 Replies - 619 Views - Last Post: 15 March 2012 - 01:56 PM Rate Topic: -----

#1 ironelf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 29-November 11

Multiple definitions error when i try and compile

Posted 15 March 2012 - 06:29 AM

The program is to create a base account class and two derived checking and savings account classes. The functions inside the base class are virtual. One of the errors i am receiving are:
savings.cpp:15: multiple definition of `SavingsAccount::withdraw(double)'
savings.cpp:15: first defined here.

account.h:
#ifndef BANKACCOUNT_H
#define BANKACCOUNT_H
class BankAccount {
    
protected:
    double balance;
    int NumWithdraws;
    int NumDeposits;
    double InterestRate;
    double MonthlyServiceCharge;
    
public:
    BankAccount(double Bal, double Rate)
    /*Pre-condition: All variables are zero, since it is a creation of a
                     class.
     Post-condition: Account created so balance has a value and the rate also
                     has a value.*/{
        balance = Bal;
        InterestRate = Rate;
    };

    virtual void deposit(double Amount)
    /*Pre-condition: The amount should be positive.
     Post-condition: Balance should have increased and the number of deposits 
                     should also have increased.*/ {
        double deposit = Amount;
        balance = balance + Amount;
        ++NumDeposits;
    };

    virtual void withdraw(double Amount)
    /*Pre-condition: The amount should be positive.
     Post-condition: Balance should have decreased and the number of withdraws
                     should also have increased.*/ {
        double withdraw = Amount;
        balance = balance - Amount;
        ++NumWithdraws;
    };

    virtual void CalcInt(double Rate)
    /*Pre-condition: Rate should be positive.
     Post-condition: Balance should have increased.*/ {
        double MonthlyRate = Rate / 12;
        double Interest = balance * MonthlyRate;
        balance = balance + Interest;
    };

    virtual void monthlyProc(double charge)
    /*Pre-condition: Balance should be positive.
     Post-condition: Variables should be equal to 0.*/ {
        MonthlyServiceCharge = charge;
        balance = balance - MonthlyServiceCharge;
        CalcInt(InterestRate);
        NumWithdraws = 0;
        NumDeposits = 0;
        MonthlyServiceCharge = 0;
    }
};
#endif


savings.h:
#ifndef SAVINGS_H
#define SAVINGS_H
#include "account.h"

class SavingsAccount : BankAccount {
    public:
        SavingsAccount(double bal, double rate) : BankAccount(bal, rate)
        {};
        
        void withdraw(double);
        
        void monthlyProc(double);
};
#endif


and savings.cpp
#include "savings.h"
#include <iostream>

void SavingsAccount::withdraw(double Amount){
    if((balance - Amount) <= 0){
        std::cout << "Your account cannot make that transaction.\n";
        if((balance - 15) <= 0){
            std::cout << "You have been charged a $15 service fee.\n";
            std::cout << "You now owe the bank: " << (balance - 15) 
                    << std::endl;
            balance = balance - 15;
        }else{
            balance = balance - 15;
            std::cout << "A mandatory $15 has been deducted from your account,"
                    " your balance is now: " << balance << std::endl;
        }
    }else{
        std::cout << "You are now withdrawing: " << Amount << std::endl;
        BankAccount::withdraw(Amount);
        std::cout << "Transaction made. Thank you." << std::endl;
    }
}

void SavingsAccount::monthlyProc(double charge){
    charge = NumWithdraws * .1;
    charge = charge + 5;
    BankAccount::monthlyProc(charge);
    std::cout << "Completing the Savings Account transactions." << std::endl;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Multiple definitions error when i try and compile

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Multiple definitions error when i try and compile

Posted 15 March 2012 - 06:42 AM

You didn't post enough of the error message. The error is occurring while some other file (which you also didn't post) is being compiled.
Was This Post Helpful? 0
  • +
  • -

#3 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 630
  • View blog
  • Posts: 2,109
  • Joined: 31-December 10

Re: Multiple definitions error when i try and compile

Posted 15 March 2012 - 06:56 AM

You have a semicolon after you define your SavingsAccount constructor, since it's a definition, you don't need the semicolon.
Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Multiple definitions error when i try and compile

Posted 15 March 2012 - 07:00 AM

View Postvividexstance, on 15 March 2012 - 08:56 AM, said:

You have a semicolon after you define your SavingsAccount constructor, since it's a definition, you don't need the semicolon.

What problem does that solve?
Was This Post Helpful? -1
  • +
  • -

#5 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 630
  • View blog
  • Posts: 2,109
  • Joined: 31-December 10

Re: Multiple definitions error when i try and compile

Posted 15 March 2012 - 07:03 AM

I just compiled those files and I didn't receive any multiple definition errors, could you tell us how you're compiling and possibly post what your main() looks like?

View Postr.stiltskin, on 15 March 2012 - 09:00 AM, said:

View Postvividexstance, on 15 March 2012 - 08:56 AM, said:

You have a semicolon after you define your SavingsAccount constructor, since it's a definition, you don't need the semicolon.

What problem does that solve?

It's a problem in the OP's code that I was just pointing out. You only need to have a semicolon after a member function declaration, not a definition.

What did your post help to solve?

This post has been edited by vividexstance: 15 March 2012 - 07:03 AM

Was This Post Helpful? -1
  • +
  • -

#6 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Multiple definitions error when i try and compile

Posted 15 March 2012 - 07:11 AM

It's not a "problem". I can put ;;;;;;;;;;;;;;;;;;;;;;; as many extraneous semicolons as I want in a program, and never generate even a warning. It simply represents an empty statement.

True, that semicolon is unnecessary, but irrelevant.

What did your post help to solve?

@ironelf: as I mentioned 25 minutes ago, your problem can't be solved from the information you posted. We need the complete error message, and probably more of the code. Or, you can look through the rest of your code to find where you redefined SavingsAccount::withdraw(double)

This post has been edited by r.stiltskin: 15 March 2012 - 07:08 AM

Was This Post Helpful? -1
  • +
  • -

#7 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 630
  • View blog
  • Posts: 2,109
  • Joined: 31-December 10

Re: Multiple definitions error when i try and compile

Posted 15 March 2012 - 07:11 AM

Haha you negative repped me but if you read my post, I state how I compiled the OP's code fine without any errors. So go negative rep some more helpful posts.
Was This Post Helpful? -1
  • +
  • -

#8 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Multiple definitions error when i try and compile

Posted 15 March 2012 - 07:15 AM

You're not being helpful at all. You're just cluttering up yet another topic with pointless arguments that only distract and confuse the OP. I already told the OP that they had to provide additional information in Post #2 almost 1/2 hour ago. I didn't bother to mention that I had already compiled the code then because that was irrelevant.
Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5315
  • View blog
  • Posts: 11,371
  • Joined: 02-June 10

Re: Multiple definitions error when i try and compile

Posted 15 March 2012 - 07:19 AM

Alright kiddies: Knock off the down rep war BS.

There's nothing wrong with a discussion and asking questions that don't lead directly to solving the problem. Its how people learn new things and exchange ideas.

Please try to act like grown-ups and senior developers that people might want to respect. This sort of grade school "You slap me, I slap you" behavior reflects badly on all of us and the site itself.
Was This Post Helpful? 1
  • +
  • -

#10 ironelf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 29-November 11

Re: Multiple definitions error when i try and compile

Posted 15 March 2012 - 11:20 AM

Sorry about the long wait i had to work. The rest of my code is:
Checking.h:
#ifndef CHECKING_H
#define CHECKING_H
#include <iostream>
#include "account.h"

class CheckingAccount : BankAccount {
public:
    bool status;
    
    CheckingAccount(double bal, double rate) : BankAccount(bal, rate)
    {
    if(bal > 25){
    status = true;
    }else{
    status = false;
    }
    };
    
    bool checkStatus();

    virtual void withdraw(double);

    virtual void deposit(double);

    virtual void monthlyProc(double);


};
#endif


Checking.cpp
#include "checking.h"
#include <iostream>

bool CheckingAccount::checkStatus() {
    if (balance >= 25) {
        status = true;
    } else {
        status = false;
    }
    std::cout << "Your account status is: " << status << std::endl;
}

void CheckingAccount::withdraw(double Amount) {
    checkStatus();
    if (status == true)
        BankAccount::withdraw(Amount);
    else
        std::cout << "Your balance is below $25, deposit more in order to"
            "withdraw.\n";
}

void CheckingAccount::deposit(double Amount) {
    if (status == true){
        BankAccount::deposit(Amount);
    std::cout << "Deposited: " << Amount << " into your account." << std::endl;
}else {
        if ((balance + Amount) >= 25) {
            status = true;
            BankAccount::deposit(Amount);
            std::cout << "Your account has been reactivated." << std::endl;
        } else {
            status = false;
            BankAccount::deposit(Amount);
            std::cout << "Your amount has been deposited but is still below "
                    "$25. Deposit more in order to withdraw." << std::endl;
        }

    }

}

void CheckingAccount::monthlyProc(double rate) {

    if (NumWithdraws >= 4) {
        MonthlyServiceCharge++;
        BankAccount::monthlyProc(rate);
        checkStatus();
        std::cout << "Completing the Checking Account transaction with "
                "added service charge." << std::endl;
    } else {
        BankAccount::monthlyProc(rate);
        std::cout << "Completing basic Checking Account transaction." 
                << std::endl;
        checkStatus();
    }
}



Uncompleted main as of right now. I want to solve these problems before i do anymore with it.
#include "checking.cpp"
#include "savings.cpp"
#include <iostream>

int main() {
    std::cout << "Welcome to Wide World Banking." << std::endl;
    std::cout << "Please create a Checking Account: " << std::endl;
    std::cout << "Enter the beginning balance:" << std::endl;
    double x = 1;
    while(x <= 0)
    {
    std::cin >> x;
    std::cout << "Please enter a positive balance." << std::endl;
    }
    std::cout << "Now enter your annual interest rate: " << std::endl;
    double y = 0;
    std::cin >> y;
    std::cout << "Creating your checking account now." << std::endl;
    CheckingAccount MyAcc1(x, y);
    std::cout << "Now would you like to withdraw or deposit? Enter 1 for "
            "withdraw and 2 for deposit: ";
    int choice = 0;
    int z = 0;
    bool check = false;
    while (check == false){
    std::cin >> choice;
    switch (choice){
        case 1 : check = true;
        z = 1;
        break;
        case 2 : check = true;
        z = 2;
        break;
        default : std::cout << "Enter a valid option.\n";
        check = false;
        break;
    }}
    if (z == 1) {
        std::cout << "Enter amount you would like to withdraw: ";
        double out;
        std::cin >> out;
        MyAcc1.withdraw(out);
    }else{
        std::cout << "Enter amount you would like to deposit: ";
        double in;
        std::cin >> in;
        MyAcc1.deposit(in);
    }
}


Also i was not asking for any code, i just want to understand what i am doing wrong here. The functions are virtual and i thought that when i want to use them i just need to have them redefined? Thank you for the help so far.
Was This Post Helpful? 0
  • +
  • -

#11 ironelf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 29-November 11

Re: Multiple definitions error when i try and compile

Posted 15 March 2012 - 11:26 AM

Also the complete error message is:
build/Debug/Cygwin-Windows/_ext/1803660498/main.o: In function `_ZN14SavingsAccount8withdrawEd':
Projects/2/savings.cpp:15: multiple definition of `SavingsAccount::withdraw(double)'
build/Debug/Cygwin-Windows/_ext/1803660498/savings.o:/cygdrive/c/Users/Documents/Project 2/../../../Desktop/Projects/2/savings.cpp:15: first defined here
Was This Post Helpful? 0
  • +
  • -

#12 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 630
  • View blog
  • Posts: 2,109
  • Joined: 31-December 10

Re: Multiple definitions error when i try and compile

Posted 15 March 2012 - 11:48 AM

The first two lines of your main() function are:
#include "checking.cpp"
#include "savings.cpp"



You want to include the header files not the implementation files, change it to:
#include "checking.h"
#include "savings.h"


And your errors should go away.

*NOTE*: These were linker errors not compilers errors, just so you know.

This post has been edited by vividexstance: 15 March 2012 - 11:49 AM

Was This Post Helpful? 1
  • +
  • -

#13 ironelf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 29-November 11

Re: Multiple definitions error when i try and compile

Posted 15 March 2012 - 12:04 PM

Thank you so much. What do you mean by linker errors though?
Was This Post Helpful? 0
  • +
  • -

#14 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 630
  • View blog
  • Posts: 2,109
  • Joined: 31-December 10

Re: Multiple definitions error when i try and compile

Posted 15 March 2012 - 12:22 PM

The linker knows where in memory each part of the code (i.e. function prototypes/definitions, variables, etc..) are located whereas the compiler does not. So the linker actually puts(links) all the different object files(including any standard libraries used) together to make the final executable program.
Was This Post Helpful? 1
  • +
  • -

#15 raspinudo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 60
  • View blog
  • Posts: 232
  • Joined: 19-September 11

Re: Multiple definitions error when i try and compile

Posted 15 March 2012 - 01:56 PM

Most people don't see what the linker does as you can do it all in one command:
g++ test.cpp -o test

This is also performing the linking procedure which connects all of the object files into one binary executable.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1