了解c ++中的模板

时间:2015-07-03 10:05:38

标签: c++ templates

我正在尝试运行以下程序,但它会生成编译错误:

#ifndef TEMPLATE_SUM_H_
#define TEMPLATE_SUM_H_

template<typename T>
class sum
{
  public:
    sum() {
      val_1 = 0;
      val_2 = 0;
    }
    sum(T a, T b) {
      val_1 = a;
      val_2 = b;
    }
    friend std::ostream& operator<<(std::ostream &, const sum<> &);

  private:
    T val_1, val_2;
    T result() const;
};

#endif

源文件:

include <iostream>
#include "inc/sum.h"

template<typename T>
T sum<T>::result() const {
   return (val_1 + val_2);
}

template<typename T>
std::ostream& operator<<(std::ostream& os, const sum<T>& obj) {
//std::ostream& operator<<(std::ostream& os, sum<T>& obj) {
  os << obj.result();
  return os;
}

int main()
{
    sum<int> int_obj(15, 15);
    sum<float> float_obj(5.2, 3.5);
    std::cout << "result of int = " << int_obj << std::endl;
    std::cout << "result of float = " << float_obj << std::endl;
    return 0;
}

使用 g ++(4.4.3)进行编译会产生以下错误:

In file included from template.cpp:2:
inc/sum.h:18: error: wrong number of template arguments (0, should be 1)
inc/sum.h:5: error: provided for ‘template<class T> class sum’
template.cpp: In function ‘std::ostream& operator<<(std::ostream&, const sum<T>&) [with T = int]’:
template.cpp:20:   instantiated from here
template.cpp:5: error: ‘T sum<T>::result() const [with T = int]’ is private
template.cpp:12: error: within this context
template.cpp: In function ‘std::ostream& operator<<(std::ostream&, const sum<T>&) [with T = float]’:
template.cpp:21:   instantiated from here
template.cpp:5: error: ‘T sum<T>::result() const [with T = float]’ is private
template.cpp:12: error: within this context

1)有人可以帮我识别错误吗? 另请提供一些链接,我可以在其中找到有关如何在c ++中使用模板的简要绝对详细信息。

2)我读到在头文件中声明的模板化的func / classes,并且单独定义的内容很容易出现链接错误。任何人都可以解释/详述吗? 在上面的例子中是否有可能链接错误?

声明如下:

“如果在.h文件中声明模板或内联函数,请在同一文件中定义。这些构造的定义必须包含在使用它们的每个.cpp文件中,否则程序可能会失败链接一些构建配置。“

这个例子可以用一些更简单的方式完成,不需要使用重载操作符等。但我正在尝试学习/练习模板并尝试一些功能。

2 个答案:

答案 0 :(得分:6)

您需要为friend函数声明设置单独的模板定义:

template<typename U>
friend std::ostream& operator<<(std::ostream &, const sum<U> &);

friend声明不继承封闭类的模板参数。

答案 1 :(得分:1)

一个简单的示例源,开始使用;

  

Calculator.h

#ifndef CALCULATOR_H
#define CALCULATOR_H

template <class TYPE>
class Calculator{

public:
    Calculator();
    TYPE Sum(TYPE param1, TYPE param2);

};


/**
 * To avoid template related compilation error
 * when templates are used in header and source files
 *
 * This class file has been removed from the project-source file.
 * However, is present in the project folder
 * Gets compiled with the header-file (being included)
 */
#include "Calculator.cpp"

#endif
  

Calculator.cpp

#include <iostream>
using namespace std;
#include "Calculator.h"


template <class TYPE>
Calculator<TYPE>::Calculator()
{
}

template <class TYPE>
TYPE Calculator<TYPE>::Sum(TYPE param1, TYPE param2){

    cout << "Calculator::sum" << endl;
    cout << param1 <<endl;
    cout << param2 <<endl;

    TYPE result = param1 + param2 ;

    return result;
}
  

Main.cpp的

#include <iostream>
using namespace std;
#include "Calculator.h"

int main(int argc, const char * argv[]) {
    cout << "Hello, Calculator!\n";

    Calculator<int> cObj;
    int out = cObj.Sum(2,3);
    cout << "out : " << out << endl;

    Calculator<string> cObjS;
    string outS = cObjS.Sum("A", "B");
    cout << "outS : " << outS << endl;

    cout << "Bye, Calculator!\n";


    return 0;
}

此外,您可以参考post,了解如何保留模板源和标头内容,以及了解如何修复编译和链接器问题(原因)。