| | |
Class Errors
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Feb 2007
Posts: 46
Reputation:
Solved Threads: 0
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.
THIS IS THE CPP FILE
Here are the classes below.
C++ Syntax (Toggle Plain Text)
// File: counter.h #ifndef COUNTER_H #define COUNTER_H class counter { public: counter(); counter(int); void increment(); void decrement(); void setcount(int); void setmaxv(int); int getcount() const; int getmaxv() const; private: int count; int maxv; }; #endif // COUNTER_H
THIS IS THE CPP FILE
C++ Syntax (Toggle Plain Text)
#include "counter.h" #include <iostream> using namespace std; int main() { counter c1; counter c2(10); c1.setcount(50); c1.decrement(); c1.decrement(); c1.increment(); cout << "Finav c1" << " " << c1.getcount() << endl; c2.increment(); c2.increment(); c2.decrement(); cout << "Final c2 " << c2.getcount() << endl; return 0; }
Last edited by ~s.o.s~; Mar 2nd, 2007 at 11:49 pm. Reason: Removed the weird coloring and fixed code tags.
•
•
Join Date: Jul 2005
Posts: 1,678
Reputation:
Solved Threads: 264
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
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.
![]() |
Similar Threads
- C++ .NET Character class errors (C++)
- Basic help with Java class (Java)
- undefined class errors (C2079) (C++)
- unresolved external error upgrading 6.0-7.1 (C++)
Other Threads in the C++ Forum
- Previous Thread: Program to display System date and Time in VC++ 6.0
- Next Thread: trying to make a "Do While" loop; loop
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node output parameter pointer problem program programming project proxy python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






