我是C ++的新手,我正在尝试制作一个计数器。我被困在构建类,并得到一些编译器错误

时间:2013-03-04 06:16:29

标签: c++ counter

这是我目前为类文件所做的,我不确定接下来要采取什么措施。 Hit应该将值增加1,并且reset应该将其设置为0.cpp文件中的main函数应该使用manual 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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您需要提供以下实现:

  void WebCounter::set(int value);
  void WebCounter::reset();
  void WebCounter::hit();
  int WebCounter::get();