[英]C++: TypeDef using Tuples
我正在嘗試使用元組創建 typedef 這是我的代碼。
#include <iostream>
#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_io.hpp>
#include "boost/tuple/tuple_comparison.hpp"
using namespace std;
typedef tuple<std::string, unsigned int, double> Person;
void Print(Person people)
{
};
int main()
{
using boost::tuple;
Person p0 (string("Udbhav"),10,10);
return 0;
}
當我這樣做時,我無法從 boost 調用 p0 上的 get<>() 。 有人可以指出我做錯了什么嗎?
您的程序中出現的問題是您using namespace std;
聲明,因此您的typedef
將元組解析為std::tuple
。 看起來你假設using boost::tuple;
在main()
中允許您使用 boost 中的一個,這是錯誤的。 typedef
不是宏,並且名稱解析發生在聲明時,而不是使用它。 您可以通過刪除using namespace std;
並且您的typedef
將無法編譯:
謝謝你的幫助。 確保 std 不會成為問題並且它有效。 這是解決方案。
#include <iostream>
#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_io.hpp>
#include "boost/tuple/tuple_comparison.hpp"
using namespace std;
typedef boost::tuple<string,unsigned int,double> Person;
void Print(Person people)
{
cout << "Name : " << people.get<0>() << endl;
cout<< "Age : " << people.get<1>() << endl;
cout << "Height : " << people.get<2>() << endl;
};
int main()
{
using boost::tuple;
Person p0("Shuniya",0,0);
Person p1("Alpha",1,1);
Person p2("Beta", 2, 2);
Print(p0);
p0.get<1>() = 1;
Print(p0);
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.