Invalid conversion from ...<char> to int

Hi, I am imitating a queue using a linked list. I created a struct node, then I created a struct called imiQueue with some member functions and the imiQueue functions like a partially complete queue. However, when I use the ? ternary operator along with cout, it doesn't work, but using if & else does.

(if you need to run the program, enter 1 and the number you want to insert to add to the queue, enter 0 to pop out (the problematic part), and -1 to exit)

Can anyone tell me why

at line 66, (my entire code is below)

1
2
3
4
5
else if (q1==0) {
            newQueue.isEmpty() ?
            newQueue.pop_out() :
            cout << "popped out element w/ value " << newQueue.pop_out() << endl;
        }

returns error

" error: invalid user-defined conversion from 'std::basic_ostream<char>::__ostream_type {aka std::basic_ostream<char>}' to 'int' [-fpermissive]|"

even though the return type of newQueue.pop_out() returns an integer

but
1
2
3
4
5
else if (q1==0) {
            if (newQueue.isEmpty()) {
            newQueue.pop_out()}
            else {
            cout << "popped out element w/ value " << newQueue.pop_out() << endl; }

works?


My entire code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <stdlib.h>

using namespace std;

struct node {
int value;
node* next;
};

struct imiQueue{
node *bac=NULL, *fro=NULL, *curr;
int sizeQueue=0;
bool isEmpty() {
    return sizeQueue==0;
}
void push_in(int input) {
    curr=(node*)malloc(sizeof(node));
    if (bac==NULL) {
        bac=curr;
    }
    if (fro!=NULL) {
        fro->next=curr;
    }
    fro=curr;
    curr->value=input;
    sizeQueue+=1;
    curr->next=NULL;
    cout << "pushed in " << input << " as new element" << endl;
    return;
}
int pop_out() {
    if (sizeQueue==0) {
        cout << "Queue is empty, no elements to remove" << endl;
        return 0;
    }
    else {
        int output=bac->value;
        curr=bac;
        bac=bac->next;
        delete curr;
        sizeQueue-=1;
        if (sizeQueue==0) {
            fro=NULL;
        }
        return output;
    }

}
int numElements() {
    return sizeQueue;
}

};

signed int q1,valInput;

int main()
{   imiQueue newQueue;
    while(1) {
        cin >> q1;
        if (q1==1) {
            cin >> valInput;
            newQueue.push_in(valInput);
        }
        else if (q1==0) {
            newQueue.isEmpty() ?
            newQueue.pop_out() :
            cout << "popped out element w/ value " << newQueue.pop_out() << endl;
        }
        else if (q1==-1) {
            return 0;
        }

}return 0;
}
Last edited on
The expression A ? B : C is an expression, not an statement like if(A) B else C;

As any expression its goal in life is to produce a value. You can write X = A ? B : C; and it will copy B into X if A is true, and it will copy C into X if A is false.

As any expression it also has a type, because C++ is a statically-typed language, that type must be known at compile time. If the types of B and C are the same, it's easy: the type of A?B:C is that type. If the types differ, A?B:C must find a common type to which both B and C can implicitly convert to.

Now in your case:
B is newQueue.pop_out() and its type is int
C is cout << "popped out element w/ value " << newQueue.pop_out() << endl; and its type is std::ostream

There is no way to convert ostream and int to the same common type, and that's what the compiler told you (in a slightly odd way, compare to current messages from
clang "incompatible operand types ('int' and 'basic_ostream<char, std::__1::char_traits<char> >')")
and gcc " error: operands to ?: have different types 'int' and 'std::basic_ostream<char>'"

Use if/else if you don't need the *result* of ?:
Last edited on
Topic archived. No new replies allowed.