Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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++ 代码不使用std::pair和构造函数编译_C++_Templates_Compiler Errors_Std Pair - Fatal编程技术网

C++ 代码不使用std::pair和构造函数编译

C++ 代码不使用std::pair和构造函数编译,c++,templates,compiler-errors,std-pair,C++,Templates,Compiler Errors,Std Pair,以下代码未编译: #include <iostream> #include <set> #include <utility> using namespace std; template <typename T1, typename T2> class meta { pair<T1,T2> xy_pair; public: /* meta(const T1 & t1, const T2 &

以下代码未编译:

#include <iostream> 
#include <set>
#include <utility>
using namespace std; 

template <typename T1, typename T2>  
class meta {
   pair<T1,T2> xy_pair; 

   public: 
   /* meta(const T1 & t1, const T2 & t2) : 
    xy_pair.first(t1), 
    xy_pair.second(t2)
    {}*/
  meta() : 
    xy_pair.first(T1()), 
    xy_pair.second(T2())
    {}
  void print() const{
      cout << xy_pair.first << endl; 
      cout << xy_pair.second << endl; 
  }  

}; 

int main(){
 meta<int,int> met_xy; 

}
我发现以下编译器错误:

[root@localhostSTL]g++-std=c++0x集.cpp-o集 sets.cpp:在构造函数“meta::meta”中: sets.cpp:16:16:错误:在“.”标记之前应为“” sets.cpp:16:16:错误:在“.”标记之前应为“{” sets.cpp:在成员函数“void meta::print const”中: sets.cpp:20:29:错误:应在“,”标记之前使用主表达式 sets.cpp:20:32:错误:在“>”标记之前应该有主表达式 sets.cpp:20:33:错误:应在“.”标记之前使用主表达式 sets.cpp:21:29:错误:应在“,”标记之前使用主表达式 sets.cpp:21:32:错误:在“>”标记之前应该有主表达式 sets.cpp:21:33:错误:应在“.”标记之前使用主表达式
不能像这样初始化初始值设定项列表中的成员对象的单个子成员。请将其设置为

meta() : xy_pair(T1(), T2()) {}
或者干脆

meta() {}

或者实际上完全忽略默认构造函数-编译器生成的构造函数也会做同样的事情。

您的代码应该如下所示:

#include <utility>
#include <ostream>
#include <iostream>

template <typename T1, typename T2>  
class meta {
   std::pair<T1, T2> xy_pair;

public: 
    meta(const T1 & t1, const T2 & t2) : xy_pair(t1, t2) { }
    // note initialization of the whole member xy_pair, not its members
    meta() : xy_pair() { }

    void print() const {
       cout << xy_pair.first << endl; 
       cout << xy_pair.second << endl; 
    }
}; 

int main() {
   meta<int, int> meta_xy;
}

这是无效的C++。使用:XYONGION{{}。