【问题标题】:I'm having issues while using codelite ide to enter a 2D array我在使用 codelite ide 输入二维数组时遇到问题
【发布时间】:2021-01-05 04:39:47
【问题描述】:

我在 Codelite IDE 中输入二维数组时遇到问题。构建日志是-

C:\Windows\system32\cmd.exe /C ""C:/Program Files/mingw-w64/mingw64/bin/mingw32-make.exe" -j8 SHELL=cmd.exe -e -f 
Makefile" "----------Building project:[ test - Debug ]----------"
mingw32-make.exe[1]: Entering directory
'C:/Users/CHIRAYU/Documents/CODING/PRACTICE/array/test' "C:/Program
Files/mingw-w64/mingw64/bin/g++.exe"  -c 
"C:/Users/CHIRAYU/Documents/CODING/PRACTICE/array/test/main.cpp" -g
-O0 -std=c++17 -Wall  -o Debug/main.cpp.o -I. -I. during RTL pass: expand C:/Users/CHIRAYU/Documents/CODING/PRACTICE/array/test/main.cpp:
In function 'int main()':
C:/Users/CHIRAYU/Documents/CODING/PRACTICE/array/test/main.cpp:10:9:
internal compiler error: in make_decl_rtl, at varasm.c:1322
     int matrix[m][n]={};
         ^~~~~~ libbacktrace could not find executable to open Please submit a full bug report, with preprocessed source if appropriate. See
<https://sourceforge.net/projects/mingw-w64> for instructions.
mingw32-make.exe[1]: *** [test.mk:98: Debug/main.cpp.o] Error 1
mingw32-make.exe[1]: Leaving directory
'C:/Users/CHIRAYU/Documents/CODING/PRACTICE/array/test'
mingw32-make.exe: *** [Makefile:5: All] Error 2
====1 errors, 0 warnings====

使用的代码是-

//entering a matrix of user input size and displaying it 
#include <iostream>
using namespace std;

int main(){
    int m{},n{}; //m- number of rows; n - number of columns
    cout<<"Enter the number of rows and columns separated by space "<<endl;
    cin>> m >> n;
    
    int matrix[m][n]={};
    
    for(int i{0}; i<m; i++){
        for(int j{0}; j<n; j++){
            cout<<"Enter the "<<i<<" x "<<j<<" element"<<endl;
            cin>>matrix[i][j];
        }
    }
    
    cout<<"The entered matrix is: "<<endl;
    for(int a{0}; a<n; a++){
        for(int b{0}; b<n; b++){
            cout<<matrix[a][b]<<" ";
        }
        cout<<endl;
    }
    cout << endl;
    return 0;
}

【问题讨论】:

    标签: c++ arrays compiler-errors mingw


    【解决方案1】:

    int matrix[m][n]={}; 是一个静态数组。静态数组在编译时解析,不能在运行时调整大小。当您通常尝试执行此类操作时,编译器和编辑器(如果您有一个标记此类错误的编辑器)会告诉您大小必须是一个常量(mn)。

    您可以使用std::vector&lt;std::vector&lt;int&gt;&gt; 使代码工作,如下所示,

    #include <vector>
    
    ...
    
    std::vector<std::vector<int>> matrix(m, std::vector<int>(n));
    

    【讨论】:

    • 但是相同的代码是如何被其他 IDE 运行的呢?
    • @Chirayu mn 的默认值为 0(在编译时和运行时初始化它)。所以matrix 的大小是int matrix[0][0];。当您尝试执行此类操作时,某些 IDE(即:Visual Studio)会发出错误。有些 IDE 没有。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-27
    • 2015-04-25
    • 2014-01-25
    • 2021-12-29
    • 2021-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多