multiple definition linker error after adding a function to a previously linking file

前端 未结 1 818
孤城傲影
孤城傲影 2021-02-08 12:21

So my program is working fine. Compiling, linking, running, the works. Then, I decide to add a simple function to one of my files, like this:

#ifndef UTILITY_H         


        
1条回答
  •  有刺的猬
    2021-02-08 12:55

    Either declare the function inline, or define it in a separate .cpp file. Otherwise every C++ file in which you include the header is trying to make its own, publicly-available definition of the function.

    Edit: and fwiw, you don't need to explicitly return true or false if you're testing a conditional. Just return the conditional itself:

    inline bool isVowel(const char c)
    {
        return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
    }
    

    0 讨论(0)
提交回复
热议问题