dev dev c++不检测错误,只使用一个尖括号与cout?

  • 本文关键字:dev 一个 cout c++ 错误 c++ dev-c++
  • 更新时间 :
  • 英文 :


我有一个用c++实现邻接矩阵的程序。
这是我的代码-

#include<iostream>
using namespace std;
void addEdge(int * add){
cout<<"Edge added successfully.n";
*add = 1;
}
int main(){

int vertex;

cout<<"Enter number of vertices in the graph: ";
cin>>vertex;

int adjMat[vertex][vertex];

for(int i=0;i<vertex;i++){
for(int j=0;j<vertex;j++){
adjMat[i][j] = 0;
}
}

cout<<"n";
cout<<"Add edges (from and to): ";
int from,to;
while(cin>>from>>to){
if((from >= vertex || from < 1) || (to >= vertex || to < 1)){
cout<"Warning: Out of bound index.n";
}else{
addEdge(&adjMat[--from][--to]);
}
}

cout<<"n";
for(int i=0;i<vertex;i++){
for(int j=0;j<vertex;j++){
cout<<adjMat[i][j]<<" ";
}
cout<<"n";
}

return 0;
}

在我的程序中,由于错误,我写了一个错误并编译了程序,但开发c++编译器没有给出错误,程序运行良好。
这是那部分-

while(cin>>from>>to){
if((from >= vertex || from < 1) || (to >= vertex || to < 1)){
cout<"Warning: Out of bound index.n";
}else{
addEdge(&adjMat[--from][--to]);
}
}

我从其他编译器得到正确的错误信息。

编译器正在利用ostreamoperator bool。在c++ 11之前,它被实现为operator void *,并返回void*而不是bool,导致一个语法上合法(但逻辑上不正确)的指针<指针的比较。您已经找到了将操作符替换为现代bool版本的众多原因之一。>

下面的评论建议不要使用如此古老的编译器的问题,但根据您的开发- c++安装的年龄,最简单的解决方案可能是打开c++ 11支持。如果您的dev - c++版本太旧了,而您仍然想使用它,我和drescherjm一起建议您用msys2的最新编译器替换它附带的编译器。下面是关于安装msys2及其GCC的一些很好的说明。

如果你想继续使用Dev c++,但不关心版本,这里有一个链接到我所知道的最新版本。它与GCC 9.3捆绑在一起,在撰写本文时,GCC 9.3只有一年的历史,并支持c++ 17。

相关内容

最新更新