简体   繁体   中英

I'm not sure why this code isn't working?

#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <tuple>
void printBoard(std::vector<std::tuple<int,int>> x) {
    for (unsigned int i = 0; i < x.size(); i++) {
        int trying = std::get<0>(x[i]);
        int num = std::get<1>(x[i]);
        std::cout << "Case #" << trying << ", Val = $" << num << std::endl; 
    }
}
int main()
{
    std::cout << 5 << std::endl;
}

I tried running this code but I am getting these errors while trying to do so:

DealOrNoDeal.cpp:7:34: error: no member named 'tuple' in namespace 'std'
void printBoard(std::vector<std::tuple<int,int>> x) {
                            ~~~~~^
DealOrNoDeal.cpp:7:43: error: expected '(' for function-style cast or type construction
void printBoard(std::vector<std::tuple<int,int>> x) {
                                       ~~~^
DealOrNoDeal.cpp:7:51: error: expected '>'
void printBoard(std::vector<std::tuple<int,int>> x) {
                                                  ^
DealOrNoDeal.cpp:7:28: note: to match this '<'
void printBoard(std::vector<std::tuple<int,int>> x) {
                           ^
DealOrNoDeal.cpp:7:34: error: no member named 'tuple' in namespace 'std'
void printBoard(std::vector<std::tuple<int,int>> x) {
                            ~~~~~^
DealOrNoDeal.cpp:7:43: error: expected '(' for function-style cast or type construction
void printBoard(std::vector<std::tuple<int,int>> x) {
                                       ~~~^
DealOrNoDeal.cpp:7:51: error: expected '>'
void printBoard(std::vector<std::tuple<int,int>> x) {
                                                  ^
DealOrNoDeal.cpp:7:28: note: to match this '<'
void printBoard(std::vector<std::tuple<int,int>> x) {
                           ^
DealOrNoDeal.cpp:8:34: error: use of undeclared identifier 'x'
    for (unsigned int i = 0; i < x.size(); i++) {
                                 ^
DealOrNoDeal.cpp:9:34: error: use of undeclared identifier 'x'
        int trying = std::get<0>(x[i]);
                                 ^
DealOrNoDeal.cpp:10:31: error: use of undeclared identifier 'x'
        int num = std::get<1>(x[i]);

Which was really strange because I never had a problem with this code segment before... I am trying to build a deal or no deal game, but all it does is bringing multiple errors. Any ideas on how I can fix this?

The errors you are getting are because you are using the wrong version of C++.

The std::tuple template was added in C++11.

To fix this error, I recommend changing the C++ version in your compiler's config. I use msys64 mingw64 g++ in VSC and can compile this code without errors.

How to install: https://code.visualstudio.com/docs/cpp/config-mingw

You can also tell clang to compile with this option: -std=c++11

Thanks anyways, I simply used this command instead to get my code working:

clang++ -std=c++11 -stdlib=libc++ DealOrNoDeal.cpp

I tried following the steps the other mentioned but couldn't do it cause microsoft windows applications cannot be opened with MacOS.

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