繁体   English   中英

我不确定为什么这段代码不起作用?

[英]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;
}

我尝试运行此代码,但在尝试这样做时遇到了这些错误:

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]);

这真的很奇怪,因为我之前从未遇到过这个代码段的问题......我正在尝试建立一个交易或不交易的游戏,但它所做的只是带来多个错误。 关于如何解决这个问题的任何想法?

您遇到的错误是因为您使用了错误的 C++ 版本。

在 C++11 中添加了std::tuple模板。

要修复此错误,我建议更改编译器配置中的 C++ 版本。 我在 VSC 中使用 msys64 mingw64 g++ 并且可以无错误地编译此代码。

如何安装: https ://code.visualstudio.com/docs/cpp/config-mingw

您还可以告诉 clang 使用此选项进行编译: -std=c++11

无论如何,谢谢,我只是使用这个命令来让我的代码工作:

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

我尝试按照其他提到的步骤进行操作,但无法使用 MacOS 打开 Microsoft Windows 应用程序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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