我想在头文件中定义一个字符数组:
#define name char[5]
然后像这样在结构中使用这个定义:
struct dog{
name nameOfDog;
int ageOfDog;
};
但它使我出现以下错误:
"Brackets are not allowed here; to declare an array, place the brackets after the name"
是否有另一种方法可以将其设置为正确的语法?
谢谢!
最佳答案
对于 C++ 中的数组,使用 std::array
#include <array>
#include <string>
struct dog
{
std::array<char,5> name;
unsigned int age;
};
std::string a_string{"Hello"};
虽然我不会使用数组来命名名字,但我会使用 std::string
关于c++ - 在 C++ 中定义数组并在结构中使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68933615/