can't use separate function files in eclipse - c++

I am trying to declare the functions in separate files. In the code given below, my main() is defined in main.cpp and the int addition(int x, int y) is defined
in an another file named function.cpp.
My code:
main.cpp
#include "function.cpp"
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
int a = 1;
int b = 15;
int sum = addition(a,b);
cout<<"\nSum = "<<sum<<"\n";
return 0;
}
fucntion.cpp
int addition(int x, int y)
{
int sum = x + y;
return sum;
}
But by using the above cod in Eclipse i am getting the following error. On the other hand, if i compile the code manually using make
through the linux terminal then, the same got works.
ERROR:
/home/eclipse_workspace/multiFiles/Debug/../funtion.cpp:9: multiple definition of `addition(int, int)'
./funtion.o:/home/eclipse_workspace/multiFiles/Debug/../funtion.cpp:9: first defined here
collect2: ld returned 1 exit status.

First of all it is not recommended to include .cpp files. You should create header (.h) with declarations, put implementations to .cpp, like now and wherever you need to use it just include.h . You should also read about avoiding multiple includes by adding #ifndef/#define/#endif.
Update:
#include works in pre compiling phase and more or less it means "paste here what you have in file ...". So it copies function from one file and pastes to main file then compiles it. After this it compiles also cpp file with your function - also ok. Now comes linking: because of previous steps and copy-paste it has two definitions (actually two symbols) which has same name - that is causing the error and that's why we have headers :)

First create a header file, for example Addition.h and declare the function name inside it. Then make a file Addition.cpp and write the addition function implementation and then include the Addition.h in your main.cpp file.
The concept of using a header file is that you can use it anywhere else and is not limited to your main.cpp program file.
So, in short
Addition.h
class Addition { public:
int addition(int a , int b); //function declaration
private: int result_; };
then in Addition.cpp
#include Addition.h
int Addition::addition(int x, int y) {
// function implementation
}
in main.cpp
#include <Addition.h>
int main()
{ int a=3, b=4, sum=0;
Addition objAdd; //creation of object for class Addition
sum = objAdd.addition(a,b);
}
Hope this helps in structuring your code.

Related

Visual Studio Code doesn't work with headers in the same project folder [duplicate]

I am using a simple function to add to integers, the class is declared in the Adder.h file as below
class Adder
{
public:
int add (int x, int y);
};
Then I have the Adder.cpp file which has the function definition
int add (int x, int y)
{
return x + y;
}
Then the main.cpp file which calls the function
# include "Adder.h"
# include <iostream>
using namespace std;
int main()
{
Adder adder1;
int result = adder1.add (2, 3);
cout << result;
}
I ran g++ -c Adder.cpp to create Adder.o file beforehand.
Then I ran g++ main.cpp but go the following error
main.cpp:(.text+0x2d): undefined reference to `Adder::add(int, int)'
Where am I going wrong?
In your second and final step, you didn't instruct the compiler (linker more exactly) to take into account Adder.o, so your final executable still doesn't know the implementation of Adder::add
Try, after getting Adder.o, to run g++ main.cpp Adder.o
Also, this may be relevant : Difference between compiling with object and source files
Also, if that is the complete code, as others have pointed out, in the Adder.cpp, you are just defining a simple function, not the one from the Adder class.
The problem is that you've defined a free function named add instead of defining a member function of class Adder. To define add as a member function we have to be in the scope of the class Adder which we can do by adding Adder:: before add as shown below:
Adder.cpp
//note the use of scope resolution operator ::
int Adder::add(int x, int y)//define a member function instead of a free function
{
return x + y;
}
In the above modified code, we are defining the member function add instead of the free function add.

Why am I getting an undefined reference error while using separate .cpp and .h files for a class?

I am using a simple function to add to integers, the class is declared in the Adder.h file as below
class Adder
{
public:
int add (int x, int y);
};
Then I have the Adder.cpp file which has the function definition
int add (int x, int y)
{
return x + y;
}
Then the main.cpp file which calls the function
# include "Adder.h"
# include <iostream>
using namespace std;
int main()
{
Adder adder1;
int result = adder1.add (2, 3);
cout << result;
}
I ran g++ -c Adder.cpp to create Adder.o file beforehand.
Then I ran g++ main.cpp but go the following error
main.cpp:(.text+0x2d): undefined reference to `Adder::add(int, int)'
Where am I going wrong?
In your second and final step, you didn't instruct the compiler (linker more exactly) to take into account Adder.o, so your final executable still doesn't know the implementation of Adder::add
Try, after getting Adder.o, to run g++ main.cpp Adder.o
Also, this may be relevant : Difference between compiling with object and source files
Also, if that is the complete code, as others have pointed out, in the Adder.cpp, you are just defining a simple function, not the one from the Adder class.
The problem is that you've defined a free function named add instead of defining a member function of class Adder. To define add as a member function we have to be in the scope of the class Adder which we can do by adding Adder:: before add as shown below:
Adder.cpp
//note the use of scope resolution operator ::
int Adder::add(int x, int y)//define a member function instead of a free function
{
return x + y;
}
In the above modified code, we are defining the member function add instead of the free function add.

How do you define a "Hello World" function in a seperate file in c++

and I apologize for asking a very basic question, but basically, I'm not able to wrap my head around include "fileImade.h"
I'm trying to write a main function, that's something like
int main()
{
int x = 5;
int y x 6;
std::cout << add(x, y) << std::endl;
}
where add() is defined in a separate .cpp file, and #include -ed in this one, (I'm doing this because I'm getting the point where my code is getting impractically large to do in a single file.), but my understanding is that you need a header file to... Glue your other files together, or I guess mortar them if the files are the bricks, but I absolutely cannot figure out how to make this work for the life of me.
(while using g++), should I tag -I? According to me googling, yes, according to my compiler output, no.
Should I write a header file and a .cpp files for the add() function? Apparantly, yes, or no, if I choose to write both files in the command line before the -o.
Should I include a forward declaration of the function in my main.cpp file? Again, according to the internet, yes, though that's not working terribly well for me.
So to my question: Can someone show me a main() function that calls a hello world() function in a separate file?
I'm honestly at my wits' end with this because all the guides seem to be on defining classes in header files, which, while useful, is a bit beyond the scope of what I'm attempting right now.
Thank you in advance for reading, and any advice offered.
The logic of the file separation may be imagined as:
(single file program)
/// DECLARATION of all functions needed in the main
int add(int x, int y); // declaration of add
///
int main()
{
std::cout << add(2, 3) << std::endl;
return 0;
}
/// IMPLEMENTATION of all functions needed in the main
int add(int x, int y)
{
return x + y;
}
The next you have to do is move all declarations to the headers and implementations to the cpps:
(separated files program)
/// add.h
#ifndef ADD_H /// https://en.wikipedia.org/wiki/Include_guard
#define ADD_H
int add(int x, int y);
#endif
/// main.cpp
#include "add.h"
int main()
{
std::cout << add(2, 3) << std::endl;
return 0;
}
/// add.cpp
#include "add.h"
int add(int x, int y)
{
return x + y;
}

Why do I get an Undefined reference error when trying to compile? [duplicate]

This question already has answers here:
Separating class code into a header and cpp file
(8 answers)
Closed 1 year ago.
Just had a little problem that I haven't been able to figure out yet.
I was using a similar program structure for a different project, but the problem boils down to this. I have two cpp files, which are:
Trading_dte.cpp :
#include <iostream>
using namespace std;
class Dte
{
public:
int addition(int a, int b)
{
return a + b;
}
};
dummy.cpp :
#include <iostream>
#include "Trading_dte.hpp"
Dte obj;
int check()
{
std::cout<<obj.addition(6,9);
}
I created a header file called Trading_dte.hpp :
# pragma once
#include <iostream>
class Dte
{
public:
int addition(int a, int b);
};
Now when I try compiling using the command :
g++ Trading_dte.cpp dummy.cpp
I get the error :
/usr/bin/ld: /tmp/ccCcM8R6.o: in function `check':
dummy.cpp:(.text+0x1a): undefined reference to `Dte::addition(int, int)'
collect2: error: ld returned 1 exit status
I'm sure it's something small, but I just can't figure what.
Thanks a lot in advance!
your cpp file need to be written differently
#include "Trading_dte.hpp"
#include <iostream>
int Dte::addition(int a, int b)
{
return a + b;
}
You've created two separate Dte classes, one visible to main and another visible only in Trading_dte.cpp. The one visible to main, defined in Trading_dte.hpp has a declaration of the addition member function but no definition.
Probably the easiest thing to do is to drop Trading_dte.cpp and put the implementation into the class definition in Trading_dte.hpp.
Trading_dte.hpp:
# pragma once
class Dte
{
public:
int addition(int a, int b)
{
return a + b;
}
};
Note that I also removed the #include <iostream> line. You don't need it in the header file because you don't use it in the class.

error LNK2005: "int __cdecl number(void)" (?number##YAHXZ) already defined in functions.obj

I think I am having a similar problem to LNK2005, LNK1169 errors, "int __cdecl g(void)" (?g##YAHXZ) already defined but I cannot find the issue.
I am working with Visual Basic and I am having the following files
main.cpp
#include <iostream>
#include "functions.h"
using namespace std;
int main(){
number();
return 0;
}
I had a functions.cpp but after reading the question I linked before I renamed it for functions.h
int number(){
int i = 1;
return i;
}
Now it is displaying error LNK2005: "int __cdecl number(void)" (?number##YAHXZ) already defined in functions.obj
Is there anything wrong with the function number() in functions.h?
You are having linking issues.
Your immediate issue is that the functions.obj contains code that is being linked in. Then you redefine number() in main.cpp so they collide. Go ahead and clean the project (which should delete functions.obj, and you should be able to compile. I, however, recommend doing it this way.
functions.hpp (or functions.h)
int number();
functions.cpp
int number(){
int i = 1;
return i;
}
main.cpp
#include <iostream>
#include "functions.h"
using namespace std;
int main(){
number();
return 0;
}
When you compile, your program will create 2 objects with compiled code functions.obj, and main.obj. Since you use number in the main file, the compiler looks for the implementation of that function. Since the implementation of that function is in the functions.obj object, then you need to link that in.
If you are going to use number() across several C++ files, then you should always separate the code out into its own file and implementation.
functions.h should only declare the functio, as in
int number();
then functions.cpp should contain the function definition
int number(){
int i = 1;
return i;
}
Of course functions.cpp needs to be compiled (add it to the project).
The problem here is that you are including functions.h in multiple files. The issue could be avoided also by simply declaring the function static as in
static int number(){
int i = 1;
return i;
}
However, since it seems you are just learning, I would suggest you to study the basics of compiling c++ code.
In one of the modules you link in there is a function with the name number(). You define you own implementation so the linker does not know which one to use.
Either rename your function or use namespaces.