“~~”的多重定义首先在此定义错误

时间:2014-03-11 09:39:36

标签: c++

我正在尝试测试我写的一个函数但是我一直收到这个错误。我搜索了一些地方,他们都说使用“extern”或类似的东西,但它似乎不是它不起作用,或者我做得不对。

这是.h文件:

#ifndef BOGGLEUTIL_H
#define BOGGLEUTIL_H

#include <string>
#include <vector>

class TST;
class bogNode;
class BoggleUtil{
  public:
  static void insertWords(const std::vector<std::string> &word_list, int counter,
                   int startR, int startL, int start, TST& t);

};

class TST {
  private:
    /**
     * Returns negative if b < c
     * 0 if b == c, positive if b > c 
     */
    int charCmp(char b, char c);

  protected:
    bogNode* root;

  public:
    /**
     * Inserts a string into the ternary tree
     */
    void insert(const std::string word);
};

/**
 * A sick-ass node for a sick-ass tree
 */
class bogNode{
  public:
    // Constructor goes here
    bogNode(const char &c): data(c) {
      data = c;
      left = right = center = nullptr;
      end = false;
    }


  //private:
    bogNode* left;    // Children nodes
    bogNode* right;
    bogNode* center;
    char data;    // Data contained in node
    bool end;              // True if node is an end node
};

#endif // BOGGLEUTIL_H

这是我的.cpp文件的一部分:

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

void BoggleUtil::insertWords(const std::vector<std::string>& word_list, int counter,
               int startR, int startL, int start, TST& t)
{
...
}


int TST::charCmp(char b, char c){
    return ((int) b - c);
}

void TST::insert(const std::string word) 
{
...
}

这是我为了一个简单的测试目的而编写的主要内容:

#include "boggleutil.cpp"
#include <vector>
#include <string>

int
main(int argc, char* argv[])
{
  std::vector<std::string> test;
  test.push_back("test1");
  test.push_back("test2");
  test.push_back("test3");
  test.push_back("test4");
  test.push_back("test5");
  test.push_back("test6");
  test.push_back("test7");
  test.push_back("test8");
  test.push_back("test9");
  test.push_back("test10");
  test.push_back("test11");
  test.push_back("test12");
  test.push_back("test13");
  test.push_back("test14");
  test.push_back("test15");
  test.push_back("test16");
  test.push_back("test17");
  test.push_back("test18");
  test.push_back("test19");
  test.push_back("test20");

  int counter = test.size()/2;
  int start = counter;
  int l = counter - counter/2;
  int r = counter + counter/2;

  TST t;

  BoggleUtil::insertWords(test, counter, r, l, start, t);
}

最后,这是我在编译器中得到的错误报告:

g++ -std=c++11 -g boggleutil.cpp boggleutil.h maintest.cpp
/tmp/ccTcJDhf.o: In function `__gnu_cxx::new_allocator<std::string>::new_allocator()':
/home/linux/ieng6/cs100w/bhn013/P4/boggleutil.cpp:5: multiple definition of `BoggleUtil::insertWords(std::vector<std::string, std::allocator<std::string> > const&, int,     int, int, int, TST&)'
/tmp/ccu0bhaW.o:/home/linux/ieng6/cs100w/bhn013/P4/boggleutil.cpp:5: first defined here
/tmp/ccTcJDhf.o: In function `TST::charCmp(char, char)':
/home/linux/ieng6/cs100w/bhn013/P4/boggleutil.cpp:62: multiple definition of `TST::charCmp(char, char)'
/tmp/ccu0bhaW.o:/home/linux/ieng6/cs100w/bhn013/P4/boggleutil.cpp:62: first defined here
/tmp/ccTcJDhf.o: In function `TST::insert(std::string)':
/home/linux/ieng6/cs100w/bhn013/P4/boggleutil.cpp:66: multiple definition of `TST::insert(std::string)'
/tmp/ccu0bhaW.o:/home/linux/ieng6/cs100w/bhn013/P4/boggleutil.cpp:66: first defined here

我对这个错误一直出现的原因有一丝了解。任何帮助表示赞赏!提前谢谢。

1 个答案:

答案 0 :(得分:2)

您包含cpp:

#include "boggleutil.cpp"

这样链接器链接两次BoggleUtil::insertWords(std::vector<std::string, std::allocator<std::string> > const&, int, int, int, int, TST&)

进入boggleutil.cpp然后进入maintest.cpp