user13180321
user13180321

Reputation:

Unknown compiler error in cout<< theoreticlly it works

#include <iostream>
using namespace std;

void matrice(int n){


cout<<"Input the number of the elements "<<endl;
cin>>n;
int D[n][n];
cout<<"Input the elements :";
for(int i=0;i<n;i++){
  for(int j=0;j<n;j++){
        cin>>D[i][j];

  }
}
int min;
for(int i=1;i<=n;i++){
    for(int j=1;j<=n;j++){
            if( min=0>D[i-1][j-1]){
        D[i-1][j-1]=min;
    }
    }
}
cout<<" The smallest element is : "<< min<<"and it is the"<< i <<" element ."  ;
}


int main(){
int i, min ,j,n;

int n1;
    cout<<"Decide: "<<endl;
    cout<<"Matrice[1]"<<"\t"<<"Vekcor[2]"<<endl;
cin>>n1;
if(n1==1){
    matrice(n);
}
else if(n1==2){
}
}



The problem is at line 22 at the cout and it gives this message: C:\Users\use\Documents\Dev C++\void_vektor.cpp|22|error: no match for 'operator<<' (operand types are 'std::basic_ostream' and '')|

Upvotes: 0

Views: 188

Answers (3)

anastaciu
anastaciu

Reputation: 23802

The main issue is that you declare min inside the for loop, it will go out of scope as the loop exits.

The bizarre error message is likely because of the std::min function. This is a good case study on why not to use using namespace std;.

At first glance there are other issues in your code:

In the future you should use compiler warnings.

Upvotes: 0

hanie
hanie

Reputation: 1885

min is only visible in for loop scope because you have declared it inside of loop.

declared it here:

int min=D[0][0];
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            if (min > D[i - 1][j - 1])
            {
                D[i - 1][j - 1] = min;
            }
        }
    }
    cout << " Elementi me i vogel eshte : " << min;

also note that you have used uninitialized n in main and even though you will take it as input in function sending an uninitialized variable to a function might be problematic.

and also move declaration of int D[n][n]; after taking n as input.

cout<<"Input the number of the elements "<<endl;
cin>>n;
int D[n][n];

instead of your loops I suggest this which is easier:

int min=D[0][0];
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (min > D[i][j])
            {
                D[i][j] = min;
            }
        }
    }
    cout << " Elementi me i vogel eshte : " << min;

also note that if you initialize min=0 you can't find min in array in which all of elements>0. I suggest min=[0][0].

Upvotes: 1

Pete Becker
Pete Becker

Reputation: 76305

There's a fundamental problem here: the call matrice(n) in main uses the uninitialized value of n, so if your compiler supports that int D[n][n] syntax you have no idea how large the array actually is. Formally, the behavior is undefined. Informally, there's a 50/50 chance that you'll get a negative number, which doesn't make sense as an array size.

Upvotes: 0

Related Questions