【问题标题】:Yet another 'member declaration not found'又一个“未找到成员声明”
【发布时间】:2017-06-28 04:30:06
【问题描述】:

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

我首先编写了一个Clock 超类,并为我的构造函数/析构函数以外的每个方法获得了一个Member declaration not found。我认为这是某个地方的一个小错误,但我什么也找不到。

时钟.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_ */

时钟.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;
}

【问题讨论】:

    标签: c++ eclipse mingw


    【解决方案1】:

    我怀疑错误消息有点长,这有助于将其视为一个整体。
    话虽如此,错误可能是由于您的定义应该是:

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

    代替:

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

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

    【讨论】:

      【解决方案2】:

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

      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;
      }
      

      【讨论】:

        猜你喜欢
        • 2012-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-08
        • 1970-01-01
        • 2011-09-14
        • 2012-11-21
        • 1970-01-01
        相关资源
        最近更新 更多