相关疑难解决方法(0)

typedef和显式实例化之间的代码重复

tree.h中

template<typename Functor, char Operator>
class binary_operation : public node
{
// ... unimportant details ...

    unsigned evaluate() const;
    void print(std::ostream& os) const;
};

typedef binary_operation<std::plus<unsigned>, '+'> addition;
typedef binary_operation<std::multiplies<unsigned>, '*'> multiplication;
// ...
Run Code Online (Sandbox Code Playgroud)

tree.cpp

template<typename Functor, char Operator>
unsigned binary_operation<Functor, Operator>::evaluate() const
{
    // ... unimportant details ...
}

template<typename Functor, char Operator>
void binary_operation<Functor, Operator>::print(std::ostream& os) const
{
    // ... unimportant details ...
}

template class binary_operation<std::plus<unsigned>, '+'>;
template class binary_operation<std::multiplies<unsigned>, '*'>;
// ...
Run Code Online (Sandbox Code Playgroud)

如您所见,头文件中的typedef与实现文件中的显式类模板实例之间存在一些代码重复.有没有办法摆脱不需要像往常一样在头文件中放置"所有"的重复?

c++ templates typedef code-duplication header-files

5
推荐指数
1
解决办法
1250
查看次数

C++ -- 我可以使用没有在头文件中实现的模板函数吗?

可能的重复:
将 C++ 模板函数定义存储在 .CPP 文件中
为什么模板只能在头文件中实现?
为什么模板类的实现和声明要在同一个头文件中?

我有三个文件。在 base.h 中,我有一个类,它有一个使用模板的成员:

class Base {
    protected:
        template <class T>
            void doStuff(T a, int b);
};
Run Code Online (Sandbox Code Playgroud)

在 base.cpp 中,我实现了 Base::doStuff():

#include "base.h"

template <class T>
void Base::doStuff(T a, int b) {
    a = b;
}
Run Code Online (Sandbox Code Playgroud)

然后我尝试在我的项目的另一个类中使用它:

#include "base.h"

void Derived::doOtherStuff() {
    int c;
    doStuff(3, c);
}
Run Code Online (Sandbox Code Playgroud)

但是我收到一个链接错误,指出它找不到“doStuff(int, int)”

从我所看到的,如果不将此函数的实现移动到头文件中,这在 C++03 中是不可能的。有没有干净的方法来做到这一点?(我可以使用 C++11x 功能)。

c++ templates

5
推荐指数
1
解决办法
2276
查看次数

如何在C++中用模板编写的库的闭源

阅读本文在.CPP文件中存储C++模板函数定义后,我知道使用模板的类的定义必须以某种方式写入头文件中.因此,客户端用户可以通过导入头文件来使用它.这是不是意味着向用户显示完整的实施细节?这是否也意味着用模板编写的库必须以开源方式提供?

感谢您帮助澄清它.

c++ templates

3
推荐指数
1
解决办法
1140
查看次数

c ++中的模板成员函数

我在c ++中有泛型问题.我有两个Matrix.h和Matrix.cpp文件.这是文件:

#pragma once
template<class T>
class Matrix
{
    public:
        static T** addSub(int size,T** firstMatrix,T** secondMatrix,int operation);
}
Run Code Online (Sandbox Code Playgroud)

和Matrix.cpp

#include "Martix.h"
template<class T>
static T** Matrix<T>::addSub( int n,T **firstMatrix,T **secondMatrix,int operation)
{
    //variable for saving result operation
    T **result = new T*[n];

    //create result matrix
    for( int i=0;i<n;i++)
        result[i] = new T[n];

    //calculate result
    for( int i=0;i<n;i++)
        for(int j=0;j<n;j++)
            result[i][j] = 
            (operation == 1) ? firstMatrix[i][j] + secondMatrix[i][j]:
                                firstMatrix[i][j] - secondMatrix[i][j];

    return result;
}
Run Code Online (Sandbox Code Playgroud)

当我运行这些时,我收到以下错误:

Error   1   error LNK2019: unresolved …
Run Code Online (Sandbox Code Playgroud)

c++ templates

3
推荐指数
1
解决办法
215
查看次数

避免在头文件中定义模板

我在标题Ah中定义了以下类:

class A {

private:

template<int i>
void method();

};
Run Code Online (Sandbox Code Playgroud)

我有什么办法可以method使用通常的方法实现将实现保留在自己的A.cpp文件中?我问这个问题是因为将实现放在一个Ah中会使接口很难阅读,特别是因为它是私有函数。

仅在重要的情况下才使用“ i”的有限值进行实例化

c++ templates

3
推荐指数
1
解决办法
1755
查看次数

轻松实施模板

假设我在某处编写了类模板声明collector.h:

template <class T, int maxElements>
class collector {

    T elements[maxElements];
    int activeCount;

public:

    collector();
    void process();
    void draw();

};
Run Code Online (Sandbox Code Playgroud)

并在collector.cpp以下方面实施其三种方法:

template <class T, int maxElements> 
collector<T, maxElements>::collector(){
    //code here
}

template <class T, int maxElements>
void collector<T, maxElements>::process(){
    //code here
}

template <class T, int maxElements>
void collector<T, maxElements>::draw(){
    //code here
}
Run Code Online (Sandbox Code Playgroud)

有没有办法不写作template <class T, int maxElements><T, maxElements> 每个功能的实现?像这样的东西:

template <class T, int maxElements>{

    collector<T, maxElements>::collector(){
        //code here
    }

    void collector<T, maxElements>::process(){ …
Run Code Online (Sandbox Code Playgroud)

c++

1
推荐指数
1
解决办法
218
查看次数

标签 统计

c++ ×6

templates ×5

code-duplication ×1

header-files ×1

typedef ×1