1

I have a program for adjacency matrix implementation in C++.
Here is my code-

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

In my program, by mistake, I wrote an error and compiled the program but the dev C++ compiler is not giving error for that and the program is running fine.
Here is that part-

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

I am getting the correct error message from other compilers.

  • 2
    `int adjMat[vertex][vertex];` this is invalid in C++ – phuclv Aug 20 '21 at 13:43
  • 2
    Dev c++ is not a compiler its an IDE. If you are on MS Windows your compiler used in dev c++ is likely some old version of mingw which you can replace and upgrade using msys2. [https://www.msys2.org/](https://www.msys2.org/) – drescherjm Aug 20 '21 at 13:44
  • 2
    *I am getting the correct error message from other compilers.* -- Visual C++ will refuse to compile your code due to the invalid C++ syntax `int adjMat[vertex][vertex];`. – PaulMcKenzie Aug 20 '21 at 14:31
  • @drescherjm: mingw (or msys2) isn't a compiler any more than Dev C++ is. Maybe the compiler is g++? – Ben Voigt Aug 20 '21 at 15:25

1 Answers1

5

The compiler is taking advantage of ostream's operator bool. Before C++ 11, it was implemented as operator void * and returned a void* rather than bool resulting in a syntactically legal (but logically incorrect) pointer < pointer comparison. You've found one of many good reasons the operator was replaced with the modern bool version.

The suggestions of the commenters below the question of don't use such an ancient compiler hold, but depending on the age of your Dev-C++ install, the simplest solution may be to turn on C++11 support. If your copy of Dev-C++ is too old for that, and you still want to use it, I join drescherjm in recommending you replace the compiler it came with with an up-to-date compiler from msys2. Here are good instructions on installing msys2 and its GCC.

If you want to keep using Dev C++ but don't care about the version, here's a link to the most up-to-date version that I'm aware of. It comes bundled with GCC 9.3, which is only a year old at the time of writing and supports C++17.

user4581301
  • 33,082
  • 7
  • 33
  • 54