0

What can i do for add functions by using files .h and .cpp?

code: source file include <iostream> include"calc.h" using namespace std; int main (){ calc (); return 0; } .h header #ifndef calc_h #define calc_h class functions { public: int calc (); }; #endif//calc_h .cpp function contains #include "calc.h" #include <iostream> using namespace std; int main (){ class functions { public: int calc (){ cout <<"hi"<<endl; }; }

20th Jul 2016, 1:02 PM
Ismael Perbech
Ismael Perbech - avatar
5 Respostas
+ 1
First of all, because your function does not return something it gets void instead of int. then the ".h" file is your template for your class. here you define the structure of your class. therefore all things you defined here goes out of your class. cpp in the class.cpp after your using namespace comes your function: void functions::calc() { cout.... } in your source is missing functions MyCalc; MyCalc.calc(); that should work then
21st Jul 2016, 12:02 AM
Steven
Steven - avatar
0
thanks it s usseful to know what i did wrong
21st Jul 2016, 1:23 PM
Ismael Perbech
Ismael Perbech - avatar
0
np... only additional Info. if you want to pass values into your class you have to do: in your ".h" void calc(int); in your ".cpp" void functions::calc(int x) { cout << 10*x << endl; in your source MyCalc.calc(5);
21st Jul 2016, 1:39 PM
Steven
Steven - avatar
0
and if i have a lot of variables in my function ??
25th Jul 2016, 12:49 PM
Ismael Perbech
Ismael Perbech - avatar
0
You can send aß much vars to your function as you need. But i have to admit, i never tried to get more than one result back from a function. But i would suggest that it's not possible because of your return Statement whats defined at the beginning of your function
31st Jul 2016, 12:01 AM
Steven
Steven - avatar