Hi,
I am trying to write a C++ program where objects are used as function arguments.
I am getting some compilation error for the objects that I have created in int main which I can't understand as why they might be coming. I am on GNU/Linux and using g++ to compile the code. not sure if the issue might be related to it
Can you please help to understand what wrong am I doing.
Here is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
#include <iostream>
class time
{
int hours, minutes;
public:
void gettime(int h, int m)
{ hours = h; minutes = m;}
void puttime(void)
{
std::cout << hours << " hours and ";
std::cout << minutes << " minutes " << "\n";
}
void sum(time, time); //declaration with objects as arguments
};
void time :: sum(time t1, time t2) //t1, t2 are objects
{
minutes = t1.minutes + t2.minutes;
hours = minutes/60;
minutes = minutes%60;
hours = hours + t1.hours + t2.hours;
}
int main()
{
time T1, T2, T3; //compilation error(Line 27)
T1.gettime(2,45); //get T1 //compilation error(Line 29)
T2.gettime(3,30); //get T2 //compilation error(Line 30)
T3.sum(T1,T2); //T3 = T1 + T2 //compilation error (Line 32)
std::cout << "T1 = "; T1.puttime(); //display T1
std::cout << "T2 = "; T2.puttime(); //display T2
std::cout << "T3 = "; T3.puttime(); //display T3
return 0;
}
|
And this is the compilation errors and warning that I get.
-bash-3.00$ g++ Objects_As_Arguments.cpp -o Objects_As_Arguments
Objects_As_Arguments.cpp: In function `int main()':
Objects_As_Arguments.cpp:27: error: expected `;' before "T1"
Objects_As_Arguments.cpp:27: warning: statement is a reference, not call, to function `time'
Objects_As_Arguments.cpp:29: error: `T1' was not declared in this scope
Objects_As_Arguments.cpp:30: error: `T2' was not declared in this scope
Objects_As_Arguments.cpp:32: error: `T3' was not declared in this scope
-bash-3.00$