Reputation: 39
I'm trying to get a reference to my class but it seems to
is not declared.
This means it's undeclared:
#include <iostream>
using namespace std;
class time
{
private:
int sec;
int mins;
int hours;
public:
void setSeconds(int x)
{
sec = x;
}
int getSeconds()
{
return sec;
}
};
int main()
{
time to;
to.setSeconds(10000000);
cout << to.getSeconds() << endl;
return 0;
}
The error reads:
main.cpp: In function 'int main()':
main.cpp:29:10: error: expected ';' before 'to'
time to;
^
main.cpp:29:12: warning: statement is a reference, not call, to function 'time' [-Waddress]
time to;
^
main.cpp:29:12: warning: statement has no effect [-Wunused-value]
main.cpp:30:5: error: 'to' was not declared in this scope
to.setSeconds(10000000);
^
Upvotes: 2
Views: 147
Reputation: 385144
std::time
is a function in the C++ standard library and, since you're using namespace std
, that's being used by default instead of your class.
You can't even write ::time
to reference yours, because your compiler's standard library implementation happens to include the old C ::time
as well before wrapping it into namespace std
.
Use some or all of these suggestions:
class time
to reference your class (this ensures the type time
is used, but is a poor hack)You should also stop using namespace std
in general to help avoid as much trouble as possible, though it cannot directly aid you in this case.
Upvotes: 8
Reputation: 2234
Clang give a better error message:
time.cpp:29:5: error: must use 'class' tag to refer to type 'time' in this scope
time t;
^
class
/usr/include/time.h:192:15: note: class 'time' is hidden by a non-type declaration of 'time' here
extern time_t time (time_t *__timer) __THROW;
^
It isn't related to using namespace std
. Rather the global time function is conflicting.
Upvotes: 1