Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.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++ 在main.cpp以外的文件中定义静态数组_C++_Arrays - Fatal编程技术网

C++ 在main.cpp以外的文件中定义静态数组

C++ 在main.cpp以外的文件中定义静态数组,c++,arrays,C++,Arrays,我想要一个口袋妖怪物种结构的静态数组,数组中的每个索引都包含每个口袋妖怪的名称及其捕获率。我试图在名为SpeciesList.hpp的文件中声明和定义它 #ifndef SPECIESLIST_HPP #define SPECIESLIST_HPP #include "Species.hpp" // In which the struct species is defined as // struct species {

我想要一个口袋妖怪物种结构的静态数组,数组中的每个索引都包含每个口袋妖怪的名称及其捕获率。我试图在名为SpeciesList.hpp的文件中声明和定义它

#ifndef SPECIESLIST_HPP
#define SPECIESLIST_HPP

#include "Species.hpp" // In which the struct species is defined as 
                       // struct species {
                       //     std::string name;
                       //     int catchRate;
                       // };

static species speciesList[719];
speciesList[1].name = "Bulbasaur";
speciesList[1].catchRate = 45;
speciesList[2].name = "Ivysaur";
speciesList[2].catchRate = 45;
// Hundreds of others...

#endif // SPECIESLIST_HPP
当我没有将SpeciesList.hpp包含到main.cpp文件中时,该代码可以顺利编译。此外,当我在speciesList.hpp文件中使用上述speciesList声明,在main.cpp文件中包含speciesList.hpp文件,并在main.cpp文件中定义speciesList而不是speciesList.hpp文件时,它也会编译。但是,当我尝试使用上述声明和定义时,我会遇到以下错误

error: C++ requires a type specifier for all declarations
speciesList[1].name = "Bulbasaur"; // With speciesList underlined.

error: expected ';' after top level declarator
speciesList[1].name = "Bulbasaur"; // With the dot operator being pointed to.

error: C++ requires a type specifier for all declarations
speciesList[1].catchRate = 45; // speciesList underlined again.

error: expected ';' after top level declarator
speciesList[1].catchRate = 45; // Dot operator pointed to again.

问题在标题中。

这是因为不能使用speciesList[1]这样的表达式;在这方面。如果要在编译时定义整个数组,可以这样声明:

static const species speciesList[] = {
    { "Bulbasaur", 45 },
    // etc.
};
static species speciesList[719] = {
 {"Bulbasaur", 45},
 {"Ivysaur", 45},
 ...
 };
然而,请注意,这将Bulbasaur置于数组的索引0处

为了提高可读性和可扩展性,可以为结构定义构造函数,如下所示:

struct species {
    std::string name;
    int catchRate;

    species(const std::string &n, int c)
      : name(n), catchRate(c)
    {
    }
};
然后声明数组,如下所示:

static const species speciesList[] = {
    species("Bulbasaur", 45),
    // etc.
};

如果您有可用的C++11,可以按如下方式初始化阵列:

static const species speciesList[] = {
    { "Bulbasaur", 45 },
    // etc.
};
static species speciesList[719] = {
 {"Bulbasaur", 45},
 {"Ivysaur", 45},
 ...
 };
在C++11中查找统一初始化

如果您不能使用此功能,但可以访问Boost,您可以使用Boost分配库执行类似的操作。请注意,我现在无法检查,但这是一个想法:

static species speciesList[719] = boost::assign::array_of<species>
  ("Bulbasaur", 45)
  ("Ivysaur", 45)
  ...;