AUserMadeMe
AUserMadeMe

Reputation: 15

no operator ""<<"" matches these operands error

I keep getting this error "no operator ""<<"" matches these operands error", but I can't understand why. I checked many solutions on the web but nothing worked.Here is my code

#include<iostream>
#include<string>
using namespace std;

void BubbleSort(int data[], int Size)
{   int temp;
    bool sorted = false; // false when swaps occur
    for (int pass = 1; (pass < Size) && !sorted; ++pass)
    { 
        sorted = true; // assume sorted
        for (int x = 0; x < Size-pass; ++x)
        { 
            if (data[x] > data[x+1])
            { 
                // exchange items
                temp = data[x];
                data[x] = data[x+1];
                data[x+1] = temp;
                sorted = false; // signal exchange
            } // end if
        } // end for
    } // end for
} // end bubbleSort

int main()
{
    const int size = 5;
    int arr[] = {1,2,3,4,5};
    cout<<BubbleSort(arr, size)<<endl;


    system ("pause");
    return 0;
}

Upvotes: 0

Views: 886

Answers (1)

wally
wally

Reputation: 11022

void BubbleSort(int data[], int Size) doesn't return anything.

So << doesn't have anything to try and output.

Rather create an operator<< for ostream and arr[] if you would like to use cout <<.

#include<iostream>
#include<string>
using namespace std;

void BubbleSort(int data[], int Size)
{   int temp;
    bool sorted = false; // false when swaps occur
    for (int pass = 1; (pass < Size) && !sorted; ++pass)
    { 
        sorted = true; // assume sorted
        for (int x = 0; x < Size-pass; ++x)
        { 
            if (data[x] > data[x+1])
            { 
                // exchange items
                temp = data[x];
                data[x] = data[x+1];
                data[x+1] = temp;
                sorted = false; // signal exchange
            } // end if
        } // end for
    } // end for
} // end bubbleSort

template < typename T,size_t N >
ostream& operator<<(ostream& os,T const (&array)[N])
{
    for (auto element : array)
        os << element << ' ';
    return os;
}

int main()
{
    const int size = 5;
    int arr[] = {1,2,3,4,5};

    BubbleSort(arr,size);
    //cout<<BubbleSort(arr, size)<<endl;
    cout << arr << '\n';


    system ("pause");
    return 0;
}

You could keep main() unchanged if you pass the array by reference back and forth so that it doesn't decay into a pointer. In that case, with templates, you don't have to pass the size explicitly as the compiler will deduce it for you.

#include<iostream>
#include<string>
using namespace std;


template < typename T,size_t Size >
T (&BubbleSort(T (&data)[Size]))[Size] 
{   int temp;
    bool sorted = false; // false when swaps occur
    for (int pass = 1; (pass < Size) && !sorted; ++pass)
    { 
        sorted = true; // assume sorted
        for (int x = 0; x < Size-pass; ++x)
        { 
            if (data[x] > data[x+1])
            { 
                // exchange items
                temp = data[x];
                data[x] = data[x+1];
                data[x+1] = temp;
                sorted = false; // signal exchange
            } // end if
        } // end for
    } // end for
    return data;
} // end bubbleSort

template < typename T,size_t N >
ostream& operator<<(ostream& os,T const (&array)[N])
{
    for (auto element : array)
        os << element << ' ';
    return os;
}

int main()
{
    //const int size = 5; // not needed anymore
    int arr[] = {1,2,3,4,5};

    cout<<BubbleSort(arr)<<endl;

    system ("pause");
    return 0;
}

Upvotes: 1

Related Questions