Making Projects with Dev - C++

Hello, i've had a question to ask anyone willing to help me.

I've tried to work on some random project, and when i compiled it, the compiler sent an error message saying
ERROR : Multiple use of main() function

and dosn't compile.

what have I done wrong?
Last edited on
You have the function main more than once in your program. It is used as entry point so you can only one function callen 'main'
but what if my project has multiple .cpp files in it?

like two different programs that when started with the
1
2
3
4
5
6
7
8
#include<iostream>
using namespace std;

int main()
{
               // Code goes here.
return 0;
}
Last edited on
1 project = 1 program

If you want a second program, create a second project.

Multiple cpp files can be used to spread the code for one program across several files, but it's still only one program.
Ok, so if thats the case, if your spreading code across .cpp files, don't they need a main function too?
or is it an extention of your' program? as in leaving off in your .cpp and continuing in the same spot in another file?

like this :

.cpp file #1
1
2
3
4
5
6
#include<iostream>
using namespace std;
int main()
{
int num;
cout << "Input a number" << endl;

.cpp file #2
1
2
3
4
5
6
cin >> num;
cout << "The number you have chosen is " << num << endl;
// Add more code here.
cin.get();
return 0;
}

No.

main() is the entry point -- it's where your program starts. Your program can't start in more than one place, so you can't have more than one main() in your program.

Multiple source files keep the code organized by having code that does a specific job in one file, and keeping unrelated code in other files. It also speeds up compile time because you don't have to recompile the entire program when you change one source file.

They're not used as your example. Instead, you put different function bodies in .cpp files.

1
2
3
4
5
6
7
8
9
10
11
// main.cpp

#include <iostream>
#include "header.h"  // your header file

int main()
{
  MyFunction();  // calls a function in another cpp file
  std::cin.get();
  return 0;
}

1
2
3
// header.h

void MyFunction();

1
2
3
4
5
6
7
8
9
// another.cpp

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

void MyFunction()
{
  std::cout << "this code is in another cpp file!";
}



In C++, usually a class has it's own h file for it's class definition, and its own cpp file for its member functions.
Last edited on
So what your saying is, other .cpp files in a project are used to contain functions that you wouldn't want hanging around in a mess in a single file?
Yes, Disch also wrote an article on this: http://www.cplusplus.com/forum/articles/10627/
You can find many info in it
Topic archived. No new replies allowed.