我有一个配置的项目:“的......多重定义”错误
./main.cpp
./type_traints/TypeTraints.cpp
./type_traints/TypeTraints.hpp
./type_traints/chapter_20.hpp
的./type_traints/CMakeLists.txt文件是:
cmake_minimum_required (VERSION 2.8)
add_library(chapter_20 TypeTraints.cpp)
和./CMakeLists.txt如下:FIL的
cmake_minimum_required (VERSION 2.8)
project (mpl)
add_subdirectory(type_traints)
include_directories(type_traints)
link_directories(type_traints)
add_executable (mpl main.cpp)
target_link_libraries(mpl chapter_20)
相关部分ES(最包括略)包括:
./type_traints/chapter_20.hpp
#ifndef CHAPTER_20_GUARD
#define CHAPTER_20_GUARD
#include <TypeTraints.hpp>
void chapter_20() {
test_23();
}
#endif //CHAPTER_20_GUARD
./type_traints/TypeTraints.hpp
#ifndef TYPE_TRAINTS_GUARD
#define TYPE_TRAINTS_GUARD
namespace details {
template<class T> const char* class2name() {
return "unknown";
};
template<> const char* class2name<int>() {
return "int";
};
}
template<class T>
class type_descriptor {
friend std::ostream& operator << (std::ostream& stream,
const type_descriptor<T>& desc) {
stream << desc.getName();
return stream;
}
public:
std::string getName() const;
};
template<class T>
std::string type_descriptor<T>::getName() const {
return details::class2name<T>();
}
void test_23();
#endif // TYPE_TRAINTS_GUARD
./type_traints/TypeTraints.cpp
#include<TypeTraints.hpp>
void test_23() {
cout << type_descriptor<int>() << endl;
}
and ./main.cpp
#include <chapter_20.hpp>
int main(int argc, char* argv[]) {
chapter_20();
return 0;
}
该项目编译,但无法链接:如果我从TypeTraints.hpp删除class2name专业化(class2name<int>()
),并且只使用了通用实现
[ 50%] Building CXX object type_traints/CMakeFiles/chapter_20.dir/TypeTraints.cpp.o
Linking CXX static library libchapter_20.a
[ 50%] Built target chapter_20
[100%] Building CXX object CMakeFiles/mpl.dir/main.cpp.o
Linking CXX executable mpl
type_traints/libchapter_20.a(TypeTraints.cpp.o): In function `char const* details::cl
ass2name<int>()':
/home/marcin/Projects/mpl/type_traints/TypeTraints.hpp:312: multiple definition of `c
har const* details::class2name<int>()'
CMakeFiles/mpl.dir/main.cpp.o:/home/marcin/Projects/mpl/type_traints/TypeTraints.hpp:
312: first defined here
collect2: ld returned 1 exit status
make[2]: *** [mpl] Błąd 1
make[1]: *** [CMakeFiles/mpl.dir/all] Error 2
make: *** [all] Error 2
23:56:[email protected] ~/P
项目链接罚款。
有没有人有想法为什么?我错过了配置cmake文件吗?
谢谢你的回答。但是,将函数定义更改为TypeTraints.cpp(template <> const char * class2name(){return“int”}中的声明(模板<> const char * class2name ();)一个compilatin错误:“/home/marcin/Projects/mpl/type_traints/TypeTraints.cpp:120:error:expected initializer before'<'token” –
Marcin
2010-12-21 00:08:00
@Marcin:你搞砸了一些东西。 。我猜你忘了命名空间,该定义应该在'details'命名空间内完成,或者应该使用一个限定名称(即它必须明确声明它是'details:class2name'你是定义) –
AnT
2010-12-21 00:24:55
是的,我已经搞砸了命名空间,一个试过的程序员是个笨手笨脚的程序员:/谢谢你,你太棒了! – Marcin 2010-12-21 00:28:57