【问题标题】:error: new types may not be defined in a return type错误:新类型可能未在返回类型中定义
【发布时间】:2014-04-24 23:24:53
【问题描述】:

上述错误发生在我实现构造函数的那一行。我发表了一条指向那条线的评论。非常感谢任何帮助。

#include <vector>   
#include <time.h>      

class Stopwatch
{
    enum state {UNSTARTED, RUNNING, PAUSED, FINISHED};
    struct time
    {
        unsigned hours;
        unsigned minutes;
        unsigned seconds;
    };
    struct lap
    {
       unsigned n; // lap number
       time t; // lap time
       lap* next_ptr;
    };

    public: 
    Stopwatch();
    ~Stopwatch();
    void right_button(); // corresponds to start/stop/pause
    void left_button();  // corresponds to lap/reset

    private: 
    state cur_state;
    std::vector<lap> lapsvec;

}

Stopwatch::Stopwatch() // <--------- Here's where the compiler error is
{
    cur_state = UNSTARTED;
}

/* trivial destructor */
Stopwatch::~Stopwatch() 
{
}

int main()
{
    return 0;
}

我查看了 C++ 构造函数,看看能否找出问题所在。没有运气。

【问题讨论】:

标签: c++


【解决方案1】:

您需要在类声明之后添加;。由于您没有它,因此当编译器到达Stopwatch::Stopwatch() 时尚未声明该类,因此抱怨它是一种新类型。

【讨论】:

    【解决方案2】:

    你正在声明一个类,它的末尾没有;

     ...
        std::vector<lap> lapsvec;
    
    };
    

    这应该可以解决它:

    【讨论】:

      猜你喜欢
      • 2011-03-13
      • 2019-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-31
      • 1970-01-01
      • 2021-04-15
      相关资源
      最近更新 更多