简体   繁体   English

C ++中多重定义的解决方案

[英]Solution to Multiple definition in C++

guys, this is my first question in StackOverflow so forgive me if make any mistake. 伙计们,这是我在StackOverflow中的第一个问题,如果有任何错误,请原谅。
I am writing a small project which contains 2 source files and 3 header files. 我正在写一个包含2个源文件和3个头文件的小项目。

// some_template_functions.h // some_template_functions.h

#ifndef SOME_TEMPLATE_FUNCTION_H
#define SOME_TEMPLATE_FUNCTION_H

template <typename T>
int getvalue(string line, string key, T & val)
{
    // method to get value (all the types except string) from line using key
}
template <>
int getvalue<string>(string line, string key, string &val)
{
    // method to get some string from line using key, similar to getvale<int>
    // but with slight difference to handle some special characters
}

#endif

//myclass.h //myclass.h

#ifndef MYCLASS_H
#define MYCLASS_H
#include "some_template_functions.h"

class MYCLASS
{
    //declarations of constructors, member functions and members
    double member_double;
    string member_string;
}

#endif

//myclass.cpp //myclass.cpp

#include "myclass.h"

MYCLASS:MYCLASS()
{
    // for each member
    // using "getvalue" defined in "SOME_TEMPLATE_FUNCTION.H" to get the values 
    getvalue(line, key, member_double);
    getvalue(line, key, member_string);
}

//main.cpp //main.cpp

#include "myclass.h"
#include "some_template_functions.h"

int main()
{
    myclass A;
    int some_value;
    getvalue(line, key, value);
    return 0;
}

I have no problem compiling the main.o and myclass.o but it is just when I was trying to link the two object files I got error message like: 我编译main.o和myclass.o没问题,但是只是在我尝试链接两个目标文件时,出现如下错误消息:

     myclass.cpp: multiple definition of int getvalue<std::basic_string><char, std::char_traits<char>, ...> and etc.    
     /tmp/cc22zEow.o:main.cpp: first defined here    
     collect2: ld returned 1 exit status    

I know the reason probably is because I am including "some_template_function.h" in both myclass.h and main.cpp, each myclass.o and main.o is going to have its own definition of getvalue which is causing the problem. 我知道原因可能是因为我在myclass.h和main.cpp中都包含了“ some_template_function.h”,每个myclass.o和main.o都有自己的getvalue定义,这会引起问题。 If I change 如果我改变

#include "some_template_functions.h"

to

#ifndef SOME_TEMPLATE_FUNCTIONS_H
#define SOME_TEMPLATE_FUNCTIONS_H
#endif

the constructors of MYCLASS is not goint to work. MYCLASS的构造函数无法正常工作。

I plan to expand the "some_template_functions.h" and its .cpp file in the future so if possible I would like to keep them separated from the other files. 我计划将来扩展“ some_template_functions.h”及其.cpp文件,因此,如果可能的话,我希望将它们与其他文件分开。 And because the way I am declaring function "getvalue" my attempt to move its definition to .cpp file was not working out for me very well. 而且由于我声明函数“ getvalue”的方式,将其定义移至.cpp文件的尝试对我来说不是很好。

I've tried to solve this problem for days but since I just start to learn C++ so far I could not get this right. 我已经尝试了好几天来解决这个问题,但是自从我刚开始学习C ++以来,我一直无法解决这个问题。 So please, any suggestions will be appreciated! 因此,请提出任何建议! Thanks in advance! 提前致谢!

The specialization of getvalue<std::string>(...) isn't a template and, thus, not implicitly inline . getvalue<std::string>(...)的专业化不是模板,因此也不是inline If you want to define this function in a header, eg, because it is close to trivial and should be inline d, you'll need to mark it explicitly as inline . 例如,如果您想在标头中定义此函数,因为它接近琐碎且应为inline d,则需要将其显式标记为inline If the function does anything non-trivial it may be worth merely declaring the specialization in the header and defining it in a suitable translation unit. 如果函数做了一些重要的事情,那么仅在标头中声明特殊化并在合适的转换单元中进行定义就值得。

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

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