0

I'm beginner in C++ and trying to run some starter code. I have the following files,

myTest.h
////////

#ifndef __myTest_h__
#define __myTest_h__

#include <string>

using std::string;

class myTest{

public:    
    int main(int, char const**);
}; 

#endif // __myArray_h__ 



myTest.cpp
//////////


#include<iostream>
#include<string>
#include "myTest.h"
using namespace std;



int myTest::main(int argc, char const *argv[])
{

    std::cout<< "Hello World/n";
    return 0;
}

When I try to run from the terminal in Mac OS using the command g++ myTest.cpp -o myTest.out, I get the following error in the terminal,

Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

How to correct the code to getting started with C++? I can provide more info using the command -v, please, let me know if that required.

Arefe
  • 11,321
  • 18
  • 114
  • 168

4 Answers4

13

Unlike Java or C# you can't have your main function in a class, it must be a global non-member function.


You can make a very simple main function which then calls your member function main function:

int main(int argc, char* argv[])
{
    myTest myTestObject;
    return myTestObject.main(argc, argv);
}

Note that I need to create an instance of the myTest class, this is because the myTest::main function is not made static. If you make it static, like

class myTest
{
public:
    static int main(int, char *[]);
    ...
};

then your non-member main function could look like this:

int main(int argc, char* argv[])
{
    return myTest::main(argc, argv);
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Thanks, it working now. Why do they keep the `main` out of the class in C++? – Arefe Jun 29 '16 at 13:19
  • 3
    @Arefe Backwards compatibility is one reason. Another is that you could have multiple classes and namespaces each having a `main` function, which should then be used? – Some programmer dude Jun 29 '16 at 13:29
  • So, in C++ the starting point will be the `main` method stays out of a class and that will be the one and only starting point even when we will have multiple classes, say, for developing a game with several characters ? – Arefe Jun 29 '16 at 14:40
  • @Arefe Yes, the global, "free", stand-alone non-member function `main` is the only entry point. If you have e.g. a game with multiple characters then you have multiple instance of the single `Character` class, for example, and handle events related to each specific character, probably in some kind of loop. – Some programmer dude Jun 29 '16 at 15:10
3

Your entry point main function cannot be inside a class. It has to be a normal function.

If you want to place your logic inside a class, have your main function allocate an instance of it and then call a function in it.

Matti Virkkunen
  • 63,558
  • 9
  • 127
  • 159
2

Every C++ program needs a main() function, which is what the C++ startup code calls immediately after loading and initializing your program. So, you'll need to link your program with a .cpp file containing a main() function,

int main(int argc, char const *argv[])
{

}
msc
  • 33,420
  • 29
  • 119
  • 214
1

You need an entry point for your program. You have to add int main() {} because that's your entry point and then call your member function in your class. It needs to be outside of a class or struct.

Shiro
  • 2,610
  • 2
  • 20
  • 36