如何使用在 C++ 的类主体中的类 def 中定义的字符数组数组

How do I use an array of character arrays defined in the class def in the class body in C++?

本文关键字:数组 定义 字符 何使用 def 主体 C++      更新时间:2023-10-16

当我的设置如下所示时,如何使用字符数组数组:

在课堂上。

    namespace foomaker
    {
        class foo
        {
            const char  **mystringarray;
            bool ipitythefoo ();
        };
    }

在福.cpp #include 类.h

    namespace foomaker
    {
        bool foo::ipitythefoo()
        {
            *mystringarray[] = {"Mr. T","Gold Chains","Mohawks"};
            return false;
        };
    }

编译器抛出错误:

    1>.foofactory.cpp(5) : error C2059: syntax error : ']'
    1>.foofactory.cpp(5) : error C2143: syntax error : missing ';' before '{'
    1>.foofactory.cpp(5) : error C2143: syntax error : missing ';' before '}'

或者这甚至可能吗?

无法为此使用字符串、映射或向量。

最终结果是,这将适用于我需要通过位置编号获取的错误字符串。错误字符串特定于此类。

谢谢

你想

做什么是不可能的。您正在尝试重新初始化数组,但这无法完成。也许,鉴于您的用例是针对错误字符串的,您可以执行以下操作:

    bool foo::ipitythefoo()
    {
        static const char* actualstringarray[] = {"Mr. T","Gold Chains","Mohawks"};
        *mystringarray = actualstringarray;
        return false;
    };

但是,一开始就static一系列错误字符串会更有意义。