Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 模板函数定义的常见做法-与函数声明混合使用?_C++ - Fatal编程技术网

C++ 模板函数定义的常见做法-与函数声明混合使用?

C++ 模板函数定义的常见做法-与函数声明混合使用?,c++,C++,大多数时候,我“避免”在单个头文件中使用以下样式 class a { void fun(); }; void a::fun() { } 为了避免以下错误。我尝试将cpp文件中的类定义和h文件中的类声明分开。例如,下面的例子是错误的: main.cpp a、 h 我将得到以下错误: 1>b.obj : error LNK2005: "public: virtual int __thiscall a::fun(void)" (?fun@a@@UAEHXZ) already de

大多数时候,我“避免”在单个头文件中使用以下样式

class a {
    void fun();
};

void a::fun() {
}
为了避免以下错误。我尝试将cpp文件中的类定义和h文件中的类声明分开。例如,下面的例子是错误的:


main.cpp
a、 h 我将得到以下错误:

1>b.obj : error LNK2005: "public: virtual int __thiscall a::fun(void)" (?fun@a@@UAEHXZ) already defined in main.obj
但是,谈到模板,我通常会这样做:

a、 h
\ifndef A\u H
#定义一个
#包括
模板
甲级{
公众:
虚拟T-fun();
};
模板ta::fun()
{
T;

std::cout您可以通过将
a::fun()
的定义声明为
inline
来消除LNK2005错误。例如:

// a.h

// ...

inline int a::fun()
{
    int t;
    std::cout << "a" << std::endl;
    return t;
}
然后,在某个地方,你需要做这样的事情(恰好一次):


通过将
a::fun()
的定义声明为
inline
,可以消除LNK2005错误。例如:

// a.h

// ...

inline int a::fun()
{
    int t;
    std::cout << "a" << std::endl;
    return t;
}
然后,在某个地方,你需要做这样的事情(恰好一次):

1> b.obj:错误LNK2005:“公共: 虚拟整数_uthiscall a::fun(void)” (?fun@a@@UAEHXZ)已在中定义 main.obj

出现此错误是因为
a::fun()
不是
内联的

inline int a::fun()
{
    int t;
    std::cout << "a" << std::endl;
    return t;
}
inline int a::fun()
{
int t;
标准::cout
1> b.obj:错误LNK2005:“公共:
虚拟整数_uthiscall a::fun(void)”
(?fun@a@@UAEHXZ)已在中定义
main.obj

出现此错误是因为
a::fun()
不是
内联的

inline int a::fun()
{
    int t;
    std::cout << "a" << std::endl;
    return t;
}
inline int a::fun()
{
int t;

STD::CUT声明虚拟函数是内联的。Naveen为什么不正确?编译器可以并且将在线函数,如果他可以,否则他会忽略内联。如果声明为内联,他将负责一个定义规则。我有点困惑。更有效的C++(Scott Mayers)第24项指出,如果类中的所有虚拟方法都是内联的,则可能会出现问题。在这种情况下,可能会在包含该类的所有对象文件中生成该类的v-table。我不打算内联函数。将虚拟函数声明为内联似乎不正确。@Naveen为什么不?编译器可以并将内联函数如果他能,否则他会忽略内联。如果它被声明为内联,他会负责一个定义规则。我有点困惑…更有效的C++(Scott Mayers)第24项指出,如果一个类中的所有虚拟方法都是内联的,则可能会出现问题。在这种情况下,可能会在包含该类的所有对象文件中生成该类的v表。我不打算内联该函数。
#ifndef A_H
#define A_H

#include <iostream>

template <typename T>
class a {
public:
    virtual T fun();
};


template<typename T> T a<T>::fun()
{
    T t;
    std::cout << "a" << std::endl;
    return t;
}

#endif
// a.h

// ...

inline int a::fun()
{
    int t;
    std::cout << "a" << std::endl;
    return t;
}
// a.h

// ...

#ifdef DEFINE_CLASS_A_FUNCTIONS

int a::fun()
{
    int t;
    std::cout << "a" << std::endl;
    return t;
}

#endif
#define DEFINE_CLASS_A_FUNCTIONS
#include "a.h"
inline int a::fun()
{
    int t;
    std::cout << "a" << std::endl;
    return t;
}