带有std :: ostream运算符的XOR运算符

时间:2016-11-29 11:43:49

标签: c++ operators xor ostream

我写了一个代表Qubit的课程。因此,对象只有一个值,state,0或1(bool)。为了进行必要的计算,我重载了+,*,^等运算符。 似乎一切都可以用+和*,也可以用^,但前提是我不能在std :: ostream操作符中使用它。

<html> 
 <head> 
    <meta charset="utf-8">         
    <title>a bar</title> 
 </head> 
 <style>
    .axis path,
    .axis line{
        fill: none;
        stroke: black;
        shape-rendering: crispEdges;
    }

    .axis text {
        font-family: sans-serif;
        font-size: 11px;
    }

    .MyRect {
        fill: steelblue;
    }

    .MyText {
        fill: white;
        text-anchor: middle;
    }
 </style>
    <body>
        <script src="http://d3js.org/d3.v4.min.js" charset="utf-8"></script>
        <script>
        var width=400;  
        var height=400; 

        var svg=d3.select("body")  
                  .append("svg") 
                  .attr("width",width)  
                  .attr("height",height); 

        var padding = {left:30, right:30, top:20, bottom:20};

        var dataset=[10,20,30,40,33,24,12,5];  


        var xScale = d3.scaleBand()
                       .domain(d3.range(dataset.length))
                       .range([0, width-padding.left-padding.right])
                       .padding(0.2);  //some value here 


        var yScale = d3.scaleLinear()
                       .domain([0,d3.max(dataset)])
                       .range([height-padding.top-padding.bottom,0]);

        var xAxis = d3.axisBottom()
                      .scale(xScale);         

        var yAxis = d3.axisLeft()
                      .scale(yScale);

        var rectPadding=4;  

        var rects = svg.selectAll(".MyRect")
            .data(dataset)
            .enter()
            .append("rect")
            .attr("class","MyRect")
            .attr("transform","translate(" + padding.left + "," + padding.top + ")")
            .attr("x", function(d,i){
                return xScale(i) + rectPadding/2;
            } )
            .attr("y",function(d){
                return yScale(d);
            })
            .attr("width", xScale.bandwidth() - rectPadding )
            .attr("height", function(d){
            return height - padding.top - padding.bottom - yScale(d);
            })
            .attr("fill","steelblue")       
            .on("mouseover",function(d,i){
                d3.select(this)
                    .attr("fill","yellow");
            })
            .on("mouseout",function(d,i){
                d3.select(this)
                    .transition()
                    .duration(500)
                    .attr("fill","steelblue");
            });
        var texts = svg.selectAll(".MyText")
                       .data(dataset)
                       .enter()
                       .append("text")
                       .attr("class","MyText")
                       .attr("transform","translate(" + padding.left + "," + padding.top + ")")
                       .attr("x", function(d,i){
                                return xScale(i) + rectPadding/2;
                        })
                       .attr("y",function(d){
                                return yScale(d);
                        })
                        .attr("dx",function(){
                                return (xScale.bandwidth() - rectPadding)/2;
                        })
                       .attr("dy",function(d){
                                return 20;
                        })
                       .text(function(d){
                                return d;
                        })

        svg.append("g")
           .attr("class","axis")
           .attr("transform","translate(" + padding.left + "," + (height - padding.bottom) + ")")
           .call(xAxis); 


        svg.append("g")
           .attr("class","axis")
           .attr("transform","translate(" + padding.left + "," + padding.top + ")")
           .call(yAxis);
        </script>
    </body> 
</html>

但是

Qubit x5, x6;
cout << x5^x6; !ERROR!
它正在工作。我的标准:操作员

Qubit x5, x6;
Qubit z = x5^x6;
cout << z;

和我的XOR运算符

std::ostream & operator <<(std::ostream & os, const Qubit & qubit)
{
    os << qubit.GetState();
    return os;
}

1 个答案:

答案 0 :(得分:7)

由于运算符优先级

cout << x5 ^ x6被评估为(cout << x5) ^ x6

由于您没有为ostream&Qubit(或const Qubit&等)提供重载的XOR运算符,因此编译失败。

解决方案是编写cout << (x5 ^ x6);

(请注意,+*运算符的优先级高于<<,这就是它们按照您的描述工作的原因。)

最后,您在XOR运算符中遇到了严重的内存泄漏(谁将要delete分配的内存?)。修复此问题,方法是更改​​函数以返回值副本:

Qubit Qubit::operator^(const Qubit& qubit) const

并在函数体中使用Qubit q;命名返回值优化将避免值复制。有关详细信息,请参阅http://en.cppreference.com/w/cpp/language/operator_arithmetic