c++ - 在 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
推荐阅读
- python - 如何使用两个列表查询集过滤器?
- javascript - 如何使用 Selenium 填写响应式表单
- intellij-idea - 如何在 intellij idea 中更改左侧菜单中按钮的大小?
- javascript - puppeteer 从单独的网格中获取所有链接
- matlab - 注册点云 - Matlab
- html - React JS在滚动另一个div时隐藏div
- javascript - 将数组json发送到控制器web api c#
- flutter - 在考虑图像大小和容器大小的情况下将一些图像定位在容器内
- mongodb - Apache Zeppelin 中的 org/bson/conversions/Bson 错误
- javascript -
nextjs 中的组件替代