-1

这是我到目前为止的类文件,我不确定下一步该采取什么措施。hit 应该将值增加 1,reset 应该将其设置回 0。.cpp 文件中的 main 函数应该使用手动 hit 和 set 命令测试其功能。任何有关如何实现这一点的帮助或建议将不胜感激。

头文件 WebCounter.h:

#include<iostream>
  class WebCounter {
    private:
      int value;
      value=0;
    public:
      void set(int value);
      void reset();
      void hit();
      int get();
};

测试计数器的主要功能

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

  using namespace std;

  int main() {

    WebCounter hitcount;
    hitcount.reset();
    cout << hitcount.get() << endl;

    int value;

    value=hitcount.get();
    cout << value << endl;
    hitcount.hit();
    hitcount.hit();
    hitcount.hit();
    hitcount.hit();
    cout << hitcount.get() << endl;
    hitcount.set(77);
    cout << hitcount.get() << endl;
    hitcount.set(89);

    return 0;
}

这些是我在编译 .cpp 文件时遇到的错误

g++ WebCounter.cpp
/tmp/cce7bLn5.o: In function `main':
WebCounter.cpp:(.text+0x10): undefined reference to `WebCounter::reset()'
WebCounter.cpp:(.text+0x1c): undefined reference to `WebCounter::get()'
WebCounter.cpp:(.text+0x41): undefined reference to `WebCounter::get()'
WebCounter.cpp:(.text+0x6c): undefined reference to `WebCounter::hit()'
WebCounter.cpp:(.text+0x78): undefined reference to `WebCounter::hit()'
WebCounter.cpp:(.text+0x84): undefined reference to `WebCounter::hit()'
WebCounter.cpp:(.text+0x90): undefined reference to `WebCounter::hit()'
WebCounter.cpp:(.text+0x9c): undefined reference to `WebCounter::get()'
WebCounter.cpp:(.text+0xc6): undefined reference to `WebCounter::set(int)'
WebCounter.cpp:(.text+0xd2): undefined reference to `WebCounter::get()'
WebCounter.cpp:(.text+0xfc): undefined reference to `WebCounter::set(int)'
collect2: ld returned 1 exit status
4

2 回答 2

0

您将需要提供以下实现:

  void WebCounter::set(int value);
  void WebCounter::reset();
  void WebCounter::hit();
  int WebCounter::get();
于 2013-03-04T06:28:46.913 回答
0

提出 WebCounter.cpp,它将实现您在 WebCounter.h 中声明的所有函数(get、hit、set 等)。然后尝试编译它。编译成功后,尝试编译具有 main 方法的 .cpp。

于 2013-03-04T06:23:37.943 回答