简体   繁体   中英

C++: TypeDef using Tuples

I am attempting to create a typedef using tuples here is my code.

#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. Can someone please point out what I am doing wrong?

Problem in your program that you use pesky using namespace std; statement so your typedef resolves tuple as std::tuple . Looks like you assumed that using boost::tuple; in main() allowed you to use one from boost, which is wrong. typedef is not a macro and name resolution there happens at time of declaration, not using it. You can test that with removing using namespace std; and your typedef will fail to compile:

Live example

Thanks for your help. Made sure std would not be an issue and it worked. 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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