简体   繁体   中英

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

I wanted to make a ASCII-Art that printed text with ASCII chars and then I massively failed. because I've been trying to solve this error for an hour now and I couldn't find a solution.

code:

#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] = "|_/ \\_|";

};


And I get this error:

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'?|

I am new to C++. I thought using multi-dimensional arrays would be a good Idea, what do you think can solve this problem? Do I use vectors instead?

You can't have "normal" statements outside of functions. Only declarations and definitions.

In your case you can solve the problem by initializing the array when you define it:

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

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