So far, from what I've learned from the c++ book I'm reading, the int main() function in a c++ class is kind of like the public static void main(String args[]) method in java. (I'm a java person and I'm new to c++).

However, I found out that within my source folder, I can't have two c++ classes with int main functions. Basically I wrote a class for printing out hello world and then I wrote another class for adding two numbers and printing the result, all in the same source folder, and I'm getting an error that says "multiple definitions of main".

Why is this happening?

In java it's possible to have a main method in each class. Why is it different for C++ and what's the alternative?

Edited 6 Years Ago by glenak: n/a

Yes only have one main function.

The main function is a placeholder for the start of execution, you can change the name if you inform the linker of the new name but generally you don't.

So what do I do if i want to create multiple class files that can be executed to perform different functions?

CreateThread()

Main is just the primary thread.

Edit: There are many implementations of createthread, and variations (such as createthreadex), an example implementation of createthread which might make your life easier is boost::thread

Edited 6 Years Ago by Unimportant: n/a

Sounds like mutilthreaded programming, this CreateThread() function. And I'm not even on that level yet. So there isn't a function or method or something where I can put in codes in each class and run the class to produce results?

For example, in a source folder I have Class Print and Class Add, and I want to be able to run Print which will print out, "Hello there," and run Add which will print out the addition between two declared integer variables within the class. In java, all I had to do was put in a public static void main method in each class.

Surely, there must be something similar. I mean, this is c++, right? Everyone says it's - well - miles ahead of every language out there.

Edited 6 Years Ago by glenak: n/a

So what do I do if i want to create multiple class files that can be executed to perform different functions?

Here's a very simple example.

testit.cpp(the main program)

#include <iostream>
#include "test2.h"

int main()
{
  myc me(123);
  
  std::cout << me.getitsvalue() << std::endl;
  
  me.setitsvalue(456);
  
  std::cout << me.getitsvalue() << std::endl;
  return 0;
}

test2.cpp(additional file with a class)

class myc
{
public:
  myc();
  myc(unsigned long val);
  ~myc();
  
  unsigned long getitsvalue() const;
  void setitsvalue(unsigned long val);
private:
  unsigned long itsvalue;
};

myc::myc()
:itsvalue(0)
{}

myc::myc(unsigned long val)
:itsvalue(val)
{}

myc::~myc()
{}

unsigned long myc::getitsvalue() const
{
  return itsvalue;
}

void myc::setitsvalue(unsigned long val)
{
  itsvalue = val;
}

test2.h(header file)

class myc
{
public:
  myc();
  myc(unsigned long val);
  ~myc();
  
  unsigned long getitsvalue() const;
  void setitsvalue(unsigned long val);
private:
  unsigned long itsvalue;
};

Now the compiling...I use the GCC compiler.

First create the object files.

g++ -c testit.cpp
g++ -c test2.cpp

Now we create the main executable testit

g++ testit.o test2.o -o testit

Edited 6 Years Ago by gerard4143: n/a

This article has been dead for over six months. Start a new discussion instead.