提问者:小点点

为什么我会得到这个错误? Std::String数组的C++中的“[variable]不命名类型”,即使它已经包含并且在相同的作用域中


我想做一个ASCII-Art打印文本与ASCII字符,然后我大规模失败。 因为我现在已经试图解决这个错误一个小时了,我没有找到解决方案。

代码:

#include <iostream>
#include <string>
class prnter {
public:
    void print(std::string text) {
        if (text == "A") {

            for (int i = 0;i < 5;i++) {
                std::cout << lett[A][i] << std::endl;
            }


        }


    }


        enum Letters {
        A, // 0
        B, // 1
        C, // 2
        D, // 3
        E, // 4
        F, // 5
        G, // 6
        H, // 7
        I, // 8
        J, // 9
        K, // 10
        L, // 11
        M, // 12
        N, // 13
        O, // 14
        P, // 15
        Q, // 16
        R, // 17
        S, // 18
        T, // 19
        U, // 20
        V, // 21
        W, // 22
        X, // 23
        Y, // 24
        Z  // 25
        };
        std::string lett[26][5];
        lett[A][0] = " _____";
        lett[A][1] = "/     \\";
        lett[A][2] = "| /_\\ |";
        lett[A][3] = "| | | |";
        lett[A][4] = "|_/ \\_|";

};


我得到了这个错误:

include\prnter.h|48|error: 'lett' does not name a type; did you mean 'getw'?|
include\prnter.h|49|error: 'lett' does not name a type; did you mean 'getw'?|
include\prnter.h|50|error: 'lett' does not name a type; did you mean 'getw'?|
include\prnter.h|51|error: 'lett' does not name a type; did you mean 'getw'?|
include\prnter.h|52|error: 'lett' does not name a type; did you mean 'getw'?|

我是C++的新手。 我以为使用多维数组会是个好主意,你认为什么能解决这个问题呢? 我用向量代替吗?


共1个答案

匿名用户

函数之外不能有“普通”语句。 只有声明和定义。

在您的情况下,您可以在定义数组时通过初始化它来解决问题:

std::string const lett[26][5] = {
    // A
    {
      " _____",
      "/     \\",
      "| /_\\ |",
      "| | | |",
      "|_/ \\_|"
    },
    // B
    { .... }
    ....
};

相关问题


MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(我会|std|string|数组|c++|中|variable|命名|类型|包含|并且在|作用|域中)' ORDER BY qid DESC LIMIT 20
MySQL Error : Got error 'repetition-operator operand invalid' from regexp
MySQL Errno : 1139
Message : Got error 'repetition-operator operand invalid' from regexp
Need Help?