我的编译器(VS 2015)声称“int rents [size]”行包含大小,这是不明确的,不会运行程序。
我做了无数的修改,无法实现至少接受变量大小的版本,因为它不是模棱两可的。我对c ++的极端细节并不是很有经验,并希望得到一些关于我收到此错误的原因的指导。谢谢!
控制台中出现错误:
1>c:\users\**\documents\visual studio 2015\projects\arrays and pointers\arrays and pointers\source.cpp(9): error C2872: 'size': ambiguous symbol
1> c:\users\**\documents\visual studio 2015\projects\arrays and pointers\arrays and pointers\source.cpp(8): note: could be 'const int size'
1> c:\users\**\documents\visual studio 2015\projects\arrays and pointers\arrays and pointers\source.cpp(9): note: or 'size'
下面的代码
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdio>
using namespace std;
const int size = 6;
int rents[size];
void sortArray(int *rents, const int size);
int main()
{
bool run_again = true;
// Define Numbers
int num0;
int num1;
int num2;
//Define Pointers
int *ptrnum0;
int *ptrnum1;
int *ptrnum2;
// Set Pointers
ptrnum0 = &num0;
ptrnum1 = &num1;
ptrnum2 = &num2;
int choice;
do
{
cout << "Main Menu:\n";
cout << "1.) Display Numbers\n";
cout << "2.) Display First and Last Rent Locations.\n";
cin >> choice;
if (choice == 1)
{
while (run_again)
{
int num;
cout << "Int 0: ";
cin >> num0;
cout << endl;
cout << "Int 1: ";
cin >> num1;
cout << endl;
cout << "Int 2: ";
cin >> num2;
cout << endl;
run_again = false;
}
cout << "num0 == " << num0 << endl;
cout << "num1 == " << num1 << endl;
cout << "num2 == " << num2 << endl << endl;
cout << "&Numbers: Addresses: \n\n";
cout << "&num0 == " << &num0 << endl;
cout << "&num1 == " << &num0 << endl;
cout << "&num2 == " << &num0 << endl << endl;
cout << "*ptrnum0 == " << ptrnum0 << endl;
cout << "*ptrnum1 == " << ptrnum1 << endl;
cout << "*ptrnum2 == " << ptrnum2 << endl << endl;
// Sort Entered numbers
}
// SHow First and last
if (choice == 2)
{
}
system("pause");
return 0;
} while (choice < 3);
}
void sortArray(int *rents, const int size) {
bool swap;
int temp;
int count = 0;
do
{
swap = false;
for (count = 0; count < (size - 1); count++) {
if (*(rents + count) < *(rents + count + 1))
{
temp = rents[count];
*(rents + count) = rents[count + 1];
*(rents + count + 1) = temp;
swap = true;
}
}
} while (swap);
for (count = 0; count < size; count++)
cout << *(rents + count) << " ";
cout << "\n";
}
答案 0 :(得分:1)
你声明了一个名为&#39; size&#39;并作为名为&#39; size&#39;。
的参数更改为:
const int RentsMaxSize = 6;
int rents[RentsMaxSize];
[阅读错误信息解释了这一点]
另外,请注意数组在C ++中为零