更多问题: 我从 mai 结构中得到一个无法识别的“字段”。
标题包含:
const int c=10;
struct Array
{
int n;
int els[c];
};
我得到的错误是:
error: request for member 'els' in 'A', which is of non-class type 'Array [(((unsigned int)(((int)a) + -0x000000001)) + 1)] {aka Array [(((unsigned int)(((int)a) + -0x000000001)) + 1)]}'
代码:
Array arrayInp()
/* Create 2 vectors by the length defined by the user*/
{
int a,b,i;
cout<<"enter length of the first array: ";
cin>>a;
cout<<"enter length of the second array: ";
cin>>b;
Array A[a],B[b];
cout<<"insert first array:";
for (int i=0;i<a;i++)
{
cin>>A.els[i];
}
cout<<"insert second array:";
for (int i=0;i<a;i++)
{
cin>>B.els[i];
}
return A,B;
}
还有一个错误,我的返回是正确的,有人可以向我解释一种如何从函数返回数组结构的方法吗? 构建错误后:
..\scr\main.cpp:32:10: warning: left operand of comma operator has no effect [-Wunused-value] ..\scr\main.cpp:32:10: error: could not convert '(0, ((Array*)(& B)))' from 'Array*' to 'Array' ..\scr\main.cpp:11:10: warning: unused variable 'i' [-Wunused-variable] ..\scr\main.cpp:33:1: warning: control reaches end of non-void function [-Wreturn-type]
最佳答案
这个:
Array A[a]
定义了一个名为A
的Array
数组。您需要索引 A
以访问 Array
的成员或(我怀疑这是您的意图)将声明更改为:
Array A, B;
只是指出可变长度数组不是标准的 C++(我认为它们是 GCC 扩展)。
return
语句:
return A,B;
正在使用 comma operator .在这种情况下,B
将返回并且是 warning: left operand of comma operator has no effect
的原因。
unused variable i
警告是提示在函数 int a, b 开头声明的
未使用,这是由于在两个 i
。 i;for
循环中重新声明了 i
:从顶部声明中删除 i
或不要在 for
循环中重新声明。
没有看到完整的源代码,我怀疑 warning: control reaches end of non-void function
是因为 int 中没有
。return 0;
主()
关于c++ - 结构错误 : Not recognized even though it is included,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9721010/