c++ - 编译器声称变量不明确,不会运行

标签 c++ arrays

我的编译器 (VS 2015) 声称“int rents[size]”行包含 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";

}

最佳答案

您声明了一个名为“size”的常量和一个名为“size”的参数。

改为:

const int RentsMaxSize = 6;
int rents[RentsMaxSize];

[阅读错误消息解释了这一点]

此外,请注意数组在 C++ 中是基于零的

关于c++ - 编译器声称变量不明确,不会运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32686456/

相关文章:

c++ - Qt、小部件刷新和 ui 交互性

php - 如何在没有ajax的情况下将数组从javascript传递到php?

ios - 在结构或其他方法中全局存储数组

c++ - 在 Visual Studio 2013 中链接 NuGet 库

Javascript 字符串转换为 'Title Case' 不返回任何输出

java - 在 Java 中将字符串数组转换为 boolean 列表

javascript - 来自地址数组的多个标记 Google Map API v3 并在 pageLoad 上进行地理编码时避免 OVER_QUERY_LIMIT

c++ - 使用 VS2015 更改应用程序的入口点

c++ - kd-tree 构建非常慢

java - 任何编程语言中的无理数表示?