Compile Error - Multi-file - Classes

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2007
Posts: 66
Reputation: superjacent is an unknown quantity at this point 
Solved Threads: 3
superjacent's Avatar
superjacent superjacent is offline Offline
Junior Poster in Training

Compile Error - Multi-file - Classes

 
0
  #1
Jan 27th, 2008
Hope someone can help. I'm self-studying from C++ Primer Plus, 5th Edition. I'm having trouble working out why the following two files will not compile. I'm using Dev-C++, under Windows Vista.
  1. /*
  2.   Name: bankaccount.h
  3.   Copyright:
  4.   Author: Steven Taylor
  5.   Date: 28/01/08 14:04
  6.   Description: C++ Primer Plus, Chapter 10, Programming Exercise. #1. Page 496
  7. */
  8. #ifndef BANKACCOUNT_H_
  9. #define BANKACCOUNT_H_
  10. class BankAccount
  11. {
  12. private:
  13. char name[40];
  14. char acctnum[25];
  15. double balance;
  16. public:
  17. BankAccount(const char *client, const char *num, double bal = 0.0);
  18. void deposit(double cash);
  19. void withdraw(double cash);
  20. void show() const;
  21. }
  22. #endif
And this file:
  1. /*
  2.   Name: bankaccount.cpp
  3.   Copyright:
  4.   Author: Steven Taylor
  5.   Date: 28/01/08 14:03
  6.   Description: BankAccount class methods. C++ Primer Plus, Chap 10, page 496
  7. */
  8. #include <iostream>
  9. #include <cstring>
  10. #include "bankaccount.h"
  11.  
  12. BankAccount::BankAccount(const char *client, const char *num, double bal)
  13. { // error reported here - cannot
  14. strncpy(name,client,39);
  15. name[39] = '\0';
  16. strncpy(acctnum, num,24);
  17. acctnum[25] = '\0';
  18. balance = bal;
  19. }
When trying to compile the above I get an error reported in the second file at the first brace indicating 13 D:\Users\Steve\Documents\LearningCpp\LearningCppPrimer\Chapter-10\PE-01\bankaccount.cpp new types may not be defined in a return type Very next error message is 13 return type specification for constructor invalid
Both files are in the same directory. Can somebody point me in the right direction.
Last edited by superjacent; Jan 27th, 2008 at 11:36 pm. Reason: Added extra error message
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 66
Reputation: superjacent is an unknown quantity at this point 
Solved Threads: 3
superjacent's Avatar
superjacent superjacent is offline Offline
Junior Poster in Training

Re: Compile Error - Multi-file - Classes

 
0
  #2
Jan 27th, 2008
Further, when I combine these two files into one file, for ease of compilation, I get the same compiler error message. So I suppose it's not related to a multiple file thing.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,639
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1497
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Compile Error - Multi-file - Classes

 
0
  #3
Jan 27th, 2008
I think the problem is line 21 of the header file -- you have to have a semicolon at the end of the class. Put a semicolon after that closing brace.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 66
Reputation: superjacent is an unknown quantity at this point 
Solved Threads: 3
superjacent's Avatar
superjacent superjacent is offline Offline
Junior Poster in Training

Re: Compile Error - Multi-file - Classes

 
0
  #4
Jan 28th, 2008
Originally Posted by Ancient Dragon View Post
I think the problem is line 21 of the header file -- you have to have a semicolon at the end of the class. Put a semicolon after that closing brace.
Thank you, how did I not see that.

I'm now getting another compiler error indicating this [Linker error] undefined reference to `WinMain@16' .

From another editor, Programmers Notepad, where all the above is just a single file I get the same error message, though with a little more detail, I assuming it's the same error
> "C:\Dev-Cpp\bin\g++.exe" singlefile.cpp -o singlefile.exe and next line is
C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../libmingw32.a(main.o)(.text+0x106):main.c: undefined reference to `WinMain@16'
collect2: ld returned 1 exit status
I'm at a loss interpreting this.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,639
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1497
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Compile Error - Multi-file - Classes

 
0
  #5
Jan 28th, 2008
You apparently created a Windows application, not a Console application. The easiest correction is to start a new console project then copy what you have done into it.

And your program must have a main() function.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 66
Reputation: superjacent is an unknown quantity at this point 
Solved Threads: 3
superjacent's Avatar
superjacent superjacent is offline Offline
Junior Poster in Training

Re: Compile Error - Multi-file - Classes

 
0
  #6
Jan 28th, 2008
Originally Posted by Ancient Dragon View Post
You apparently created a Windows application, not a Console application. The easiest correction is to start a new console project then copy what you have done into it.

And your program must have a main() function.
Thank you once again, I quickly added a blank int main() function and it compiled without error. I shouldn't forget this gotcha in future.

ps. I probably should have created a new thread for my second question as the first was well and truly solved. Anyway, I'll mark this thread as solved.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum


Views: 2556 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC