C ++类错误
我有这段代码
#include <iostream>
using namespace std;
class time{
public:
time(); //constructor
void settime(int,int,int);
void print();
private:
int hour,min,sec;
};
//constructor
time::time(){
hour=min=sec=0;
}
int main(){
int num;
time t1;//line1
time t2;//line2
cout<<"hello"<<endl;
cin>>num;
return 0;}
,这些行中的错误是:
预期的“;”在“t1”[警告]语句之前
“time”的引用,而不是调用,
是对每行函数
有什么问题吗???
i have this code
#include <iostream>
using namespace std;
class time{
public:
time(); //constructor
void settime(int,int,int);
void print();
private:
int hour,min,sec;
};
//constructor
time::time(){
hour=min=sec=0;
}
int main(){
int num;
time t1;//line1
time t2;//line2
cout<<"hello"<<endl;
cin>>num;
return 0;}
and the errors in those lines are:
expected `;' before "t1"
[Warning] statement is a reference, not call, to function `time'
for each line
whats the problem???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有一个
std::time
函数,可以通过使用using namespace std;
导入到全局命名空间中。这与您名为time
的类冲突。这是永远不要在命名空间范围内使用using namespace std;
的另一个很好的理由。但请注意,并非所有标准库实现都遵守以下规则:默认情况下,标准库中来自 C 标准库的名称不应放置在全局命名空间中。
另一种选择是用
class
限定名称time
,这将允许它在任何系统上工作:您也可以考虑重命名您的类。
There is a
std::time
function that is imported into the global namespace by your use ofusing namespace std;
. This conflicts with your class namedtime
. This is yet another good reason never to useusing namespace std;
at namespace scope.Note, however, that not all standard library implementations respect the rule that names in the standard library that come from the C standard library should not be placed in the global namespace by default.
Another option is to qualify the name
time
withclass
, which will allow this to work on any system:You might also just consider renaming your class.