Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 类名不命名类型c++;_C++_Class_Templates - Fatal编程技术网

C++ 类名不命名类型c++;

C++ 类名不命名类型c++;,c++,class,templates,C++,Class,Templates,我正在尝试重载Pila是一个类模板,使用它时需要指定模板参数。并且可以使操作符Pila成为类模板,使用时需要指定模板参数。您可以使operatorostream&operator编译器是正确的:Pila不是类的名称。它是模板的名称。ostream&operator编译器是正确的:Pila不是类的名称。这是一个模板的名称。旁注:我将省略print底部的return。旁注:我将省略print底部的return。它没有任何作用。 #include <vector> #include <

我正在尝试重载
Pila
是一个类模板,使用它时需要指定模板参数。并且可以使
操作符
Pila
成为类模板,使用时需要指定模板参数。您可以使
operator
ostream&operator编译器是正确的:
Pila
不是类的名称。它是模板的名称。
ostream&operator编译器是正确的:
Pila
不是类的名称。这是一个模板的名称。旁注:我将省略
print
底部的
return
。旁注:我将省略
print
底部的
return
。它没有任何作用。
#include <vector>
#include <iostream>
using namespace std;


template <class T>
class Pila {
 private:
  vector <T> elem;

 public:
  /* Pila(){

  } */

  Pila ( int n ) {
    elem.resize(n);
  }

  void print(ostream & f_out){
    for (int i = 0; i < getNElem(); i++)
      f_out << elem[i] << " ";
    return;
  }


};

ostream& operator << (ostream& f_out, Pila p ){
  p.print(f_out);
  return f_out;
}
template <typename T>
ostream& operator << (ostream& f_out, Pila<T> p ){
  p.print(f_out);
  return f_out;
}
template <class T>
class Pila {
  ...

  void print(ostream & f_out) const {
    for (int i = 0; i < getNElem; i++)
      f_out << elem[i] << " ";
  }

};

template <typename T>
ostream& operator << (ostream& f_out, const Pila<T>& p ){
  p.print(f_out);
  return f_out;
}