Fast plz! 'time' does not name a type but it's declared in a struct

the code is supposed to take 2 clock times and take the difference between them.
and write it out in second, later on i will transform it into hour,min,sec again, but due to have gone in to compilation error i can't continue.



#include <iostream>
using namespace std;
struct time
{
int h,m,s;

};
int tosec( const time &);
int main()
{
time t;
time t1;
char c;
cout<<"get first clock \n";
cin>> t.h >>c>> t.m >>c>> t.s;
cout<<"get second clock\n";
cin>> t1.h >>c>> t1.m >>c>> t1.s;
int sec;
sec=tosec(t);
int sec1;
sec1=tosec(t);
int timediff;
timediff=(sec-sec1);
if(timediff<0)
{
timediff=-timediff;
}
cout<<timediff;
return 0;
}
int tosec( const time &t)
{
return 3600*t.h +60*t.m +t.s;
}

following compilation error i get

i don't get following
"test.cc:9:18: error: ‘time’ does not name a type"

$ g++-4.7 -Wall -Wextra -pedantic -std=c++11 test.cc
test.cc:9:18: error: ‘time’ does not name a type
test.cc:9:23: error: ISO C++ forbids declaration of ‘parameter’ with no type [-fpermissive]
test.cc: In function ‘int main()’:
test.cc:14:8: error: expected ‘;’ before ‘t’
test.cc:14:9: warning: statement is a reference, not call, to function ‘time’ [-Waddress]
test.cc:14:9: warning: statement has no effect [-Wunused-value]
test.cc:15:8: error: expected ‘;’ before ‘t1’
test.cc:15:10: warning: statement is a reference, not call, to function ‘time’ [-Waddress]
test.cc:15:10: warning: statement has no effect [-Wunused-value]
test.cc:18:8: error: ‘t’ was not declared in this scope
test.cc:20:8: error: ‘t1’ was not declared in this scope
test.cc: At global scope:
test.cc:36:18: error: ‘time’ does not name a type
test.cc:36:24: error: ISO C++ forbids declaration of ‘t’ with no type [-fpermissive]
test.cc: In function ‘int tosec(const int&)’:
test.cc:39:17: error: request for member ‘h’ in ‘t’, which is of non-class type ‘const int’
test.cc:39:25: error: request for member ‘m’ in ‘t’, which is of non-class type ‘const int’
test.cc:39:30: error: request for member ‘s’ in ‘t’, which is of non-class type ‘const int’
test.cc:40:1: warning: control reaches end of non-void function [-Wreturn-type]



It seems that the name of the structure conflicts with standard C function time(). Try to change the name of the structure for example from time to Time. everywhere in your program:)

EDIT: or use keyword struct everywhere before name time. For example

int tosec( const struct time &);

Last edited on
I did some changes on the code then i got no compilation errors and can run the program correct but i still don't get why my first tri didn't work can anyone explain?

EDIT: i tried to set struct as @vlad from moscow said and then my first example worked :) thanks


here is the new code the difference is that I have read function connected to the declartion


#include <iostream>
using namespace std;
struct time
{
int h,m,s;

};
int read();

int tosec( const time &);
int main()
{
cout<<"get first clock\n";
time t=read();
cout<<"get second clock\n";
time t1=read();


int sec;
sec=tosec(t);
int sec1;
sec1=tosec(t);
int timediff;
timediff=(sec-sec1);
if(timediff<0)
{
timediff=-timediff;
}
cout<<timediff;
return 0;
}
int tosec( const time &t)
{
return 3600*t.h +60*t.m +t.s;
}
int read()
{
time t;
char c;
cin>> t.h >>c>> t.m >>c>> t.s;
return t;
}
Last edited on
As for me it looks like a bug of the compiler.
Topic archived. No new replies allowed.