错误:与"运算符<<"不匹配(操作数类型为"std::ostream" {aka 'std::basic_ostream<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;
}
我不明白为什么运营商<lt;当我删除直方图参数处的"const"时,它不起作用,错误是:
错误:与'operator<不匹配<'(操作数类型为"std::ostream"{aka"std::basic_stream"}和"std::vector::size_type"{aka"long unsigned int"}(
我从友元函数和函数声明中的Histogram参数中删除了"const",错误消息不再存在
伟大的
但我收到错误消息"error:'std::vector Histogram::sample'在此上下文中受到保护">
无论有没有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-
相关文章:
- 使用std::multimap迭代器创建std::list
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- 错误:调用"std::vector<:vector<int>>::p ush_back(std::vector<std::__cxx11::basic_string<
- C 建造者Clang STD :: Sill,找不到超载的操作员&lt;
- 为什么STD :: MAP需要操作员&lt;以及我如何写一个
- std::vector::reserve(未知m),我知道m<<;N(通常)并且知道N
- std::vector<;uint8_t>;当C++11/14启用时,手动复制而不是调用memcpy
- C++运算符<<调用::ostream而不是std::osttream
- 是std :: set&lt; std :: future&gt;不可能存在
- 在修改列表后,std :: list&lt; t&gt; :: end()的值是否会更改
- BOOST ::变体无法解决运算符&lt;&lt;对于STD :: Ostream
- C++重载<<具有typedef'd std::vector
- 以x的倍数填充前导零,使用std::cout<<std::十六进制
- 错误:没有匹配'运算符<<"在'std::cout
- std::pair的默认构造函数<>将基本类型(int等)设置为零
- std::ostream&operator< & lt; (std:: ostream&压力,压力& &;val)
- 将std::endl传递给std::operator<<
- std::映射<>或std::vector<>在处理大型标志集时
- 重载& lt; & lt;使用命名空间std