在c++中定义数组并在struct中使用它

我想在头文件中定义字符数组:

#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++中定义数组并在struct中使用它的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>