I just started teaching myself the concept of classes via the internet and I do not understand why my simple program isn't working. I know its a simple question probably with an obvious answer but I need some help to learn the ins and outs of C++.
#include <iostream>
using namespace std;
class classTest {
public:
// The Error is that it is an "unidentified reference to main."
int main()
{
cout << "Class Test";
return 0;
The main function needs to be outside of the class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
usingnamespace std;
class classTest {
public:
void test() {
cout << "class test" << endl;
}
};
int main() {
classTest ct; //declare an instance of the class
ct.test(); //call classTest's member function
return 0;
}
The declaration of the class should go in the file classTest.h, the definitions of the functions in classTest.cpp, and main() appears only once in a .cpp file that can have any name you like - normally the application name.
I suggest you read the tutorial & reference info at the top left of this page, & probably get a good text book.