I am getting the following error: Multiple definition of `main'
I have created a new project, there are two c++ files in it:
File 1
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello World";
//fflush(stdin);
//getchar();
return 0;
}
File 2
#include <iostream>
using namespace std;
int main()
{
cout<<"Demo Program";
return 0;
}
When I press Build project and Run, I get error. How do I run these files?
You cannot have two main functions in the same project. Put them in separate projects or rename one of the functions and call it from the other main function.
You can never have more than one main() function in your project since it is the entrypoint, no matter what the parameter list is like.
You can however have multiple declarations of other functions as long as the parameter list is different (function overloading).
File 1
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello World";
otherFunction();
return 0;
}
File 2
#include <iostream>
using namespace std;
void otherFunction()
{
cout<<"Demo Program";
}
Dont forget the appropiate #includes.
You can't have two main functions. In fact you can't have any two functions having the same signature through out your project (not your files).
And as Mr.TAMER said main is a special case, you can't even have two functions called main.
Decide which file you want to be as entry point of your project.
In other file, change the method name to some other name. you can call it from the file you chose at step 1.
main is entry point of your program and you can't have more than one entry point.
For more clear explanation see this: Two 'main' functions in C/C++
You can not use the same function signature in a same project,becouse the compiler start execution from the main(). If you define multiple times of main() then it produce an error.
file1.c
#include <iostream>
#include <file2.h>
using namespace std;
int main()
{
cout<<"Hello World";
//fflush(stdin);
//getchar();
return 0;
}
And in file2.h,you can define the function of file2.c(first rename the main() of the file2)
Related
So I have 3 files in a single folder, my main file (Q1.cpp), my header file (pa2Functions.h), and my implementation file, (pa2Functions.cpp). When I call my function, I don't get any output and I am extremely confused as to why. I have included the contents of all 3 files below.
pa2Functions.h
#include <iostream>
using namespace std;
void initialize();
pa2Functions.cpp
#include <iostream>
#include "pa2Functions.h"
using namespace std;
void initialize(){
cout << "hello" << endl;}
Q1.cpp
#include <iostream>
#include "pa2Functions.h"
using namespace std;
int main(){
void initialize();
}
I compile with
g++ Q1.cpp pa2Functions.cpp -o Output
But when I run the output I just don't get anything. Any help would be greatly appreciated.
In main, remove the void in front of initialize. You're declaring the function again, not calling it.
int main(){
initialize();
}
So I was trying to access a method that is defined in another class and has the prototype in the header. I'm pretty positive I defined it but it keeps popping up undefined reference to SafeCracker.
Main.cpp
#include <iostream>
#include "mystuff.h"
using namespace std;
void BigDog(int KibblesCount);
int main()
{
cout << SafeCracker(1);
return 0;
}
mystuff.cpp
#include <iostream>
using namespace std;
string SafeCracker(int SafeID)
{
return "123456";
}
mystuff.h
using namespace std;
#ifndef MYSTUFF_H_INCLUDED
#define MYSTUFF_H_INCLUDED
string SafeCracker(int SafeID);
#endif // MYSTUFF_H_INCLUDED
Here it tells you that you have an undefined reference, so you don't really have a problem with the prototype.
Had you forgotten to include the header file that contains the prototype you would have gotten something like
main.cpp: In function ‘int main()’:
main.cpp:8:13: error: ‘SafeCracker’ was not declared in this scope
cout << SafeCracker(1);
Your undefined reference is a linker error. The most likely cause would be that you did not use mystuff.cpp when compiling
If you're compiling from the command line, you should give both files as parameters.
If you're using an IDE that calls the compiler, make sure that the file is part of the project.
For example in Code::Blocks right-click on the file name and go "add to project" (If I remember correctly)
It is also possible that you made a typo in the function declaration in mystuff.cpp (that doesn't seem to be the case here though)
Now there is one important thing about your code you should take note of:
It is very bad practice to put a using namespace in a header file.
using namespace std; in a .cpp source file is mostly up to you, and that using statement will only apply to that particular file.
But if you put it in a header file that is meant to be included through #include , the using there will be forced upon any code that includes it.
Here is an example:
main.cpp
#include <iostream>
// including mystuff.h to use that awesome SafeCracker()
#include "mystuff.h"
// I need to use an std::map (basically an associative array)
#include <map>
// the map of my game
class map
{
int tiles[10][10];
};
int main()
{
// The std map I need to use
std::map<int, int> mymappedcontainer;
// The map of my game I need to use
map mytiles;
// The reason why I need to include mystuff.h
cout << SafeCracker(1);
return 0;
}
Normally, my class map should not be a problem since the map I included from the standard library is inside the namespace std, so to use it you would need to go std::map.
The problem here is that, since mystuff.h has using namespace std; in it, the symbol map is already used, and that creates a conflict.
You do not now who will use your header files, or if you will use them again a long time from now, and maybe then you will want to use name that is already used in the std namespace.
I advise you to use std:: before things taken from the standard libraries instead (std::string instead of just string for example)
PS: In C++, "class" refers to a class data structure, and the functions you made here are not part of any class. You should say "defined in another file" or "defined in another translation unit" instead
I have a project that has the main method accessing another method from another source file, BigDog(int). I'm pretty sure the code is right but CodeBlocks seems to not be able to detect the definition of the method unless I build the other file using debug build in CodeBlocks. In Release, I get the following error when building:
Error: undefined reference to 'BigDog(int)'
Why is that so?
main.cpp
#include <iostream>
using namespace std;
void BigDog(int KibblesCount);
int main()
{
BigDog(3);
return 0;
}
mystuff.cpp
#include <iostream>
using namespace std;
void BigDog(int KibblesCount)
{
cout << KibblesCount;
}
If you're adding a new file in codeblocks, make sure to check the checkmarks in the dialog to add it to both the debug and the release build.
Also its better practice to move your declarations to a header file and include that where needed, like this:
main.cpp:
#include "mystuff.h"
int main()
{
BigDog(3);
return 0;
}
mystuff.h:
#pragma once
void BigDog(int KibblesCount);
mystuff.cpp:
#include "mystuff.h"
#include <iostream>
void BigDog(int KibblesCount)
{
// add a newline so the line gets printed immediately
std::cout << KibblesCount << "\n";
}
All I'm trying to do is create a separate class to hold my Hello World function (this is for a class), but I am getting an "identifier is undefined" compiler error. What is the issue?
Here is my main function (helloworld.cpp):
#include <iostream>
using namespace std;
int main() {
print_me();
system("pause");
return 0;
}
And here is the header class (helloworld.h) :
#include <iostream>
void print_me() {
std::cout << "Hello World\n";
}
You have not included helloworld.h in helloworld.cpp. The following code should work:
#include <iostream>
#include "helloworld.h"
using namespace std;
int main() {
print_me();
system("pause");
return 0;
}
One thing to remember is from your compiler's point of view, there is no connection between the two files unless you specify it. The fact that both files have the same name does not have any significance for compiler.
Side note 1: Consider using include guards in your header files. For simple projects, it may not be obviously necessary but for larger projects, not using them can lead to annoying ambiguous compilation errors.
Side note 2: Implementing function bodies in header files is generally discouraged.
I have two libraries included in my program which both have the same function name, but I need to be able to use both, but I also need C++ to know which one I'm referring to (in certain places I will only be referring to one or the other). The reason why I'm doing this is because I am making my own library and I want to have certain names for my functions, but they are conflicting with functions in someone else's library that I've included, and to make matters worse, some of my functions in my library actually USE the functions in the other persons library which has the same name.
My library is just a .h/.cpp file by the way. Also, when calling MY functions, I don't want any extra luggage such as myNameSpace::myFunc(). I just want to call it myFunc(). However, I don't mind calling the other persons function using a namespace (though I don't want to modify their library in case I break something). (I'm completely new to C++ btw)
HERES MY NEW (TEST - SO FAR) CODE : NOT WORKING W/ ERRORS:
error C2668: 'myFunc' : ambiguous call to overloaded function
main program.cpp
#include "otherslib.h"
#include "mylib.h"
#include <iostream>
using namespace myNamespace;
int main(){
std::cout << myFunc() << std::endl;
return 0;
}
mylib.h
#pragma once
namespace myNamespace{
int myFunc();
}
mylib.cpp
#include "mylib.h"
namespace myNamespace{
int myFunc(){
return 1;
}
}
otherslib.h
#pragma once
int myFunc();
otherslib.cpp
#include "otherslib.h"
int myFunc(){
return 0;
}
You should define your functions in a namespace, and use the namespace when calling them.
namespace myNamespace
{
int myFunc(etc) { ... }
}
int main() {
cout << myNamespace::myFunc();
}
To avoid having to specify your namespace all the time, you could do something like this:
namespace myNamespace
{
int myFunc(etc) { ... }
int main()
{
// Call your own myFunc:
myFunc();
// Call their myFunc:
::myFunc();
}
}