为什么会出现此错误? “[variable] does not name a type” in C++ for std::string array 即使它已被包含并且在同一范围内

Why do I get this error? "[variable] does not name a type" in C++ for std::string array even though it has been included and is in the same scope

我想制作一个用 ASCII 字符打印文本的 ASCII-Art 然后我彻底失败了。因为我已经尝试解决这个错误一个小时了,但找不到解决方案。

代码:

#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++ 新手。我认为使用多维数组是个好主意,您认为可以解决这个问题吗?我是否改用矢量?

函数外不能有“正常”语句。只有声明和定义。

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

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