未声明的标识符/c++
undeclared identifier / c++
我正在尝试用 C++ 解决这个简单的问题(编译器:Visual Studio)。结果应该是您可以用给定向量的元素得出的最小和最大和,但在求和时始终排除一个元素。我的解决方案是计算所有总和,将它们放入一个向量中,对它们进行排序,然后显示第一个和最后一个元素。
我遇到的问题是程序没有执行。我收到诸如 arr / vector / miniMaxSum undeclared identifier 之类的错误...我已经包含了所有正确的 headers,所以我不明白为什么它不起作用...
更新:我的程序已修改,现在可以运行了。谢谢大家的意见:)
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
//new and working function:
void miniMaxSum(vector<int> arr) {
double minSum=0, maxSum=0;
sort(arr.begin(), arr.end());
for (int j = 0; j<arr.size()-1; j++) {
minSum = minSum + arr[j];
maxSum= maxSum + arr[j+1];
}
cout << minSum << " " << maxSum;
}
//old and not working implementation
void miniMaxSum(vector<int> arr) { //Third error here:Error C2065 'vector': undeclared identifier
vector<int> sums;
int i = 0;
while (i <= arr.size()) {
for (int j = 0; j<arr.size(); j++) {
if (i == j) { sums[i] = sums[i] + arr[j + 1]; }
else { sums[i] = sums[i] + arr[j]; }
}
i++;
}
sort(sums.begin(), sums.end());
cout << sums[0] << " " << sums[sums.size() - 1];
}
int main() {
vector<int> arr { 1,2,3,4,5 };
miniMaxSum(arr);//First error here: Error C2065 'arr': undeclared identifier / Second error also here: Error C3861 'miniMaxSum': identifier not found
}
我用 Visual Studio 检查了你的程序。它编译。
请尝试select 构建菜单中的命令“清理解决方案”,然后是“重建解决方案”
也许这对你有帮助。
另外:
但是当你运行你的程序时,会抛出异常。这是因为在 Visual Studio 的默认调试模式中,将执行越界检查。并且,如果出现问题,将抛出异常。
你这里有几个越界错误。根本原因是,您需要了解,数组从索引 0 开始。如果您有一个包含 5 个元素的数组,那么大小为 5,那么有效的 5 个索引是:0、1、2、3、4 .
如果你写 i <= arr.size()
那么 i 的最后一个值将是 5。那是越界的。并且,您访问 arr[j + 1]
。这也将越界。
所以,你的程序不能运行。
请在此处查看您的程序并附上评论,其中我发现了一个问题:
#include <iostream>
#include<vector>
#include<algorithm>
using namespace std; // Should never be used. Always use fully qualified names
void miniMaxSum(vector<int> arr) { // Vector should be passed by reference to the function
vector<int> sums; // Problem. Vector is Empty. It has no element
int i = 0;
while (i <= arr.size()) { // <= is false. Arrays in C++ start with index 0. So, this will go out of bounds
for (int j = 0; j < arr.size(); j++) {
if (i == j) {
sums[i] = sums[i] + arr[j + 1]; // Will go out of bounds for i and j
}
else {
sums[i] = sums[i] + arr[j]; // Will go out of bounds for i and j
}
}
i++;
}
sort(sums.begin(), sums.end());
cout << sums[0] << " " << sums[sums.size() - 1];
}
int main() {
vector<int> arr{ 1,2,3,4,5 };
miniMaxSum(arr);
// Return missing
}
求解题思路。我不太明白你的问题。
The result should be the smallest and largest sum you can make with the elements of a given vector, but always excluding one element when getting the sum
这似乎不适合您的实施。例子。如果一开始就对 std::vector
进行排序,那么除 1 之外的最小元素的总和将非常简单。只需对前 n-1 个元素求和即可。对于最大值就是相应的方法。
如果您明确要求,我将编辑我的答案并添加正确的解决方案。
根据我原来的理解,解决方案可能是:
#include <iostream>
#include <vector>
#include <limits>
#include <algorithm>
void showMinMaxSum(std::vector<int>& data) {
// Initialize temporary variables
int sum{}, minValue{ std::numeric_limits<int>::max() }, maxValue{ std::numeric_limits<int>::min() };;
// Calculate sum of vector elements and get may and min value
for (int temp : data) {
sum += temp;
maxValue = std::max(maxValue, temp);
minValue = std::min(minValue, temp);
}
std::cout << "Min Sum: " << sum - maxValue << " \tMax Sum: " << sum - minValue << '\n';
}
int main() {
// Test data
std::vector<int> data{ 1,2,3,4,5 };
// Calculate and show results
showMinMaxSum(data);
return 0;
}
我正在尝试用 C++ 解决这个简单的问题(编译器:Visual Studio)。结果应该是您可以用给定向量的元素得出的最小和最大和,但在求和时始终排除一个元素。我的解决方案是计算所有总和,将它们放入一个向量中,对它们进行排序,然后显示第一个和最后一个元素。 我遇到的问题是程序没有执行。我收到诸如 arr / vector / miniMaxSum undeclared identifier 之类的错误...我已经包含了所有正确的 headers,所以我不明白为什么它不起作用...
更新:我的程序已修改,现在可以运行了。谢谢大家的意见:)
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
//new and working function:
void miniMaxSum(vector<int> arr) {
double minSum=0, maxSum=0;
sort(arr.begin(), arr.end());
for (int j = 0; j<arr.size()-1; j++) {
minSum = minSum + arr[j];
maxSum= maxSum + arr[j+1];
}
cout << minSum << " " << maxSum;
}
//old and not working implementation
void miniMaxSum(vector<int> arr) { //Third error here:Error C2065 'vector': undeclared identifier
vector<int> sums;
int i = 0;
while (i <= arr.size()) {
for (int j = 0; j<arr.size(); j++) {
if (i == j) { sums[i] = sums[i] + arr[j + 1]; }
else { sums[i] = sums[i] + arr[j]; }
}
i++;
}
sort(sums.begin(), sums.end());
cout << sums[0] << " " << sums[sums.size() - 1];
}
int main() {
vector<int> arr { 1,2,3,4,5 };
miniMaxSum(arr);//First error here: Error C2065 'arr': undeclared identifier / Second error also here: Error C3861 'miniMaxSum': identifier not found
}
我用 Visual Studio 检查了你的程序。它编译。
请尝试select 构建菜单中的命令“清理解决方案”,然后是“重建解决方案”
也许这对你有帮助。
另外:
但是当你运行你的程序时,会抛出异常。这是因为在 Visual Studio 的默认调试模式中,将执行越界检查。并且,如果出现问题,将抛出异常。
你这里有几个越界错误。根本原因是,您需要了解,数组从索引 0 开始。如果您有一个包含 5 个元素的数组,那么大小为 5,那么有效的 5 个索引是:0、1、2、3、4 .
如果你写 i <= arr.size()
那么 i 的最后一个值将是 5。那是越界的。并且,您访问 arr[j + 1]
。这也将越界。
所以,你的程序不能运行。
请在此处查看您的程序并附上评论,其中我发现了一个问题:
#include <iostream>
#include<vector>
#include<algorithm>
using namespace std; // Should never be used. Always use fully qualified names
void miniMaxSum(vector<int> arr) { // Vector should be passed by reference to the function
vector<int> sums; // Problem. Vector is Empty. It has no element
int i = 0;
while (i <= arr.size()) { // <= is false. Arrays in C++ start with index 0. So, this will go out of bounds
for (int j = 0; j < arr.size(); j++) {
if (i == j) {
sums[i] = sums[i] + arr[j + 1]; // Will go out of bounds for i and j
}
else {
sums[i] = sums[i] + arr[j]; // Will go out of bounds for i and j
}
}
i++;
}
sort(sums.begin(), sums.end());
cout << sums[0] << " " << sums[sums.size() - 1];
}
int main() {
vector<int> arr{ 1,2,3,4,5 };
miniMaxSum(arr);
// Return missing
}
求解题思路。我不太明白你的问题。
The result should be the smallest and largest sum you can make with the elements of a given vector, but always excluding one element when getting the sum
这似乎不适合您的实施。例子。如果一开始就对 std::vector
进行排序,那么除 1 之外的最小元素的总和将非常简单。只需对前 n-1 个元素求和即可。对于最大值就是相应的方法。
如果您明确要求,我将编辑我的答案并添加正确的解决方案。
根据我原来的理解,解决方案可能是:
#include <iostream>
#include <vector>
#include <limits>
#include <algorithm>
void showMinMaxSum(std::vector<int>& data) {
// Initialize temporary variables
int sum{}, minValue{ std::numeric_limits<int>::max() }, maxValue{ std::numeric_limits<int>::min() };;
// Calculate sum of vector elements and get may and min value
for (int temp : data) {
sum += temp;
maxValue = std::max(maxValue, temp);
minValue = std::min(minValue, temp);
}
std::cout << "Min Sum: " << sum - maxValue << " \tMax Sum: " << sum - minValue << '\n';
}
int main() {
// Test data
std::vector<int> data{ 1,2,3,4,5 };
// Calculate and show results
showMinMaxSum(data);
return 0;
}