Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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++_Templates_Specialization - Fatal编程技术网

C++ 模板类中专用函数的声明

C++ 模板类中专用函数的声明,c++,templates,specialization,C++,Templates,Specialization,我正在努力解决的问题是在模板类中声明专用模板函数,我将类声明保存在头文件中,并在关联的.C文件中定义成员函数 我有一个表示点的模板类。头文件如下所示: //... template<typename T, int dim=3> // T - coords. type, int dim - no. of dimensions class Point { public: // ... // function below sets val at t

我正在努力解决的问题是在模板类中声明专用模板函数,我将类声明保存在头文件中,并在关联的.C文件中定义成员函数

我有一个表示点的模板类。头文件如下所示:

//...

template<typename T, int dim=3> // T - coords. type, int dim - no. of dimensions
class Point {
    public:
        // ...
        // function below sets val at the given position in array m_c and returns reference
        template<int position> Point& set(T val); 
    private:
        T m_c[dim]; // coordinates
};

//...
据我所知,这是其定义的最一般形式

在main函数中,我创建浮点为T的点,并尝试在数组中设置一些值:

int main(int argc, char** argv) {
    Point<float> p1;
    p1.set<0>(3).set<1>(3.6).set<2>(3);
    //...
}
为了在头文件外定义模板的成员函数时实现这一点,我需要通知编译器.C文件中的专门化:

template<typename T, int dim> template<int position> Point<T, dim>& Point<T, dim>::set(T val){
    // ...
    return *this;
}
模板类点

我还需要声明set函数的用法,我尝试用这种方法来实现 这段代码就是问题所在:

模板模板点和点::setfloat

不幸的是,这不起作用,我会出错:

/tmp/ccR7haA5.o: In function `main':
.../pdim.C:32: undefined reference to `Point<float, 3>& Point<float, 3>::set<0>(float)'
.../pdim.C:32: undefined reference to `Point<float, 3>& Point<float, 3>::set<1>(float)'
.../pdim.C:32: undefined reference to `Point<float, 3>& Point<float, 3>::set<2>(float)'

如果有人知道如何处理这个问题,我会非常感谢他的解释。谢谢。

为了在不同的TU中提供函数模板专门化的定义,您需要一个明确的实例化声明:

[点.水电站]

template<typename T, int dim=3>
struct Point
{
    template<int position> Point& set(T val);
};

// `extern` makes this an explicit instantiation _declaration_
extern template Point<float,3>& Point<float,3>::set<0>(float);
extern template Point<float,3>& Point<float,3>::set<1>(float);
extern template Point<float,3>& Point<float,3>::set<2>(float);
[Point.cpp]

#include <iostream>
#include "Point.hpp"

template<typename T, int dim>
template<int position>
Point<T,dim>& Point<T,dim>::set(T val)
{
    // note: non-standard macro
    std::cout << __PRETTY_FUNCTION__ << std::endl;
    return *this;
}

// no `extern`: this is an explicit instantiation _definition_
// which instantiates the function template, and therefore requires the definition
// to be available in this TU
template Point<float,3>& Point<float,3>::set<0>(float);
template Point<float,3>& Point<float,3>::set<1>(float);
template Point<float,3>& Point<float,3>::set<2>(float);
[main.cpp]

#include "Point.hpp"

int main()
{
    // in this TU, there's no definition for the function template
    // hence, it cannot be instantiated
    // however, we can use the explicit instantiations

    Point<float,3>().set<0>(0);
    Point<float,3>().set<1>(0);
    Point<float,3>().set<2>(0);

    // does not compile (linker error):
    //Point<int,3>().set<0>(0);
    //Point<float,4>().set<0>(0);
    //Point<float,3>().set<4>(0);
}

可能的重复为了在无法实例化集合的TU中使用集合的专门化,您需要从实例化集合的TU中导出所需的特定专门化。我必须查找语法。不幸的是,建议的主题与我的问题无关。我想知道这样的功能是什么:模板Point&setT val;应该声明-在我的理解中,如何告诉编译器生成足够的代码。在本例中,要与类点的实例一起使用。有:一些有用的提示,但参考了未指定type.Ex的模板成员函数。外部模板点和点::setfloat;注意:不需要显式实例化类模板;成员函数的定义不需要用于实例化类模板。