John Bardeen
John Bardeen

Reputation: 105

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 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;
}

Upvotes: 3

Views: 168

Answers (1)

farbiondriven
farbiondriven

Reputation: 2468

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.

Upvotes: 2

Related Questions