[英]Pass array of structs by reference
I would like to pass an array of structs to a function.我想将一个结构数组传递给 function。
struct Month {
std::string name;
int days;
};
Month months[12]{{"January", 31}, {"February", 28}, {"March", 31}, {"April", 30}, {"May", 31}, {"June", 30},
{"July", 31}, {"August", 31}, {"September", 30}, {"October", 31}, {"November", 30}, {"December", 31}};
I have tried to pass the months array to functions with the parameter: Month (&months)[]我试图将月份数组传递给带有参数的函数:Month (&months)[]
The compiler noted: "no known conversion for argument 1 from 'main()::Month [12]' to 'Month (&)[12]'".编译器指出:“没有已知的参数 1 从 'main()::Month [12]' 到 'Month (&)[12]' 的转换”。
I then attempted to pass the array by a pointer and to do this I first allocated the elements of the months array on the free-store as shown below.然后我尝试通过指针传递数组,为此我首先在自由存储区分配了月份数组的元素,如下所示。
Month* months[12]{new Month{"January", 31}, new Month{"February", 28}, new Month{"March", 31}, new Month{"April", 30}, new Month{"May", 31}, new Month{"June", 30},
new Month{"July", 31}, new Month{"August", 31}, new Month{"September", 30}, new Month{"October", 31}, new Month{"November", 30}, new Month{"December", 31}};
I tried to pass this array to a function with the parameter: Month**我尝试将此数组传递给 function 参数:Month**
The compiler stated: "no known conversion for argument 1 from 'main()::Month* [12]' to 'Month**'"编译器声明:“没有已知的参数 1 从 'main()::Month* [12]' 到 'Month**' 的转换”
I would like to know how I could pass my months array by pointer and if it is possible to pass it by reference as well.我想知道如何通过指针传递我的月份数组,以及是否可以通过引用传递它。
And yes, I do know that using a vector would be a lot easier but I just started learning C++ a few days ago and I would like to become familiarized with the arrays.是的,我知道使用向量会容易得多,但我几天前才开始学习 C++,我想熟悉 arrays。
The function itself is empty, I cannot write the function without knowing what my parameter will be. function 本身是空的,我无法在不知道我的参数是什么的情况下编写 function。
void print_table(Month** months) {
}
And the function call is: function 调用是:
print_table(months)
After removing all extraneous code I was left with this:删除所有无关代码后,我得到了这个:
struct Month;
void print_table(Month** months);
int main() {
struct Month {
std::string name;
int days;
}
Month* months[12]{new Month{"January", 31}, new Month{"February", 28}, new Month{"March", 31}, new Month{"April", 30}, new Month{"May", 31}, new Month{"June", 30},
new Month{"July", 31}, new Month{"August", 31}, new Month{"September", 30}, new Month{"October", 31}, new Month{"November", 30}, new Month{"December", 31}};
print_table(months);
}
The issue was explained in the comments by john, I have also posted the corrected code.约翰在评论中解释了这个问题,我还发布了更正的代码。
Since you will treat the content of Month
as constant, simply pass a rvalue reference, eg Month*&& m
.由于您将
Month
的内容视为常量,因此只需传递一个右值引用,例如Month*&& m
。 Example:例子:
void showmonths (Month*&& m, size_t nmonths)
{
for (size_t i = 0; i < nmonths; i++)
std::cout << std::left << std::setw(12) << m[i].name << m[i].days << '\n';
}
For further details on the declaration and use of references, see: Reference declaration有关引用的声明和使用的更多详细信息,请参阅:引用声明
After reading john's comment I realized that the issue was that my struct was declared inside of the main function as shown here:阅读约翰的评论后,我意识到问题在于我的结构是在主 function 内部声明的,如下所示:
struct Month;
void print_table(Month** months);
int main() {
struct Month {
std::string name;
int days;
}
Month* months[12]{new Month{"January", 31}, new Month{"February", 28}, new Month{"March", 31}, new Month{"April", 30}, new Month{"May", 31}, new Month{"June", 30},
new Month{"July", 31}, new Month{"August", 31}, new Month{"September", 30}, new Month{"October", 31}, new Month{"November", 30}, new Month{"December", 31}};
print_table(months);
}
I edited to declare the struct outside of the main function and it worked.我编辑以在主 function 之外声明结构,它起作用了。 Thanks!
谢谢!
struct Month {
std::string name;
int days;
}
void print_table(Month** months);
int main() {
Month* months[12]{new Month{"January", 31}, new Month{"February", 28}, new Month{"March", 31}, new Month{"April", 30}, new Month{"May", 31}, new Month{"June", 30},
new Month{"July", 31}, new Month{"August", 31}, new Month{"September", 30}, new Month{"October", 31}, new Month{"November", 30}, new Month{"December", 31}};
print_table(months);
}
Try this尝试这个
Month months[10];
//like this you can pass
print_table(months);
Pass to this function传递给这个 function
void print_table(struct Month * months)
{
//code here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.