[英]I get this error for having defined a function twice
Error :错误:
CMakeFiles\Final_Project_2nd.dir/objects.a(Tab.cpp.obj): In function `Z8Type2IntNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE':
C:/Users/Andrea/CLionProjects/Final_Project_2nd/Utils.hpp:37: multiple definition of `Type2Int(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
CMakeFiles\Final_Project_2nd.dir/objects.a(main.cpp.obj):C:/Users/Andrea/CLionProjects/Final_Project_2nd/Utils.hpp:37: first defined here
I've created a header Utils.hpp
with two enum
s and two functions and I included it wherever I needed to use these things:我创建了一个 header
Utils.hpp
,其中包含两个enum
和两个函数,并将它包含在需要使用这些东西的任何地方:
enum Types {
OptionInt,
OptionFloat,
[...]
OptionInvalid
};
enum Commands {
CommandCreate = OptionInvalid + 1,
CommandDrop,
[...]
CommandInvalid
};
Types Type2Int(string type){
if(type == "int") return OptionInt;
if(type == "float") return OptionFloat;
[...]
return OptionInvalid;
}
Commands Command2Int(string command){
if(command == "CREATE") return CommandCreate;
if(command == "DROP") return CommandDrop;
[...]
return CommandInvalid;
}
You are defining the function in the header, that's the problem.您在 header 中定义 function,这就是问题所在。 multiple definition in header file
header 文件中的多重定义
The inline
solution is fine, in alternative you can keep the declaration in the hpp file and implement it in a separate cpp file - which is the most 'standard' solution. inline
解决方案很好,或者您可以将声明保留在 hpp 文件中并在单独的 cpp 文件中实现它 - 这是最“标准”的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.