class and header

how can i access the functions in header to a class in main.cpp

i mean what is the use of header file if i cannot access it on a class
Include the header in main.cpp.
yea i included it . thats not what i meant.
example

a.h
1
2
3
4
5
6
7
8
9
10
11
#ifndef A_H_INCLUDED
#define A_H_INCLUDED

int addition ( int number1 , int number2 ) {
return number1+number2;
}




#endif // A_H_INCLUDED 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
main.cpp
#include <iostream>
#include "a.h"
using namespace std;
int addition( int number1 , int number2 );
class Calculator {
int firstnumber;
int secondnumber;
public:
 void operate();
};
void Calculator::operate() {
cout << "Enter number";
cin >> firstnumber >> secondnumber;
addition(firstnumber, secondnumber);
}
int main()
{
Calculator newCalculator;
newCalculator.operate();

}



the error is undefined reference to addition (int , int)

but i already declare the prototype and include the header

You don't need to declare the prototype as you have it declared in the header that has been included in the source file.
I have no idea why you get an error. Are you sure this is the same code that gave you the error?
@ajh32 oh i see ,about declaring prototype in header
but i think declaring prototype in main instead on header would be more readable
Last edited on
@Peter87

yeah this is
why not give me code similar to this
Last edited on
@ajh32 oh i see ,about declaring prototype in header
but i think declaring prototype in main instead on header would be more readable


No, just remove the line #5 in the source file as you have the function declared and written in the header, at compile time the #include "a.h" is replaced by the contents of the "a.h" file, in your example as if you had written the addition function at line #3 of your source.
Last edited on
The reason I asked was because when I compile your code it compiles without any problems. Could you please post the full error message that you get?
||=== Build: Debug in asd (compiler: GNU GCC Compiler) ===|

obj\Debug\main.o||In function `ZN10Calculator7operateEv':|

C:\Users\Lorence\Desktop\asd\main.cpp|14|undefined reference to `addition(int, int)'|

||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 2 second(s)) ===|
@ajh32
i see but it doesnt work due to my issue
Topic archived. No new replies allowed.