我收到3个错误:
请帮我解决这个问题。这是代码:
#include<iostream>
int power(int x[5])
{
x[0]=12;
x[1]=23;
x[2]=234;
x[3]=344;
x[4]=232;
return x;
}
int main()
{
int action[5]={1,2,3,,4,5};
std::cout<<x[0]<<std::endl;
x();
std::cout<<x[0]<<std::endl;
return 0;
}
你的帮助将不胜感激!
答案 0 :(得分:2)
首先,错误是非常自我解释的。
从int *到int
的无效转换
您的函数以预期返回int
的方式声明。不是int
的数组,只是int
。此外,您无法从函数返回数组。然而,可以返回指向第一个元素的指针,在您的情况下是不必要的。
猜测您希望函数只是在数组中设置值,您可以通过将函数声明为void
返回来实现:
void power(int x[5])
{
x[0]=12;
x[1]=23;
x[2]=234;
x[3]=344;
x[4]=232;
}
x未在此范围内声明
嗯,鉴于你的主要:
int main()
{
int action[5]={1,2,3,,4,5};
std::cout<<x[0]<<std::endl;
// ^
...
}
在这里,您尝试使用变量x
,它从未在main
内声明或作为全局变量,因此编译器不知道您所指的是什么。在一些不相关的函数中简单地将参数别名为x
不会使其对所有代码都可见。您的不能像这样使用它。
在','标记
之前的预期主要表达
仔细查看您的main
功能和action
声明。部分:
int action[5]={1,2,3,,4,5};
// ^^
是非法的。请注意,,
。您应该在它们之间放置一个整数,或者删除其中一个。
您可能希望实现的目的是首先声明数组,打印出第一个元素,应用power()
函数并再次打印第一个元素,希望它能够改变。鉴于我所写的power()
的声明,你可以这样做:
#include <iostream>
void power(int x[5])
{
x[0]=12;
x[1]=23;
x[2]=234;
x[3]=344;
x[4]=232;
}
int main()
{
int x[5] = {1,2,3,4,5};
std::cout << x[0] << ' ';
power(x);
std::cout << x[0] << std::endl;
}
输出: 1 12
。
答案 1 :(得分:0)
第一个错误是一个简单的错误声明:函数接收指针并返回一个指针,因此返回类型为int *
,而不仅仅是int
。顺便说一句,我喜欢函数返回指针,因为它允许函数嵌套,如下面的代码所示(函数调用的结果可以是另一个函数的直接参数)。
请注意,您声明的参数x
是一个指针,尽管有外观。可以用两种方式声明指针参数:
f ( int *p );
或
f2( int p[] );
两者完全相同;将数组传递给函数是不可能的。它们总是被“调整”到指向第一个元素的指针。
事实上,人们甚至可以写
f2( int p[100] );
我在下面的代码中演示。这个数字是无关紧要的。特别是,编译器认为没有数组,并且它没有对实际参数进行类型或索引检查。
现在你提出的原始功能是不安全的;它完全取决于调用者提供一个包含5个元素的数组。
C ++有可能传递引用,甚至是对数组的真正引用。不同大小的数组具有不同的类型,即使元素属于同一类型。这使得引用数组的普通函数变得毫无用处(除非你有一个类似于屏幕缓冲区的应用程序,其中所有数组都具有相同的大小,在编码时已知)。
但是C ++也有模板,它是在编译时从提供的模式构造函数的工具,具体取决于调用者在编译时提供的类型和int参数(但不一定是在模板编码时!)。对于函数,这些模板参数可以由编译器从函数参数中推断出来,这使得模板函数可以方便地使用:编译器几乎神奇地创建了正确的模板函数。
迭代固定大小数组的所有元素的函数是模板的主要候选者。查看下面代码中的最后一个函数。
#include<iostream>
using namespace std; // for brevity in the example
/** An unsafe function based on passing pointers
without element counts.
*/
int *power_5_elems(int x[100]) {
// This is somewhat scary.
// There is no way to check whether the array
// starting at x has indeed 5 elements. It's
// like writing assembler: All is up to the caller.
// Another thing: It would be nice to use a loop.
// We know there are 5 elements, right?
x[0] = x[0] * x[0];
x[1] = x[1] * x[1];
x[2] = x[2] * x[2];
x[3] = x[3] * x[3];
x[4] = x[4] * x[4];
return x;
}
// A reference to an int array with 5 elements.
// Remember, a typedef is written like a variable declaration; the
// type name takes the syntactic place of the variable.
// To decipher it, solve the expression in the parentheses first:
// "I must first de-reference the variable".
// The next operator is the square brackets index operator:
// "I must index the result of the dereferencing" (i.e. it is an array).
// There are 5 indices (0..4).
// The last information is: "The resulting type is int."
// Summed up: This is a reference to an array with 5 elements of type int.
typedef int(&intArr5)[5];
/** This is a nicer way to declare the function below. */
intArr5 power_ref5Arr(intArr5 arrRef);
/** A safe function with very limited usability.
It works only for int arrays with 5 elements.
This is the raw declaration of the same function.
*/
int (&power_ref5Arr( int (&x)[5] ))[5] {
// This is not scary at all.
// Only arrays with 5 ints can be supplied.
for (int i = 0; i < 5; i++)
{
x[i] = x[i] * x[i];
}
return x;
}
/** A versatile function which squares arrays
of arbitrary length of any type that isn't on
a tree on the count of three. It's very similar to the
function above with 5 int elements.
*/
template<int N, typename T>
T(&refPower( T (&arr)[N] ))[N]
{
// Note the use of the template parameter N
// in place of the "5" above.
for (int i = 0; i < N; i++)
{
arr[i] = arr[i] * arr[i];
}
return arr;
}
int main() {
int x[5] = { 2,3,4,5,6 };
int *p = &x[0]; // make clear: this is a pointer
power_5_elems(power_5_elems(power_5_elems(x)));
cout << x[0] << endl;
// The function actually expects a pointer!
// The pointer points to x.
power_5_elems(p);
cout << x[0] << endl;
int y[2] = { 2,3 };
refPower(refPower(refPower(y)));
cout << y[0] << endl;
double z[2] = { 0.2 , 0.3 };
refPower(refPower(refPower(z)));
cout << z[0] << endl;
return 0;
}