c++ - 在 C++ 中定义数组并在结构中使用它

标签 c++ arrays

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

#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/

相关文章:

c++ - 无效的堆参数 : allocated with operator new[], 通过运算符(operator)删除释放

jquery - 如何使用 jQuery 从数组中删除特定值

c++ - 动态捕获 CPU 和内存使用情况

c++ - 64 位溢出数学转换

arrays - 可以是标量或数组的值的技术术语是什么?

C 数字母函数

javascript - Stomp 客户端订阅创建新数组 javascripts

c++ - 如何为 LookAt 转换获取正确的 Up vector ?

python - for 循环 : C++ and python

c++ - cout 一个带有类定义的变量