![](/img/trans.png)
[英]error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘void’)
[英]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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.