简体   繁体   English

C++:使用元组的 TypeDef

[英]C++: TypeDef using Tuples

I am attempting to create a typedef using tuples here is my code.我正在尝试使用元组创建 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;
}

I am unable to call the get<>() on p0 from boost when I do this.当我这样做时,我无法从 boost 调用 p0 上的 get<>() 。 Can someone please point out what I am doing wrong?有人可以指出我做错了什么吗?

Problem in your program that you use pesky using namespace std;您的程序中出现的问题是您using namespace std; statement so your typedef resolves tuple as std::tuple .声明,因此您的typedef将元组解析为std::tuple Looks like you assumed that using boost::tuple;看起来你假设using boost::tuple; in main() allowed you to use one from boost, which is wrong.main()中允许您使用 boost 中的一个,这是错误的。 typedef is not a macro and name resolution there happens at time of declaration, not using it. typedef不是宏,并且名称解析发生在声明时,而不是使用它。 You can test that with removing using namespace std;您可以通过删除using namespace std; and your typedef will fail to compile:并且您的typedef将无法编译:

Live example活生生的例子

Thanks for your help.谢谢你的帮助。 Made sure std would not be an issue and it worked.确保 std 不会成为问题并且它有效。 here is the solution.这是解决方案。

#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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM