ravi
ravi

Reputation: 6328

Compilation Errors while defining vector of tuple in C++11 using GCC v4.8 on Ubuntu 14.04

I am trying to define a vector of tuples in C++ 11 as shown below:

#include <iostream>
#include <string>
#include <vector>
#include <tuple>

typedef unsigned char uchar;
typedef std::tuple<uchar, std::string, uchar, float> fruitInfoTuple;
const std::vector<fruitInfoTuple> jointsInfo{
  { 0,  "mango",   100,   -6.01},
  {10,  "apple",   144,    6.25},
  {12,  "orange",  159,    2.59},
  {33,  "banana",  144,  -28.96},
  { 4,  "grapes",  128,    3.79},
};

I compile the program with C++11 flag enabled. However, it is showing complication errors as shown below:

ravi@lab:~/Desktop/a$ g++  -std=c++11 learn.cpp 
learn.cpp:14:1: error: converting to ‘std::tuple<unsigned char, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned char, float>’ from initializer list would use explicit constructor ‘constexpr std::tuple< <template-parameter-1-1> >::tuple(_UElements&& ...) [with _UElements = {int, const char (&)[6], int, double}; <template-parameter-2-2> = void; _Elements = {unsigned char, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned char, float}]’
 };
 ^
learn.cpp:14:1: error: converting to ‘std::tuple<unsigned char, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned char, float>’ from initializer list would use explicit constructor ‘constexpr std::tuple< <template-parameter-1-1> >::tuple(_UElements&& ...) [with _UElements = {int, const char (&)[6], int, double}; <template-parameter-2-2> = void; _Elements = {unsigned char, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned char, float}]’
learn.cpp:14:1: error: converting to ‘std::tuple<unsigned char, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned char, float>’ from initializer list would use explicit constructor ‘constexpr std::tuple< <template-parameter-1-1> >::tuple(_UElements&& ...) [with _UElements = {int, const char (&)[7], int, double}; <template-parameter-2-2> = void; _Elements = {unsigned char, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned char, float}]’
learn.cpp:14:1: error: converting to ‘std::tuple<unsigned char, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned char, float>’ from initializer list would use explicit constructor ‘constexpr std::tuple< <template-parameter-1-1> >::tuple(_UElements&& ...) [with _UElements = {int, const char (&)[7], int, double}; <template-parameter-2-2> = void; _Elements = {unsigned char, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned char, float}]’
learn.cpp:14:1: error: converting to ‘std::tuple<unsigned char, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned char, float>’ from initializer list would use explicit constructor ‘constexpr std::tuple< <template-parameter-1-1> >::tuple(_UElements&& ...) [with _UElements = {int, const char (&)[7], int, double}; <template-parameter-2-2> = void; _Elements = {unsigned char, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned char, float}]’

I guess GCC 4.8 is not supporting tuple feature. Is there any workaround, please? Please note that I can use boost if needed. I just want a clean way of definig the tuple as done above.

Upvotes: 1

Views: 1190

Answers (2)

Clonk
Clonk

Reputation: 2070

In C++11 you should use std::make_tuple to construct the tuple:

#include <iostream>
#include <string>
#include <vector>
#include <tuple>

typedef unsigned char uchar;
typedef std::tuple<uchar, std::string, uchar, float> fruitInfoTuple;
const std::vector<fruitInfoTuple> jointsInfo{
  std::make_tuple( 0,  "mango",   100,   -6.01),
  std::make_tuple(10,  "apple",   144,    6.25),
  std::make_tuple(12,  "orange",  159,    2.59),
  std::make_tuple(33,  "banana",  144,  -28.96),
  std::make_tuple( 4,  "grapes",  128,    3.79),
};
int main()
{
    for(int i = 0 ; i < jointsInfo.size(); ++i)
    {
        std::cout << std::get<1>(jointsInfo[i]) << std::endl;

    }
}

Result :

mango
apple
orange
banana
grapes

Upvotes: 3

Jarod42
Jarod42

Reputation: 217145

You might try:

const std::vector<fruitInfoTuple> jointsInfo{
    fruitInfoTuple{ 0,  "mango",   100,   -6.01},
    fruitInfoTuple{10,  "apple",   144,    6.25},
    fruitInfoTuple{12,  "orange",  159,    2.59},
    fruitInfoTuple{33,  "banana",  144,  -28.96},
    fruitInfoTuple{ 4,  "grapes",  128,    3.79},
};

Upvotes: 5

Related Questions