简体   繁体   English

我因两次定义了 function 而收到此错误

[英]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.

相关问题 链接器在头文件中定义非内联函数时出错? - Linker Error on having non Inline Function defined in header file? 两个具有相同特征的对象是否只在内存中存储一​​次? 无论在哪个函数中定义它们都是不同的 - Is two objects having same characteristics get stored in memory only once? Irrespective of the function in which they are defined are different C ++:定义函数时出现未定义的函数错误 - C++: Undefined function error when I have defined the function 头文件中定义的模板函数,但仍然得到未解决的外部符号错误 - Template function defined in header but still get unresolved external symbol error 在获取下一个字符后如何正确刷新输入缓冲区,而不必两次按Enter键! C ++ - How do I flush the input buffer correctly after I get the next char without having to press enter twice! C++ 我如何检查该函数是否确实获得了定义为const的变量? - How can I check that function really get a variable that defined as const? 我已经声明,定义和#include的函数的“未定义引用”错误 - “Undefined Reference” error to a function I've already declared and defined and #included 缺少 vtable 错误说我没有定义我的虚函数 - missing vtable error saying I have not defined my virtual function 用户定义的本征函数使用的内存是预期的两倍 - User defined Eigen function using twice as much memory as expected 为什么我会同时收到函数已使用但未定义和已定义但未使用的警告? - Why do I get warnings both that a function is used but not defined and defined but not used?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM