Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c+中双重包含库的神秘错误+;_C++_Include - Fatal编程技术网

C++ c+中双重包含库的神秘错误+;

C++ c+中双重包含库的神秘错误+;,c++,include,C++,Include,很抱歉这个蹩脚的标题,但我真的不知道发生了什么。它似乎两次声明了一个函数和一个变量。也许我在某个地方把它包括了两次,但是这个项目真的很小,我找不到它包括在哪里 我的代码在这里。当我进行make时,这是输出: g++ arbolesJuego.cpp main.cc -o othello /tmp/ccwVFD8e.o: In function `lookup()': main.cc:(.text+0x0): multiple definition of `lookup()' /tmp/cc3

很抱歉这个蹩脚的标题,但我真的不知道发生了什么。它似乎两次声明了一个函数和一个变量。也许我在某个地方把它包括了两次,但是这个项目真的很小,我找不到它包括在哪里

我的代码在这里。当我进行
make
时,这是输出:

g++  arbolesJuego.cpp main.cc  -o othello
/tmp/ccwVFD8e.o: In function `lookup()':
main.cc:(.text+0x0): multiple definition of `lookup()'
/tmp/cc3YvuYq.o:arbolesJuego.cpp:(.text+0x0): first defined here
/tmp/ccwVFD8e.o:(.bss+0x0): multiple definition of `trans'
/tmp/cc3YvuYq.o:(.bss+0x0): first defined here
collect2: ld returned 1 exit status
make: *** [all] Error 1
为什么会这样?任何帮助都将不胜感激

更新

因为,提供的链接是到回购协议的,它将得到改进(我希望呵呵),所以我接下来粘贴了错误的代码:

stored_info_t lookup() {
  stored_info_t info;
  return info;
};

hash_table_t trans;

trans
正在源文件中使用。

以下是您的问题之一:

定义

显示在头文件中。由于使用头将头复制到每个编译单元中,因此最终会得到多个同名变量。这会导致链接器错误

解决办法是说

extern hash_table_t trans;
在标题中,以及

hash_table_t trans;
在一个源文件中


类似的方法也适用于您的其他错误。

以下是您的问题之一:

定义

显示在头文件中。由于使用头将头复制到每个编译单元中,因此最终会得到多个同名变量。这会导致链接器错误

解决办法是说

extern hash_table_t trans;
在标题中,以及

hash_table_t trans;
在一个源文件中


类似的方法也适用于您的其他错误。

如果您在
hashTable.h
的第35行中使用此函数
内联

stored_info_t lookup() {
  return NULL;
}

它应该消除错误。

如果将此函数
内联
设置在hashTable.h的第35行

stored_info_t lookup() {
  return NULL;
}

它应该会消除错误。

或者您可以将它包装在名称空间周围,创建一个静态上下文,如下所示

namespace{
    hash_table_t trans;
    stored_info_t lookup() {
      return NULL;
    }
}

注意,如果可能的话,您应该避免使用全局变量。

或者您可以将其环绕在名称空间周围,以创建一个类似这样的静态上下文

namespace{
    hash_table_t trans;
    stored_info_t lookup() {
      return NULL;
    }
}

注意,如果可能的话,您应该避免使用globals。

谢谢!我必须在
.cpp
文件中重新声明它们吗?同样,这适用于变量trans,但不适用于函数(
lookup()
)。。。是否有类似的保留字C++在这些情况下使用?@ SuoZe:同样的方法适用:你必须把定义移到源文件中,只在头文件中留下一个声明。谢谢!我必须在
.cpp
文件中重新声明它们吗?同样,这适用于变量trans,但不适用于函数(
lookup()
)。。。是否有类似的保留字C++在这些情况下使用?@ SuoZe:同样的方法适用:必须将定义移动到源文件,只在头文件中留下声明。