2011-11-22 87 views
0

您好我無法掌握如何在文件中組織我的模板類。 我看過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常見問題的建議,這些行給我的控制檯錯誤:

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>; 
/* 

當我不包括在主我得到試圖建立第一個錯誤的聲明。在這條線 多個標記 - 未定義參考Pressure<float, double>::~Pressure()' - undefined reference to壓力::壓力(浮動,性病:: basic_string的,性病::分配器

)'

對不起我想確保我包括給所有相關信息的長度在這一點上,我沒有線索 在此先感謝我希望我不會將我的實現移動到我的標題

回答

2

當使用模板時,實現應該在翻譯單元中(最常見相同的頭文件),它定義了接口。有解決方法,但事實總是存在。