还有一个 'member declaration not found'

Yet another 'member declaration not found'

我已经有一段时间没有写过任何 C++ 了,我只是想做一个简单的程序来复制不同类型的时钟来刷新我的记忆。

我已经开始编写一个 Clock 超类,并且除了我的 constructors/destructor 之外,每个方法都得到了一个 Member declaration not found。我认为这是某个地方的小错误,但我无法发现任何东西。

Clock.h

/*
 * Clock.h
 */

#ifndef CLOCK_H_
#define CLOCK_H_

class Clock {
private:
    int seconds;
    int minutes;
    int hours;

public:
    Clock();
    Clock(int, int, int);
    virtual ~Clock();
    virtual void tick() = 0;
    void setTime(int, int, int);
    void print();
};

#endif /* CLOCK_H_ */

Clock.cpp

/*
* Clock.cpp
 */

#include "Clock.h"
#include <iostream>

Clock::Clock() {
    seconds = 0;
    minutes = 0;
    hours = 0;
}

Clock::Clock(int secs, int mins, int hrs) :
        seconds(secs), minutes(mins), hours(hrs) {
}

Clock::~Clock() {
    // TODO Auto-generated destructor stub
}

Clock::setTime(int secs, int mins, int hrs) {
    seconds = secs;
    minutes = mins;
    hours = hrs;
}

Clock::print() {
    std::cout << hours << ":" << minutes << ":" << seconds << std::endl;
}

我怀疑错误消息有点长,它有助于整体查看。
也就是说,错误可能是由于您的定义应该是:

void Clock::setTime(int secs, int mins, int hrs) { /* ... */ } 

而不是:

Clock::setTime(int secs, int mins, int hrs) { /* ... */ } 

也就是说,return 您的情况缺少类型。
这同样适用于 print.

实现文件 (Clock.cpp) 中缺少以下方法的 return 类型。

Clock::setTime(int secs, int mins, int hrs) {
    seconds = secs;
    minutes = mins;
    hours = hrs;
}

Clock::print() {
    std::cout << hours << ":" << minutes << ":" << seconds << std::endl;
}

应该是

void Clock::setTime(int secs, int mins, int hrs) {
    seconds = secs;
    minutes = mins;
    hours = hrs;
}

void Clock::print() {
    std::cout << hours << ":" << minutes << ":" << seconds << std::endl;
}