1

I'm new to c++ and have taken the liberty to learn it this summer after coming from a python background. I was watching a video about how to create and use tuples within c++ and it seemed to have worked for the YouTuber, however when I replicated his steps, my compiler had thrown some errors even though there was no distinct differences in the code

code:

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

int main() {

    std::tuple <int, std::string> person(18, "Chris");
    std::cout << std::get<1>(person) << std::endl;

    return 0;
}

Errors:

❯ g++ -o main Tuples.cpp && ./main
Tuples.cpp:7:10: error: no member named 'tuple' in namespace 'std'
    std::tuple <int, std::string> person(18, "Chris");
    ~~~~~^
Tuples.cpp:7:20: error: expected '(' for function-style cast or type construction
    std::tuple <int, std::string> person(18, "Chris");
                ~~~^
Tuples.cpp:8:30: error: use of undeclared identifier 'person'
    std::cout << std::get<1>(person) << std::endl;
                             ^
3 errors generated.

Video for reference: https://www.youtube.com/watch?v=T9-agjKW4PQ&list=PLzMcBGfZo4-lmGC8VW0iu6qfMHjy7gLQ3&index=16

Raymond Chen
  • 44,448
  • 11
  • 96
  • 135
dfq432
  • 31
  • 4
  • 4
    Try adding `--std=c++11` as a compiler option. [Demo](https://godbolt.org/z/8acbh7v43) – rturrado Jun 28 '22 at 21:11
  • 2
    code (new version) is verified, should work (on c++11). – lorro Jun 28 '22 at 21:12
  • 2
    *I was watching a video* that's often mistake 1. Any fool can make a video, and unless you know you can trust the poster of the video or whoever recommended it to you, someone new to C++ likely won't be able to tell if they're watching the rantings of a fool or the wisdom of a master. [Prefer a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to pick up the basics of any programming language, especially one that can get as batsmurf crazy as C++ can. – user4581301 Jun 28 '22 at 21:34
  • 2
    If you need to tell GCC explicitly to use C++11, then you are using an old version of GCC (< 6.x). Try to install an up-to-date one. The most recent one is GCC 12.1. If you are using MinGW on Windows you might not find this most recent one, but something like GCC 8.x should be available at least. Also, if the compiler supports it (which newer versions definitively should), use `-std=c++14`, `-std=c++17` or `-std=c++20` to support new features of C++14, C++17 and C++20 respectively. – user17732522 Jun 28 '22 at 21:37
  • 1
    `std::cout << __cplusplus << "\n";` to see which C++ standard the code was compiled using. – Eljay Jun 28 '22 at 21:42
  • 1
    And if you are just starting then use `--std=c++20` or at least `--std=c++17`. c++11 isn't so modern anymore. – Goswin von Brederlow Jun 28 '22 at 21:43
  • 1
    For what it's worth, I wouldn't say the video shows anything woeful, other than the ubiquitous `using namespace std;` which you gladly left out. It's just that examples like these are a bit contrived, in this case you might as well define a `class Person` and obtain much clearer code. IMO, the true power of tuples is as a vehicle for generic combinations of types in template programming, which is a topic worth many hours of videos. Or a book! – sigma Jul 02 '22 at 00:14

0 Answers0