Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何为字符串数组c+初始化字符指针数组+;_C++_Arrays_Initialization - Fatal编程技术网

C++ 如何为字符串数组c+初始化字符指针数组+;

C++ 如何为字符串数组c+初始化字符指针数组+;,c++,arrays,initialization,C++,Arrays,Initialization,因此,我必须填充一个单词数组,以便以后可以访问它们。名词={“男孩”、“女孩”、“房子”}等等。问题是我必须使用字符点数组。我已经试过了,但是它抛出了一个错误,说error-too-many initializer值。这是密码 class Sentence { private: char* article[sz]; char* verb[sz]; char* preposition[sz]; char* noun[sz]; 后来我像这样调用构造函数,但它

因此,我必须填充一个单词数组,以便以后可以访问它们。名词={“男孩”、“女孩”、“房子”}等等。问题是我必须使用字符点数组。我已经试过了,但是它抛出了一个错误,说error-too-many initializer值。这是密码

class Sentence
{
    private:
    char* article[sz];
    char* verb[sz];
    char* preposition[sz];
    char* noun[sz];
后来我像这样调用构造函数,但它没有填充它们

Sentence::Sentence()
{
    article[10]  = { "the", "a", "one", "some", "any" };
    verb[] = { "drove", "jumped", "ran", "walked", "skipped" };
    preposition[] = { "to", "from", "over", "under", "on" };
    noun[] = { "boy ", "girl", "dog", "town", "car" };
}

如果需要初始化此类固定大小的数组,可以在构造函数的成员初始值设定项列表中执行此操作。在构造函数的主体中不可能这样做

假设
sz
被声明为

static const size_t sz = 10; 
您可以使用以下语法使用构造函数初始化它们

Sentence::Sentence() 
: article{ "the", "a", "one", "some", "any" }
, verb{ "drove", "jumped", "ran", "walked", "skipped" }
, preposition{ "to", "from", "over", "under", "on" }
, noun { "boy ", "girl", "dog", "town", "car" } {
}

P>考虑弗拉德的回答中提到的使用,或者甚至如R. Sahu所提到的那样。其中任何一个都应该接受此初始值设定项语法。

如果
sz
是某个常量,则可以编写

#include <array>

//...

const size_t sz = 5;

class Sentence
{
    private:
    std::array<const char *, sz> article;
    std::array<const char *, sz> verb;
    std::array<const char *, sz> preposition;
    std::array<const char *, sz> noun;

//...

Sentence::Sentence()
{
    article  = { "the", "a", "one", "some", "any" };
    verb = { "drove", "jumped", "ran", "walked", "skipped" };
    preposition = { "to", "from", "over", "under", "on" };
    noun = { "boy ", "girl", "dog", "town", "car" };
}
#包括
//...
const size_t sz=5;
类别句
{
私人:
std::数组文章;
数组动词;
std::数组介词;
std::数组名词;
//...
句子::句子()
{
第{“一”、“一”、“一些”、“任何”}条;
动词={“开车”、“跳”、“跑”、“走”、“跳过”};
介词={“to”,“from”,“over”,“under”,“on”};
名词={“男孩”、“女孩”、“狗”、“城镇”、“汽车”};
}
否则,您可以使用
std::vector
而不是数组

#include <vector>

//...


class Sentence
{
    private:
    std::vector<const char *> article;
    std::vector<const char *> verb;
    std::vector<const char *> preposition;
    std::vector<const char *> noun;

//...

Sentence::Sentence()
{
    article  = { "the", "a", "one", "some", "any" };
    verb = { "drove", "jumped", "ran", "walked", "skipped" };
    preposition = { "to", "from", "over", "under", "on" };
    noun = { "boy ", "girl", "dog", "town", "car" };
}
#包括
//...
类别句
{
私人:
std::矢量文章;
向量动词;
向量介词;
向量名词;
//...
句子::句子()
{
第{“一”、“一”、“一些”、“任何”}条;
动词={“开车”、“跳”、“跑”、“走”、“跳过”};
介词={“to”,“from”,“over”,“under”,“on”};
名词={“男孩”、“女孩”、“狗”、“城镇”、“汽车”};
}
你说过:

我必须填充一个单词数组,以便以后可以访问它们

如果这是核心需求,那么最好使用
std::vector
而不是
char*
数组

class Sentence
{
    public:
        Sentence();

    private:
        std::vector<std::string> articles;
        std::vector<std::string> verbs;
        std::vector<std::string> prepositions;
        std::vector<std::string> nouns;
};

您只需在成员变量上使用
push_back
,即可将项添加到集合中。

sz定义在哪里?@Perrin Hawver何时何地设置sz?此外,您还试图静态初始化数组中的索引。这没有意义。去掉10和方括号。sz设置在程序顶部,尽管我把它硬编码为5,它抛出了相同的错误。@Perrinhawer如果你想在这里问这样的问题,你应该提供一个,并在你的问题中发布准确的错误消息。
Sentence::Sentence() : articles{ "the", "a", "one", "some", "any" },
                       verbs{ "drove", "jumped", "ran", "walked", "skipped" },
                       prepositions{ "to", "from", "over", "under", "on" },
                       nouns{ "boy ", "girl", "dog", "town", "car" }
{
}