unix compiling issues with multiple files

Hi,

I am having issues getting my files to compile in unix. I am following along with the online example given. the online instructors is compiling(in vm). mine is not compiling in unix.

here are my different files

account.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  #pragma once
 
  class NotPositiveDeposit
  {
  };
 
  class InvalidWithdrawal
  {
  };
 
  class Account
  {
          private:
             double balance;
          public:
             Account(void);
             ~Account(void);
             Account(double initialDeposit);
             double getBalance();
             double deposit(double amount);
             double withdrawal(double amount);
  };


account.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 #include "account.h"
 
  Account::Account(void):balance(0)
  {
  }
  Account::~Account(void)
  {
  }
  Account::Account(double initialDeposit): balance(initialDeposit)
  {
  }
  double Account::getBalance()
  {
          return balance;
  }
  double Account::deposit(double amount)throw(NotPositiveDeposit)
  {
     if(amount>0)
        balance += amount;
     else
        //return -1;
        throw NotPositiveDeposit();
     return balance;
  }
  double Account::withdraw(double amount)throw(InvalidWithdrawal)
  {
          if((amount>balance)|| (amount <0))
            // return -1;
            throw InvalidWithdrawal();
          else
             balance -= amount;
          return balance;
  }

here are the errors:
account.cpp:16: error: declaration of âdouble Account::deposit(double) throw (NotPositiveDeposit)â throws different exceptions
account.h:20: error: from previous declaration âdouble Account::deposit(double)â
account.cpp:25: error: no âdouble Account::withdraw(double)â member function declared in class âAccountâ

Can anyone help me figure these errors out?

Thanks!
Last edited on
The problem is that int foo() is not the same as int foo() throw(bar).
http://www.gotw.ca/publications/mill22.htm

The compiler is duly complaining that the prototype in the header (.h) does not match the prototype in the source (.cpp) file.

Best answer -- get rid of all throw specifications. You don't need them.

Hope this helps.
so instead of using :

double Account::deposit(double amount)throw(NotPositiveDeposit)

and

double Account::withdraw(double amount)throw(InvalidWithdrawal)

i use:

double Account::deposit(double amount)

and

double Account::withdraw(double amount)

and then I will still be able to use the throw code within the function? as it is written:

1
2
3
4
5
6
if(amount>0)
        balance += amount;
     else
        //return -1;
        throw NotPositiveDeposit();
     return balance;


and
1
2
3
4
5
6
if((amount>balance)|| (amount <0))
            // return -1;
            throw InvalidWithdrawal();
          else
             balance -= amount;
          return balance;


and finally out of curiosity....Why would the instructor code it this way (have it compile for him), but then come to find out it is not even necessary?

Thanks!
okay I deleted the throw code and the two lines of code now read:

double Account::deposit(double amount)

and

double Account::withdraw(double amount)

but I am still getting an error:
account.cpp:25: error: no âdouble Account::withdraw(double)â member function declared in class âAccountâ
typo.......
fixed the typo and now this is what is shown when I compile:

157% g++ account.cpp -o account
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status

when I compile the bankaccount.cpp code(nothing was changed from the above version. these are the errors listed:

In function `main':
bankaccount.cpp:(.text+0x1b): undefined reference to `Account::Account(double)'
bankaccount.cpp:(.text+0x4b): undefined reference to `Account::deposit(double)'
bankaccount.cpp:(.text+0x11c): undefined reference to `Account::~Account()'
bankaccount.cpp:(.text+0x138): undefined reference to `Account::~Account()'
collect2: ld returned 1 exit status


any ideas?

Thanks!
Last edited on
The main program must have a function int main(...).

Since account.cpp is not the main program, it does not have the main function.

Hence, you must tell the compiler not to try linking your source code into an executable, since it has no main:

    g++ -c account.cpp

It will produce account.o in the cwd. When you have compiled everything this way, you can link it all with the compiler:

    g++ project2.o account.o -o project2


Also, it is likely that your instructor's code compiles and links just fine. You are just not doing it like he did.

Hope this helps.
sorry. I thought I included my int main file above.....

Here is the int main file

however the errors still apply. So I really dont know what is the issue......

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 #include<iostream>
 #include"account.h"
 
  using namespace std;
 
  int main()
  {
    Account a(300);
 
    try
    {
          cout << "Deposit 150"<< endl;
          cout<< "The new balance is: "<< a.deposit(150)<<endl;
    }
    catch(NotPositiveDeposit)
    {
       cout<< " The amount attempting to deposit must be positive"<< endl;
    }
    catch(InvalidWithdrawal)
    {
       cout<<"This is an invalid withdrawal amount."<<endl;
    }
    return 0;
  }
could it be how I am using the compiling commands in unix

for account.cpp

g++ account.cpp -o account

and for bankaccount:

g++ bankaccount.cpp -o bankaccount
Topic archived. No new replies allowed.