簡體   English   中英

錯誤:'operator&lt;&lt;' 不匹配(操作數類型是 'std::ostream' {aka 'std::basic_ostream<char> '}</char>

[英]error: no match for ‘operator<<’ (operand types are ‘std::ostream’ {aka ‘std::basic_ostream<char>’}

monte_carlo.hpp 上的代碼是:

class Histogramme{
    protected:
        std::vector<double> echantillon;
        unsigned int nb_boxes;
        double lbound;
        double ubound;
        double box_width;
    public:
        Histogramme(double min_intervalle, double max_intervalle, unsigned int n) : nb_boxes(n), lbound(min_intervalle),
        ubound(max_intervalle), box_width((max_intervalle - min_intervalle)/n),echantillon(n) {}
        Histogramme& operator+=(double x);
        Histogramme& operator/=(double n);
        friend std::ostream& operator<<(std::ostream&, const Histogramme &);
};

在 monte_carlo.cpp 中:

std::ostream& operator<<(std::ostream& o,const Histogramme& H){
    for(int i=0; i<H.echantillon.size(); i++){ o << H.echantillon.size() << std::endl;}
    return o;
}

我不明白為什么當我在 Histogramme 參數中刪除“const”時運算符 << 不起作用,錯誤是:

錯誤:'operator<<' 不匹配(操作數類型是 'std::ostream' {aka 'std::basic_ostream'} 和 'std::vector::size_type' {aka 'long unsigned int'})

我從朋友 function 和 function 聲明中的直方圖參數中刪除“const”,錯誤消息不再存在

偉大的

但我收到錯誤消息“錯誤:'std::vector Histogram::sample' is protected within this context”

無論有沒有const ,我都沒有收到任何錯誤。 這是我寫的:

#include <iostream>
#include <vector>
//using namespace std;

class Histogramme {
protected:
    std::vector<double> echantillon;
    unsigned int nb_boxes;
    double lbound;
    double ubound;
    double box_width;
public:
    Histogramme(double min_intervalle, double max_intervalle, unsigned int n) : 
        nb_boxes(n), lbound(min_intervalle),
        ubound(max_intervalle), box_width((max_intervalle - min_intervalle) / n), echantillon(n) {}
    Histogramme& operator+=(double x)
    {
        ;
    }
    Histogramme& operator/=(double n)
    {
        ;
    }
    friend std::ostream& operator<<(std::ostream&, Histogramme&);
};

std::ostream& operator<<(std::ostream& o, Histogramme& H) {
    for (int i = 0; i < H.echantillon.size(); i++)
    {
        o << H.echantillon.size() << std::endl;
    }
    return o;
}

int main()
{
    Histogramme example(0.3, 34.2, 5);

    std::cout << example << std::endl;
    return 0;
}

https://godbolt.org/z/HaQQR-

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 錯誤:“operator&lt;&lt;”不匹配(操作數類型為“std::ostream {aka std::basic_ostream”<char> }&#39; 和 &#39;void&#39;) 錯誤:'operator&lt;&lt;' 不匹配(操作數類型是 'std::ostream {aka std::basic_ostream<char> }'和'列表')</char> 錯誤:'operator<<' 不匹配(操作數類型為'std::ostream {aka std::basic_ostream<char> }' 和 'std::ostream {aka std::basic_ostream<char> }')</char></char> 'operator&lt;&lt;' 不匹配(操作數類型是 'std::ostream {aka std::basic_ostream<char> }' 和 '分數')</char> 與&#39;operator &lt;&lt;&#39;不匹配(操作數類型為&#39;std :: ostream {aka std :: basic_ostream <char> }” 錯誤:'operator&lt;&lt;' 不匹配(操作數類型是 'std::ostream' {aka 'std::basic_ostream<char> '} 和 'std::_List_iterator<int> ')</int></char> 錯誤:“operator&lt;&lt;”不匹配(操作數類型為“std::basic_ostream”<char> &#39; 如何修復錯誤:'operator&lt;&lt;' 不匹配(操作數類型為 'std::ostream {aka std::basic_ostream<char> }' 和 'void')同時使用字符串和堆棧</char> \\ main.cpp | 103 |錯誤:“ operator &lt;&lt;”不匹配(操作數類型為“ std :: ostream {aka std :: basic_ostream <char> }”和“人”) 'operator&lt;&lt;' 不匹配(操作數類型為 'std::ostream' {aka 'std::basic_ostream<char> '} 和 'const std::type_index')</char>
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM