从 Find Max 方法返回一个 int 值并在测试文件中打印该值
Returning an int value from Find Max method & printing that in test file
我已经实现了一个 findMax 方法和一个测试器来测试这个功能。之前,我将 findMax 方法设置为 void,并在该方法的末尾简单地设置了 cout << maxValue <<,以便在主测试器中调用它时,它会打印出我想要的结果。
我正在尝试更改它,使方法的 return 类型为 int,并且主要能够打印出该方法 returned 的值。当我尝试在测试文件中操作变量 maxValue 时,它说变量未定义。
我该怎么做才能解决这个问题?还有什么是最合适的方法来做到这一点?将方法作为 void 类型并在方法中包含 cout 语句或将其作为整数类型以便它 return 末尾是一个 int??
谢谢。
#ifndef FINDMAX_H
#define FINDMAX_H
#include <iostream>
using namespace std;
template < typename T >
int FindMax(T* array, int array_len) {
if (!array || array_len <=0 ) {
cout << "Invalid Array" << endl;
exit(1);
}
//T * newArray = new int[array_len]; //create new array
T maxValue = array[0]; //set to the first array element
int largestIndex = 0;
for (int i = 1; i < array_len; i++) { //going through array from pos 2
if (array[i] > maxValue) { //checking if value at array position i is > maxValue
maxValue = array[i]; //set maxValue = to element at current Array position
largestIndex = i; //set largest index = to the current index it is at
}
return maxValue;
}
//cout << "The max value in this array is: " << maxValue << endl;//return highest value in array
//cout << "The max value is at position : " << largestIndex << endl;//return position of highest value in the array
//cout << "" << endl;
}
#endif
测试人员
#include "FindMax.h"
#include <iostream>
using namespace std;
#include <string>
int main() {
int array_len = 10;
int* array = new int[array_len];
double* array2 = new double[array_len];
for (int i = 0; i < array_len; i++) //fill array 1
array[i] = i * i;
for (int i = 0; i < array_len; i++) //fill array 2
array2[i] = i * 2.5;
FindMax(array, array_len);
cout << maxValue << endl; // error here
}
首先函数有无法访问的代码
template < typename T >
int FindMax(T* array, int array_len) {
//...
return maxValue;
return largestIndex;
^^^^^^^^^^^^^^^^^^^^^^
}
//cout << "The max value in this array is: " << maxValue << endl;//return highest value in array
//cout << "The max value is at position : " << largestIndex << endl;//return position of highest value in the array
//cout << "" << endl;
}
您应该删除最后一个 return 语句。
至于错误那你应该写
int maxValue = FindMax(array, array_len);
^^^^^^^^^^^^^
cout << maxValue << endl; // error here
也就是说,您必须声明变量 maxValue
并为其分配算法的 return 值。
我已经实现了一个 findMax 方法和一个测试器来测试这个功能。之前,我将 findMax 方法设置为 void,并在该方法的末尾简单地设置了 cout << maxValue <<,以便在主测试器中调用它时,它会打印出我想要的结果。
我正在尝试更改它,使方法的 return 类型为 int,并且主要能够打印出该方法 returned 的值。当我尝试在测试文件中操作变量 maxValue 时,它说变量未定义。
我该怎么做才能解决这个问题?还有什么是最合适的方法来做到这一点?将方法作为 void 类型并在方法中包含 cout 语句或将其作为整数类型以便它 return 末尾是一个 int??
谢谢。
#ifndef FINDMAX_H
#define FINDMAX_H
#include <iostream>
using namespace std;
template < typename T >
int FindMax(T* array, int array_len) {
if (!array || array_len <=0 ) {
cout << "Invalid Array" << endl;
exit(1);
}
//T * newArray = new int[array_len]; //create new array
T maxValue = array[0]; //set to the first array element
int largestIndex = 0;
for (int i = 1; i < array_len; i++) { //going through array from pos 2
if (array[i] > maxValue) { //checking if value at array position i is > maxValue
maxValue = array[i]; //set maxValue = to element at current Array position
largestIndex = i; //set largest index = to the current index it is at
}
return maxValue;
}
//cout << "The max value in this array is: " << maxValue << endl;//return highest value in array
//cout << "The max value is at position : " << largestIndex << endl;//return position of highest value in the array
//cout << "" << endl;
}
#endif
测试人员
#include "FindMax.h"
#include <iostream>
using namespace std;
#include <string>
int main() {
int array_len = 10;
int* array = new int[array_len];
double* array2 = new double[array_len];
for (int i = 0; i < array_len; i++) //fill array 1
array[i] = i * i;
for (int i = 0; i < array_len; i++) //fill array 2
array2[i] = i * 2.5;
FindMax(array, array_len);
cout << maxValue << endl; // error here
}
首先函数有无法访问的代码
template < typename T >
int FindMax(T* array, int array_len) {
//...
return maxValue;
return largestIndex;
^^^^^^^^^^^^^^^^^^^^^^
}
//cout << "The max value in this array is: " << maxValue << endl;//return highest value in array
//cout << "The max value is at position : " << largestIndex << endl;//return position of highest value in the array
//cout << "" << endl;
}
您应该删除最后一个 return 语句。
至于错误那你应该写
int maxValue = FindMax(array, array_len);
^^^^^^^^^^^^^
cout << maxValue << endl; // error here
也就是说,您必须声明变量 maxValue
并为其分配算法的 return 值。