簡體   English   中英

了解C ++中的模板

[英]Understanding templates in c++

我正在嘗試運行以下程序,但是會生成編譯錯誤:

#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 / classs易於鏈接錯誤。 任何人都可以解釋/闡述嗎? 在上面的示例中是否有可能鏈接錯誤?

聲明如下:

“如果在.h文件中聲明了模板或內聯函數,請在同一文件中對其進行定義。這些結構的定義必須包含在使用它們的每個.cpp文件中,否則程序可能無法在某些構建配置中鏈接 ”。

可以使用更簡單的方式完成此示例,而無需使用重載運算符等。但是,我正在嘗試學習/練習模板並嘗試一些功能。

您需要為friend函數聲明提供單獨的模板定義:

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

friend聲明不繼承封閉類的模板參數。

一個簡單的示例源,開始;

計算器

#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 ,以了解如何保留模板源和標頭內容,以及如何解決編譯和鏈接器問題(有原因)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM