Eclipse C++ multiple main error only when using multiple headers - c++

I'm trying to learn how to utilize header files in C++ projects, so I made .cpp files containing simple functions to make sure I'm doing all the declaring and including correctly.
Everything worked fine when I only had one set of .cpp and .h files, but when I try to add more I get errors.
To start with, in my project I had:
helloworld.cpp
#include "helloworld.h"
#include <iostream>
#include <cstdio>
using namespace std;
int HelloWorld() {
puts("Hello, World!");
cout << "Hello, World!" << endl;
return 0;
}
helloworld.h
#ifndef HELLOWORLD_H_INCLUDED
#define HELLOWORLD_H_INCLUDED
int HelloWorld();
#endif /* HELLOWORLD_H_INCLUDED */
main.cpp
#include "helloworld.h"
#include <iostream>
using namespace std;
int main(){
HelloWorld();
return 0;
}
Which built with no errors and ran correctly.
Next I tried adding a second .cpp and .h file, which created building errors.
pointers.cpp
#include "pointers.h"
#include <iostream>
using namespace std;
int Pointers() {
int x = 1;
int *ptr_a = &x;
cout << *ptr_a << endl;
return 0;
}
pointers.h
#ifndef POINTERS_H_INCLUDED
#ifndef POINTERS_H_INCLUDED
int Pointers();
#endif /* POINTERS_H_INCLUDED */
and modified main.cpp:
#include "helloworld.h"
#include "pointers.h"
#include <iostream>
using namespace std;
int main(){
HelloWorld();
Pointers();
return 0;
}
Now when I try to build, I get an error saying there are multiple definitions of main -- one in main.cpp, and the other in pointers.cpp.
Even more oddly, if I make a new project and do the exact same thing but reverse the order in which I create the .cpp and .h files (i.e. pointers first then helloworld), it builds and runs correctly with just the pointers files but runs into the same error when adding helloworld files, saying that the multiple exceptions of main are in main.cpp and helloworld.cpp.
I figure it must have something to do with Eclipse itself, but I don't know what the exact issue is.
Does anyone know what might be going on?

Related

Still can't use a function defined in a separate .cpp file

I've been searching through questions on this for hours, and it just isn't working for some reason, so sorry if there's already an answer and I just didn't understand it.
I'm doing a course on C++, and I've gotten to classes. As practice, he started by just calling a simple function that was defined in a separate .cpp file to show how a class would work later on. See code below:
//main.cpp
#include <iostream>
#include "Cat.h"
using namespace std;
int main() {
speak();
return 0;
}
============================
//Cat.cpp
#include <iostream>
#include "Cat.h"
using namespace std;
void speak() {
cout << "Meow" << endl;
}
================================
//Cat.h
#ifndef CAT_H_
#define CAT_H_
void speak();
#endif //CAT_H_
I've triple checked everything I could think of. They're in the same directory and I'm cross-referencing everything exactly as he did. I expect it to output "Meow" in the console, but if I call the speak() function in main.cpp, I still just get an empty function, despite it being defined in class.cpp. I don't know what I'm missing, and it's quite frustrating. Is is because of my compiler? I'm using Visual Studio IDE.
Edits: Used the wrong commented name
I'm using the built-in Visual Studio IDE's "Build" and "Compile" options. I'm not running the compilation using Linux commands or anything else.
#273K Here is the screenshot of my 3 VS files:
https://i.stack.imgur.com/9tbw8.png
The problem is that you are including Cat.h but named your header class.h instead of Cat.h.
To solve this rename the files from class.h and class.cpp to Cat.h and Cat.cpp respectively, as shown below
Cat.h
#ifndef CAT_H_
#define CAT_H_
void speak();
#endif //CAT_H_
Cat.cpp
#include <iostream>
#include "Cat.h"
using namespace std;
void speak() {
cout << "Meow" << endl;
}
main.cpp
#include <iostream>
#include "Cat.h"
using namespace std;
int main() {
speak();
return 0;
}
Working demo
Thanks to #273K for the solution:
In the screenshot, the wrong project was "selected" in the solution explorer, so it was running the wrong project code.
I fixed it by right-clicking the project I wanted and clicking "Set as StartUp Project"
Before: https://i.stack.imgur.com/na0nF.png
After: https://i.stack.imgur.com/EJMLq.png

How to link multiple .cpp files in Code::Blocks for a single project?

While following the book C++ For Dummies, I have three files in my CodeBlocks project, main.cpp, Pen.h, and Pen.cpp. They look like this:
main.cpp:
#include <iostream>
#include "Pen.h"
//#include "Pen.cpp"
using namespace std;
int main()
{
Pen MyPen = Pen();
MyPen.test();
}
Pen.h:
#ifndef PEN_H_INCLUDED
#define PEN_H_INCLUDED
//#include "Pen.cpp" // Uncommenting this gives a different error
using namespace std;
class Pen
{
public:
// attributes omitted
// PROTOTYPES:
// other functions omitted
void test();
};
#endif // PEN_H_INCLUDED
Pen.cpp:
#include <iostream>
#include "Pen.h"
using namespace std;
//other function definitions omitted
void Pen::test()
{
cout << "Test successful." << endl;
}
When I run the code as listed above, I get an "undefined reference to `Pen::test()'" error. To fix this, I changed the #include statements at the top of main.cpp to:
#include <iostream>
//#include "Pen.h"
#include "Pen.cpp"
This works as intended and correctly prints out "Test successful."
My question is this: what in the world is the point of putting a function prototype in a header file if I have to import the .cpp file later on anyways?
EDIT: It turns out this was a problem with not knowing how to use Code::Blocks rather than with the C++ language.
Assuming you're using gcc, you can compile and link in one step by supplying multiple .cpp files via the command line.
g++ Pen.cpp main.cpp
clang should be similar.
clang++ Pen.cpp main.cpp
An #include should never reference a .cpp file. At all. There's no good reason to do it. Include your headers and then supply the names of all .cpp files when you compile. If your project gets big and you have too many .cpp files to reasonably list, then it's time to break out a makefile or similar.
In the main.cpp include the header file:
#include "Pen.h"
The Pen.h file it's ok.
You need to add the Pen.cpp file to the project tree.
Go to Project -> Add files... and add Pen.cpp

Why does my program only work with the debug build?

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";
}

Need assistance with code where it claims code not declared in this scope (C++)

I am practicing using multiple files for C++ in Code::Blocks. I have three files, two source files named main.cpp and Cat.cpp, and a header file named Cat.h. Though I declare a function designed to output text in Cat.h, the implementation in the main function returns the error "'speak' was not declared in this scope."
I tried researching the error, but that was tricky because it's such a general error that can occur for a wide variety of reasons. I tried carefully checking for syntax errors or improper #include statements in my code, but I can't find anything.
This is in my main.cpp file:
#include <iostream>
#include "Cat.cpp"
#include "Cat.h"
using namespace std;
int main()
{
speak();
return 0;
}
this is my Cat.h file:
#ifndef CAT_H_INCLUDED
#define CAT_H_INCLUDED
void speak();
#endif
and this is my Cat.cpp file:
#include <iostream>
#include "Cat.h"
using namespace std;
void speak(){
cout << "Meow!!" << endl;
}
I am expecting speak() to run, but the error says it is not declared in this scope.

Why are my functions undefined when I declared the type already?

Hi I was just trying to learn separate Classes in C++. I don't know why my code is not working.
So here is the main file code
#include <iostream>
#include "Number.h"
using namespace std;
int main()
{
Number key;
key.setNumber(200);
cout<<key.getNumber();
return 0;
}
Here is the Class cpp functions file code
#include "Number.h"
#include <iostream>
using namespace std;
void Number::setNumber(int transfernumber)
{
privatenumber = transfernumber;
}
int Number::getNumber()
{
return privatenumber;
}
And here is the header file
#ifndef NUMBER_H
#define NUMBER_H
class Number
{
public:
Number();
void setNumber(int transfernumber);
int getNumber();
private:
int privatenumber;
};
#endif // NUMBER_H
Thanks
In your cpp file you need to define the default constructor for the Number class. For example:
Number::Number() : privatenumber(0) {}
I have test your example. The error happened for the main.cpp cannot found the number.cpp. You have three ways to solve it:
write your main() to the number.cpp, not a solo file.
complie the main.cpp with the linux command gcc or write a Makefile, instead of using codeblocks.
If you want to use the codeblocks for compiling, you should create a project, and then add your three files to the project. Now compile the main.cpp.
Use the three ways above, I think you will compile successfully.
BTW, you should add the Number::Number() 's implementation.