C++ 错误:应为'';在定义类的对象之前

C++ 错误:应为'';在定义类的对象之前,c++,C++,我的编译器提供了一些非常有用的诊断,可以准确地解释发生了什么: output: time.cpp: In function ‘int main()’: time.cpp:25:7: error: expected ‘;’ before ‘t1’ time t1; ^ time.cpp:28:2: error: ‘t1’ was not declared in this scope t1.get_time(5, 4, 2);

我的编译器提供了一些非常有用的诊断,可以准确地解释发生了什么:

output:
    time.cpp: In function ‘int main()’:
    time.cpp:25:7: error: expected ‘;’ before ‘t1’
      time t1;
           ^
    time.cpp:28:2: error: ‘t1’ was not declared in this scope
      t1.get_time(5, 4, 2);
      ^

我认为最好的策略是给你的类起一个不同的名字(可能是
Time
加上大写的
T
?)

有一个全局函数叫做,继承自C标准库。它隐藏您的类名,[basic.scope.declarative]/4:

给定单个声明区域中的一组声明,每个 指定相同的非限定名称

  • [……]

  • 只有一个声明可以声明不是typedef名称的类名或枚举名 其他声明均应[……]提及功能 和功能模板在这种情况下,类名或枚举名是隐藏的(3.3.10)。[…]


最简单的解决方案是给你的类另一个名字(例如,代码>时间<代码> -C和C++标准库,在标识符中不使用大写字母)。

与你的问题无关。如果你有一个<代码> GETYTIME>/代码>函数,它通常应该返回一些东西。您所拥有的应该是
set\u time
output:
    time.cpp: In function ‘int main()’:
    time.cpp:25:7: error: expected ‘;’ before ‘t1’
      time t1;
           ^
    time.cpp:28:2: error: ‘t1’ was not declared in this scope
      t1.get_time(5, 4, 2);
      ^
$ g++ test.cpp
test.cpp:25:6: error: must use 'class' tag to refer to type 'time' in this scope
            time t1;
            ^
            class 
/usr/include/time.h:118:8: note: class 'time' is hidden by a non-type
      declaration of 'time' here
time_t time(time_t *);
       ^
1 error generated.