Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I get this error for having defined a function twice

Tags:

c++

oop

enums

class

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 enums and two functions and I included it wherever I needed to use these things:

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;
}
like image 580
John Bardeen Avatar asked Jan 17 '26 06:01

John Bardeen


1 Answers

You are defining the function in the header, that's the problem. multiple definition in header file

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.

like image 157
farbiondriven Avatar answered Jan 19 '26 18:01

farbiondriven



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!