944,204 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 950
  • C++ RSS
Mar 2nd, 2007
0

Class Errors

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 12
Solved Threads: 0
Light Poster
torbecire is offline Offline
46 posts
since Feb 2007
Mar 2nd, 2007
0

Re: Class Errors

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Mar 2nd, 2007
0

Re: Class Errors

[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
Reputation Points: 12
Solved Threads: 0
Light Poster
torbecire is offline Offline
46 posts
since Feb 2007
Mar 2nd, 2007
0

Re: Class Errors

Yup. You include a.h in b.cpp and you're ready to go.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Mar 2nd, 2007
0

Re: Class Errors

[Torbecire]

It worked thanks man.
Reputation Points: 12
Solved Threads: 0
Light Poster
torbecire is offline Offline
46 posts
since Feb 2007
Mar 2nd, 2007
0

Re: Class Errors

You're welcome.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Program to display System date and Time in VC++ 6.0
Next Thread in C++ Forum Timeline: trying to make a "Do While" loop; loop





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC