Class Errors

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

Join Date: Feb 2007
Posts: 46
Reputation: torbecire is an unknown quantity at this point 
Solved Threads: 0
torbecire torbecire is offline Offline
Light Poster

Class Errors

 
0
  #1
Mar 2nd, 2007
I am trying to write a file with extension h and a cpp file. When i try to compile the cpp file it gives me an error that says. [Linker error] undefined reference to `counter::counter()' . Could anyone help me out please. I am using a Dev.c++ compiler

Here are the classes below.

  1. // File: counter.h
  2.  
  3. #ifndef COUNTER_H
  4. #define COUNTER_H
  5.  
  6. class counter
  7. {
  8. public:
  9. counter();
  10. counter(int);
  11.  
  12. void increment();
  13.  
  14. void decrement();
  15.  
  16. void setcount(int);
  17.  
  18. void setmaxv(int);
  19.  
  20. int getcount() const;
  21.  
  22. int getmaxv() const;
  23.  
  24. private:
  25.  
  26. int count;
  27. int maxv;
  28. };
  29. #endif // COUNTER_H

THIS IS THE CPP FILE
  1. #include "counter.h"
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. counter c1;
  8.  
  9. counter c2(10);
  10.  
  11. c1.setcount(50);
  12. c1.decrement();
  13. c1.decrement();
  14. c1.increment();
  15. cout << "Finav c1" << " " << c1.getcount() << endl;
  16.  
  17. c2.increment();
  18. c2.increment();
  19. c2.decrement();
  20. cout << "Final c2 " << c2.getcount() << endl;
  21.  
  22. return 0;
  23. }
Last edited by ~s.o.s~; Mar 2nd, 2007 at 11:49 pm. Reason: Removed the weird coloring and fixed code tags.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 264
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Class Errors

 
0
  #2
Mar 2nd, 2007
The cpp file used to define the methods in the h file shouldn't have main() in it. The driver program, which is also a cpp file, will have main in it. It would have been nice if the driver files were given a different extension than the files used to define class methods but they are what they are. Don't give the driver program and the header file the same name.

a.h
a.cpp //where methods in a.h are defined. May be blank if all methods inlined.
b.cpp //driver program where a.h is included and a.cpp linked by association with a.h
Last edited by Lerner; Mar 2nd, 2007 at 4:09 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 46
Reputation: torbecire is an unknown quantity at this point 
Solved Threads: 0
torbecire torbecire is offline Offline
Light Poster

Re: Class Errors

 
0
  #3
Mar 2nd, 2007
[Torbecire]

O.k lets see if i get this right.

a.h is the class file
a.cpp is the implementation file for a.h
b.cpp is the uses both of the above to carry out its task
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 264
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Class Errors

 
0
  #4
Mar 2nd, 2007
Yup. You include a.h in b.cpp and you're ready to go.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 46
Reputation: torbecire is an unknown quantity at this point 
Solved Threads: 0
torbecire torbecire is offline Offline
Light Poster

Re: Class Errors

 
0
  #5
Mar 2nd, 2007
[Torbecire]

It worked thanks man.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 264
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Class Errors

 
0
  #6
Mar 2nd, 2007
You're welcome.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC