error: no match for operator<< in std::operator<< <std::char_traits<char> >(*&std::cout),((const char*)

StackOverflow https://stackoverflow.com/questions/23177726

  •  06-07-2023
  •  | 
  •  

Question

error: no match for operator<< in std::operator<< <std::char_traits<char> >(*&std::cout),((const char*)


#include <iostream>
using namespace std;

int ContarDig (int num){
    int contar=1;
    while (num>9){
        num=num/10;
        contar=contar+1;
    }
    return contar;
}
void arreglo(int a[], int t){
    for(int i=(ContarDig(t)-1); i>=0; i--){
        a[i]=t%10;
        t=t/10;
        cout <<"valor posicion[" << i << "]= "<<a[i]<<endl;
    }
}
void invertir(int A[],int x){
    int l=(ContarDig(x)/2);
    int i=0;
    int f=ContarDig(x);
    for(l;l>=0;l--){
        int b=A[i];
        A[i]=A[f];
        A[f]=b;
        i++;
        f--;
    }
    for (i;i<=ContarDig(x);i++){
        cout <<"valor posicion[" << i << "]= "<<A[i]<<endl;
    }
}
int main(int argc, char *argv[]) {
    int x;
    cout<<"Digite un numero para invertir"<<endl;
    cin>>x;
    int A[ContarDig(x)];
    arreglo (A,x);
    cout<<"========================================================================"<<endl;
    cout<< "El arreglo invertido es:" << invertir(A,x) << endl;
    return 0;
}

So, I would like to know how to solve my problem, its objective is to have an array and invert it.

#include <iostream>
using namespace std;

int ContarDig (int num){
    int contar=1;
    while (num>9){
        num=num/10;
        contar=contar+1;
    }
    return contar;
}
void arreglo(int a[], int t){
    for(int i=(ContarDig(t)-1); i>=0; i--){
        a[i]=t%10;
        t=t/10;
        cout <<"valor posicion[" << i << "]= "<<a[i]<<endl;
    }
}
void invertir(int A[],int x){
    int l=(ContarDig(x)/2);
    int i=0;
    int f=ContarDig(x)-1;
    for(l;l>=0;l--){
        int b=A[i];
        A[i]=A[f];
        A[f]=b;
        if (ContarDig(x)==2)
            break;
        i++;
        f--;
    }
}
int main(int argc, char *argv[]) {
    int x;
    cout<<"Digite un numero para invertir"<<endl;
    cin>>x;
    int A[ContarDig(x)];
    arreglo (A,x);
    cout<<"========================================================================"<<endl;
    cout<< "El arreglo invertido es:" <<endl;
    invertir(A,x);
    for (int i=(ContarDig(x)-1);i>=0;i--){
        cout <<"valor posicion[" << i << "]= "<<A[i]<<endl;
    }
    return 0;
}

This is the new version of my program and now it runs perfectly.

Was it helpful?

Solution

The error related to the title stems from this line:

cout<< "El arreglo invertido es:" << invertir(A,x) << endl;

The function invertir () returns void, and can't be printed. To the compiler, that's like writing std:: cout <<;, which is invalid. Change the return type of the function to one appropriate, or, simply don't try to print it. Call the function and allow it to print normally (since there's calls to std:: cout:: operator << () in the function).

You should attempt to interpret these errors that you get. Although the errors you get for such simple mistakes span pages (I got 194 lines of errors), changing just one line of code fixes it all. Don't be intimidated, check the line number you are given, and look around that code for simple mistakes.

My error looked like this, and a whole bunch more.

test92.cpp: In function ‘int main(int, char**)’:
test92.cpp:41:39: error: no match for ‘operator<<’ 
  (operand types are ‘std::basic_ostream<char>’ and ‘void’)

That's all you need.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top