Compilation errors in program where objects are used as function arguments

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$
I have no idea why the compiler thinks that you want to call the time() function instead :|

As a temporary solution add class to the object declaration to explicitly define that you want the class named time instead.
eg : class time T1, T2, T3;

Or maybe change the time class to Time
Last edited on
closed account (NyqLy60M)
Just a heads up, it works perfectly fine with Windows 7 on both the Visual Studio and GNU GCC compilers. I'd recommend the UNIX/Linux Programming forum.
If you're getting name clashes, then I'd recommend using namespaces to disambiguate your names.
Thank you for you replies shadow fiend, Vemc and MikeyBoy.
@shadow fiend: It worked perfectly when I added class as you had suggested in object declaration :-)
Not sure about the reason but I think GCC on GNU/Linux was expecting this.

@MikeyBoy: When I started learning C++ (some 30 days before) I was using "using namespace std" in programs but I was advised not do that as it may result in name conflict and this link was suggested for reference

http://www.parashift.com/c++-faq/using-namespace-std.html

After your reply, I am again confused whether I should use namespace or not?
1
2
3
4
5
6
7
8
9
10
namespace foo{
   class time{
      //...
   };
}

int main(){
   foo::time T1, T2, T3;
   //...
}



using namespace is saying `I don't want to use that namespace'
Oh, ok. I was not aware about this use of namespace.
Thank you ne555.
When I said "using namespaces", I meant in the normal English meaning of the word "using". I didn't mean "add a using namespace foo; directive".

The whole point of namespaces is that they allow you to disambiguate between entities with the same name. As ne555 showed you, you define your class (or function, or whatever) within a namespace, so that it can be uniquely identified rather than clashing with something else called "time" in the global namespace.
Topic archived. No new replies allowed.