似乎无法在主目录中调用类
Can't seem to call a class in main
我试图获取对我的类的引用,但似乎没有声明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;
}
错误如下:
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);
^
std::time
是C++标准库中的一个函数,由于您using namespace std
,因此默认情况下使用它而不是您的类。
您甚至无法编写::time
来引用您的,因为编译器的标准库实现恰好在将其包装到命名空间std
之前也包含旧的 C ::time
。
使用以下部分或全部建议:
- 给你的班级一个更好、更独特的名字
- 编写
class time
来引用您的类(这可确保使用类型time
,但这是一个糟糕的黑客) - 自己使用命名空间以避免将来的所有歧义(建议这样做)
您还应该停止一般using namespace 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;
^
它与using namespace std
无关。 相反,全局时间函数是冲突的。
相关文章:
- 如何在主函数中调用模板类
- C++函数无法正确调用主函数
- 在哪里调用主函数(dev c ++)
- 在 Ubuntu 16.04 的 usr/local/bin 中安装的 c++ 应用程序在从主目录执行时无法正常运行
- 如何使用 C++17 std::文件系统获取用户的主目录?
- 如何将主目录替换为波浪号?Linux
- 我在从另一个类调用主函数时遇到问题
- 如何从不同的线程调用主v8循环中的函数
- 缺少libgomp:在主目录中安装c++编译器
- C++存储调用主的次数
- 似乎无法在主目录中调用类
- CRT如何调用主,具有不同的参数
- C++ WinApi 选项卡帮助调用主静态栏
- 如何从C++中的类调用主函数中的数组
- 将解码实例添加到可以调用主类的类中
- 编译一个文件时会在我的主目录中留下文件,以进行流浪提供
- 调用主构造函数
- 主目录中没有".cmake"文件
- 链接到我的主目录中的共享对象文件
- QT退出不调用主窗口析构函数