了解c ++中的模板 [英] Understanding templates in c++

查看:146
本文介绍了了解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> 
#includeinc / 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)生成以下错误:

 在template.cpp中包含的文件中:2:
inc / sum.h:错误:模板参数的数目错误(0,应为1)
inc / sum.h:5:错误:为'template< class T& class sum'
template.cpp:在函数'std :: ostream&操作符<<<<<(std :: ostream& const sum< T&)[with T = int]':
template.cpp:20:从这里实例化
template.cpp:错误:'T sum< T> :: result()const [with T = int]'is private
template.cpp:12:error:在此上下文中
template.cpp: :: ostream&操作符<<<<<<<<(std :: ostream& const sum<>&)[with T = float]':
template.cpp:21:从这里实例化
template.cpp:错误:'T sum< T> :: result()const [with T = float]'is private
template.cpp:12:error:在此上下文中
/ pre>

1)任何人都可以帮我识别错误吗?
还请建议一些链接,我可以找到关于如何使用c ++中的模板的简短绝对细节。



2)我读了模板化的func /头文件,并单独定义都容易出现链接错误。任何人都可以解释/阐述这个?
上面的例子中是否有可能出现链接错误?



语句如下:



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



这个例子可以在一些更简单的方法,而不使用重载的操作符等,但我想学习/练习模板和实验一些功能。

解决方案

您需要为 code>函数声明:

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

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


I am trying run the following program, But it generates compilation error:

#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

Source file:

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;
}

Compiling with g++ (4.4.3) it generates following error:

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) Can Anyone please help me in identifying the error ? Also Please suggest some links where I can find brief absolute details on how to use templates in c++.

2) I read that templatized func/classes declared in header file, and defined separately are prone to linking error. Can anyone explain/elaborate this ? Is there any possibility of linking error in above example ?

The statement is as below:

"If a template or inline function is declared in a .h file, define it in that same file. The definitions of these constructs must be included into every .cpp file that uses them, or the program may fail to link in some build configurations."

This example can be done in some more easy way, without using overloaded operator etc. But I am trying to learn/practising templates and experimenting some features.

解决方案

You need to have a separate template definition for the friend function declaration:

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

friend declarations do not inherit the template parameters of the enclosing class.

这篇关于了解c ++中的模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆