【问题标题】:When I am pass the array as arguments then I am faceing this error invalid types 'int[int]' in c++错误:C++ 中数组下标的无效类型“int [int]”
【发布时间】:2022-01-05 00:46:20
【问题描述】:

这是我的代码,我想尝试将我的数组与数字进行比较,但我给了我一些错误 基本上,我正在尝试通过线性搜索找到一个数字并在 C++ 中打印该数字的索引

   // linear search 
    #include <iostream>
    using namespace std;
    
    int search(int arr, int n, int x)
    {
        int i=0;
        for (i = 0; i < n; i++){
            if (arr[i] == x){
                return i;}
        }
        return -1;
    }
    
    // Driver code
    int main(void)
    {
        int size;
        int temp;
        cout << "Enter Size of Arry";
        cin >> size;
        int  arr[size];
        for(int i=0;i<size;i++){
            cout << "Enter a " << i << "Element of your arry : ";
            cin >> temp;
            arr[i]=temp;
    
        }
        
    
         cout << "Enter the number that you will find index";
            int x;
         cin >> x;
        // Function call
        int result = search(arr, size, x);
        (result == -1)
            ? cout << "Element is not present in array"
            : cout << "Element is present at index " << result;
        return 0;
    }

这是错误

PS C:\Users\talha\OneDrive\Desktop\Study_Material\DSA_lab\cs201149_lab1_3C> g++ Q1.cpp -o Q11.exe
Q1.cpp: In function 'int search(int, int, int)':
Q1.cpp:9:12: error: invalid types 'int[int]' for array subscript
   if (arr[i] == x){
            ^
Q1.cpp: In function 'int main()':
Q1.cpp:35:34: error: invalid conversion from 'int*' to 'int' [-fpermissive]
  int result = search(arr, size, x);
                                  ^
Q1.cpp:5:5: note:   initializing argument 1 of 'int search(int, int, int)'
 int search(int arr, int n, int x)
     ^~~~~~
PS C:\Users\talha\OneDrive\Desktop\Study_Material\DSA_lab\cs201149_lab1_3C> 

【问题讨论】:

  • arr 不是int search(int arr, int n, int x) 中的数组,而是一个整数。

标签: c++ arrays compiler-errors function-declaration


【解决方案1】:

对于像这样的初学者可变长度数组

int  arr[size];

不是标准的 C++ 功能。相反,在 C++ 程序中使用标准容器 std::vector&lt;int&gt; 会更好。

其次,函数声明不正确。第一个参数声明为 int 类型。

int search(int arr, int n, int x)

你需要通过以下方式声明函数

int search(int arr[], int n, int x)

或等效

int search(int *arr, int n, int x)

注意 C++ 中有标准算法std::find 执行在容器中搜索元素。

【讨论】:

    【解决方案2】:

    首先在C++中,数组的大小必须是编译时常数。所以,以下面的代码sn-ps为例:

    int n = 10;
    int arr[n]; //INCORRECT because n is not a constant expression
    

    上面的正确写法是:

    const int n = 10;
    int arr[n]; //CORRECT
    

    同样,以下(您在代码示例中所做的)不正确:

        int size;
        int temp;
        cout << "Enter Size of Arry";
        cin >> size;
        int  arr[size];//INCORRECT because variable size is not a constant  expression
    

    您应该使用std::vector 来达到您的目的,如下所示:

    #include <iostream>
    #include <vector>
    using namespace std;
    //the first argument is a vector<int>
    int search(std::vector<int> arr, int x)
    {
        int i=0;
        for (i = 0; i < arr.size(); i++){//use size()member function
            if (arr[i] == x){
                return i;}
        }
        return -1;
    }
    
    // Driver code
    int main(void)
    {
        int size;
        int temp;
        cout << "Enter Size of Arry";
        cin >> size;
        //create a vector instead of an array 
        std::vector<int> arr(size);
        for(int i=0;i<arr.size();i++){//use the size member function
            cout << "Enter a " << i << "Element of your arry : ";
            cin >> temp;
            arr[i]=temp;
    
        }
        
    
         cout << "Enter the number that you will find index";
            int x;
         cin >> x;
        // Function call
        int result = search(arr,  x);//no need to pass the size of the vector
        (result == -1)
            ? cout << "Element is not present in array"
            : cout << "Element is present at index " << result;
        return 0;
    }
    
    

    请注意,您也可以使用std::find,而不是编写自己的搜索算法。

    【讨论】:

    • 谢谢,兄弟帮了很大忙,但是编译后,我的程序,关闭,你能解释一下为什么会发生这种情况
    • @TalhaIqbal 不客气。您能否确定您对程序的哪个点(行)有进一步的疑问。从您上次的评论中不清楚您到底在问什么。你能澄清一下你还有什么疑问(如果有的话)吗?
    • int search(std::vector&lt;int&gt; arr, int x) 变量 arr 可能应该通过 const 引用传递。
    • @drescherjm 是的,理想情况下它应该作为对 const 的引用传递。但我只是想演示如何使用/传递vector 而不是数组,并且不想让他感到困惑(以防他不知道引用)。另外,由于您在吹毛求疵,我也会这样做。从技术上讲,C++ 中 没有 const references。技术上更正确的术语是对 const 的引用。
    猜你喜欢
    • 2021-05-04
    • 1970-01-01
    • 1970-01-01
    • 2015-09-26
    • 2013-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多