繁体   English   中英

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

[英]I get this error for having defined a function twice

错误

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

我创建了一个 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;
}

您在 header 中定义 function,这就是问题所在。 header 文件中的多重定义

inline解决方案很好,或者您可以将声明保留在 hpp 文件中并在单独的 cpp 文件中实现它 - 这是最“标准”的解决方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM