gpt4 book ai didi

c++ - 使用Codelite IDE输入2D阵列时出现问题

转载 作者:行者123 更新时间:2023-12-03 07:39:02 25 4
gpt4 key购买 nike

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

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;
}

最佳答案

int matrix[m][n]={};是一个静态数组。静态数组在编译时解析,并且无法在运行时调整大小。通常,当您尝试执行此类操作时,编译器和编辑器(如果您具有标记此类错误的编辑器)将告诉您大小必须为常数(mn)。
您可以按照以下方式使用std::vector<std::vector<int>>来使代码正常工作,

#include <vector>

...

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

关于c++ - 使用Codelite IDE输入2D阵列时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65573347/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com