没有操作员""<<""匹配这些操作数错误

时间:2016-05-06 14:13:46

标签: visual-c++

我不断收到此错误"没有操作员""<<""匹配这些操作数错误",但我不明白为什么。我在网上检查了很多解决方案,但没有任何效果。这是我的代码

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

1 个答案:

答案 0 :(得分:1)

void BubbleSort(int data[], int Size)不返回任何内容。

所以<<没有任何可以尝试和输出的内容。

如果您想使用operator<<,请为ostreamarr[]创建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;
}

如果你来回传递数组,那么你可以保持main()不变,这样它就不会衰变成指针。在这种情况下,使用模板,您不必显式传递大小,因为编译器会为您推断它。

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