求一数组程序解释(有个函数看不懂)解决思路
求一数组程序解释(有个函数看不懂)
这个应该是递归调用吧,但是有个参数没搞明白,还有这个程序在vs2012上运行通不过,程序是看书上写的,
错误提示:
错误 1 error LNK2019: 无法解析的外部符号 "void __cdecl someFunction(int * const,int)" (?someFunction@@YAXQAHH@Z),该符号在函数 "void __cdecl someFuntion(int * const,int)" (?someFuntion@@YAXQAHH@Z) 中被引用 D:\ex\8\源.obj 8
错误 2 error LNK1120: 1 个无法解析的外部命令 D:\ex\8\Debug\8.exe 8
------解决方案--------------------
someFunction( &b[1], size - 1 ); //这里看不懂, &b[1] ?
&b[1]取数组b中第一个元素的地址,你的函数的第一个参数要求是指针.
------解决方案--------------------
#include <iostream>
using namespace std;
void someFunction( int [], int );
int main()
{
const int arraySize = 10;
int a[ arraySize ] = { 32, 27, 64, 18 ,95, 14, 90, 70, 60, 37 };
cout << "The values in the array are: " << endl;
someFunction( a, arraySize );
cout << endl;
system("pause");
return 0;
}
void someFuntion( int b[], int size )
{
if ( size > 0 )
{
someFunction( &b[1], size - 1 ); //这里看不懂, &b[1] ?
cout << b[0] << " ";
}
}
这个应该是递归调用吧,但是有个参数没搞明白,还有这个程序在vs2012上运行通不过,程序是看书上写的,
错误提示:
错误 1 error LNK2019: 无法解析的外部符号 "void __cdecl someFunction(int * const,int)" (?someFunction@@YAXQAHH@Z),该符号在函数 "void __cdecl someFuntion(int * const,int)" (?someFuntion@@YAXQAHH@Z) 中被引用 D:\ex\8\源.obj 8
错误 2 error LNK1120: 1 个无法解析的外部命令 D:\ex\8\Debug\8.exe 8
------解决方案--------------------
someFunction( &b[1], size - 1 ); //这里看不懂, &b[1] ?
&b[1]取数组b中第一个元素的地址,你的函数的第一个参数要求是指针.
------解决方案--------------------
#include <iostream>
#include<string>
#include<vector>
#include<cstring>
using namespace std;
void someFunction(int[], int );
int main()
{
const int arraySize = 10;
int a[arraySize] = { 32, 27, 64, 18 ,95, 14, 90, 70, 60, 37 };
cout << "The values in the array are: " << endl;
someFunction(a,arraySize );
cout << endl;
system("pause");
return 0;
}
void someFunction(int *p,int size )
{
if ( size > 0 )
{
someFunction(p+1, size - 1 ); //这样你应该好理解吧,跟你的程序完全等价
cout << *p << " ";
}
}