c++ - 多个文件困难的模板

标签 c++ templates undefined-reference

您好,我无法掌握如何在文件中组织模板类。 我看过 C++ templates, undefined reference 并尝试使用 http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12 中给出的提示将我的头文件和实现文件分开。

    #include "Variables.h"

    int main() {
Pressure<float,double> first(7.9,"atm");
return 0;
    }

变量是我的顶级类,为简洁起见,我删除了一些函数。

 /*
 * Variable_Parent.h
 *
 * Here an abstract base class from which variables can be derived is defined.
 *
 */

#ifndef PARENT_VARIABLE_H_
#define PARENT_VARIABLE_H_

#include <string>
#include <map>

template <typename V,typename D> // To let different variables use different S.F.
struct Variable{
    /*
     * This structure is a abstract base class from which all dimensional variables
     * are derived. Derived variables may be operated with and units will be largely
     * automatically handled.
     */

protected:
    //Functions
    Variable(V v, std::string u, D conversions [], std::map<std::string,short> units);
    ~Variable();

    //Variables
    V value;
    std::string unit;
    std::map<std::string, unsigned short> * valid_units; //contains all units for which conversions have been defined.
    D * conversion; //An n by n array for calculating conversions

public:
    V get_value() const;
    std::string get_unit() const;
    void change_unit(std::string new_unit);


        #endif /* PARENT_VARIABLE_H_ */

我的实现:

    /*
     * Header files
     */
    #include "Variables_Parent.h"
    #include <string>
    #include <map>
    #include <typeinfo>

    template<typename V,typename D>
    Variable<V,D>::Variable(V v, std::string u, D conversions [], std::map<std::string,short> units){
        value = v;
        unit = u;
        conversion = conversions;
        valid_units = &units;
    }

    template <typename V,typename D>
    V Variable<V,D>::get_value() const{
            return this.value;
        }

    template <typename V,typename D>
    std::string Variable<V,D>::get_unit() const{
        return unit;
    }

template <typename V,typename D>
void Variable<V,D>::change_unit(std::string new_unit){
    if (valid_units->find( new_unit ) == valid_units->end()){//Check the unit is defined
        std::string message("%s is not a valid unit. /n", new_unit);
        warning(message);
    }
    else{
        int target = (*valid_units)[new_unit];
        int original = (*valid_units)[unit];
        int width = valid_units->size();
        value*=conversion[ (original*width) + target];
        unit=new_unit;
    }
}

现在我希望能够创建此类的专门实例,这些实例在 valid_units 和转换指向的位置不同,我知道我可以将其作为构造函数中的一个选项,但我想要创建其中的许多实例一个简单的构造函数。所以:

/*
 * Variables.h
 *
 * Here all the variable classes are defined.
 *
 *  Created on: Nov 16, 2011
 *      Author: 
 */

#ifndef VARIABLES_H_
#define VARIABLES_H_

#include "Variables_Parent.h"
#include <string>


///////////////////////////////////////////////////////////////////////////////////////////////////////
/*
 * Pressure
 */
template <typename V,typename D>
class Pressure : Variable<V,D> {
public:
    Pressure(V v,std::string u);
    ~Pressure();
};

在底部的实现文件中,我已经按照 parashift FAQ 的建议注释掉了我尝试使用的两个模板类声明,这些行给我控制台错误:

Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/Variables.d" -MT"src/Variables.d" -o"src/Variables.o" "../src/Variables.cpp"
../src/Variables.cpp: In constructor ‘Pressure<V, D>::Pressure(V, std::string) [with V = float, D = double, std::string = std::basic_string<char>]’:
../src/Variables.cpp:27:16:   instantiated from here
../src/Variables.cpp:22:43: error: no matching function for call to ‘Variable<float, double>::Variable()’
../src/Variables_Parent.h:26:2: note: candidates are: Variable<V, D>::Variable(V, std::string, D*, std::map<std::basic_string<char>, short int>*) [with V = float, D = double, std::string = std::basic_string<char>]
../src/Variables_Parent.h:17:16: note:                 Variable<float, double>::Variable(const Variable<float, double>&)
../src/Variables.cpp:23:2: error: no matching function for call to ‘Variable<float, double>::Variable(float&, std::string&, double [3], const std::map<std::basic_string<char>, short unsigned int>&)’
../src/Variables_Parent.h:26:2: note: candidates are: Variable<V, D>::Variable(V, std::string, D*, std::map<std::basic_string<char>, short int>*) [with V = float, D = double, std::string = std::basic_string<char>]
../src/Variables_Parent.h:17:16: note:                 Variable<float, double>::Variable(const Variable<float, double>&)
make: *** [src/Variables.o] Error 1

\

/*
 * Variables.c++
 *
 *  Created on: Nov 18, 2011
 *      Author: 
 */

#include "Variables.h"
#include <string>
#include <boost/assign.hpp>

///////////////////////////////////////////////////////////////////////////////////////////////////////
/*
 * Pressure
 */
const static std::map<std::string, unsigned short> PRESSURE_UNITS =
    boost::assign::map_list_of("kPa",0)("atm",1)("psi",2);// might want new

static double PRESSURE_CONVERSION[3][3]= {{1,0.009869232667,0.145037738},{101.325,1,14.6959488},{6.89475729,0.0680459639,1}};

template <typename V,typename D>
Pressure<V,D>::Pressure(V v, std::string u){
    Variable<V,D>(v,u,PRESSURE_CONVERSION[0],PRESSURE_UNITS);
}

/*
template class Pressure<float, double>;
template class Variable<float, double>;
/*

当我不在 main 中包含声明时,我在尝试首先创建时遇到错误。 此行有多个标记 - 对 Pressure<float, double>::~Pressure()' - undefined reference to 的 undefined reference 压力::压力( float ,std::basic_string,std::allocator

)'

抱歉,我想确保我包含了此时给出的所有相关信息,但我没有任何线索。 在此先感谢我希望我不必将我的实现移到我的标题中。

最佳答案

使用模板时,实现应该在定义接口(interface)的翻译单元(最常见的是同一个头文件)中。有变通办法,但事实总是存在的。

关于c++ - 多个文件困难的模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8231811/

相关文章:

C++ - 有没有办法让模板类特化包含通用模板中的代码?

c++ - 传递给模板函数时,lambda 自动衰减为函数指针

无法使用多线程在我的树莓派上编译我的 C 程序

c++ - 对 main() 中函数的 undefined reference

c++ - recv() 延迟问题

c++ - vector 的交集

templates - 如何覆盖 MATLAB 中的默认文本

c++ - G++ 模板实例化导致 "Undefined reference to"错误

C++ 输出中的完全右对齐

c++ - 阻止调试器进入功能