C++ Can';I don’’我似乎没有在大班上课
我试图获取对我的类的引用,但似乎未声明C++ Can';I don’’我似乎没有在大班上课,c++,C++,我试图获取对我的类的引用,但似乎未声明to 这意味着它是未声明的: #include <iostream> using namespace std; class time { private: int sec; int mins; int hours; public: void setSeconds(int x) { sec = x; } int getSeconds() { retu
to
这意味着它是未声明的:
#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;
}
<>代码> STD::时间<代码>是C++标准库中的一个函数,因为您使用NoStESTST/<代码> >代码,默认情况下它将被使用而不是您的类。 您甚至不能编写
::time
来引用您的代码,因为编译器的标准库实现在将其包装到命名空间std
之前也恰好包含了旧的C::time
使用以下部分或全部建议:
- 给你的类一个更好、更独特的名字
- 写
参考你的班级(,但这是一个差劲的黑客)上课时间
- 自己使用名称空间以避免将来出现歧义(建议这样做)
一般来说,您也应该停止使用命名空间std
,以帮助避免尽可能多的麻烦,尽管在这种情况下它不能直接帮助您。Clang提供更好的错误消息:
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;
^
它与使用命名空间std的无关。而是全局时间函数是冲突的。< /p> @克里斯在C++中是必需的。@克里斯:因为它是语言规则所要求的。这里没有引用。现在,使用标题空间改进建议。使用名称空间STD<代码>停止<代码> -似乎<代码> <代码>包括<代码> <代码>(在LBC+++和LIbSTDC++中都有)。“安东斯万”在C++标题中摆脱全局命名空间的东西是否如此困难?或者,这不是出于遗留原因吗?@BaummitAugen:D.5/2允许这样做,主要是为了遗留原因。是的。无论如何,[extern.names]都明确保留了该名称。
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;
^