簡體   English   中英

C++ 使用運算符重載時出現意外 output

[英]C++ unexpected output when using operator overloading

我正在嘗試通過“圓形”結構(基本上是二叉樹)來 go 。 每個圓都有一個 centerX、centerY、radius 和兩個葉節點。 這些葉子要么都是 null,要么都不是 null。 永遠不會有一個 null 和一個沒有 null。

我正在使用運算符重載使用 SVG 文件格式將圓圈打印到 output stream 。 以下是相關代碼:

圈子.h:

#include <set>
#include <iostream>

using namespace std;

class Circle {
 private:
  double centerX, centerY, radius;
  Circle* c1;
  Circle* c2;
  
 public:
  static const int PAGE_DIMENSION = 200;
  static const int DEFAULT_MAX_RADIUS = 15;
  
  Circle( double x, double y, double radius, Circle* r1,  Circle* r2 );
  Circle( double x, double y, double radius );
  Circle();
  
  int isLeaf() const;
  double getCenterX() const;
  double getCenterY() const;
  double area() const;
  double getRadius() const;
  Circle* getFirstSubcircle() const;
  Circle* getSecondSubcircle() const;
  
  bool operator<( Circle& other );
  Circle* operator()( double x_in, double y_in);
  Circle& operator=( Circle& rhs);
  Circle* operator,( Circle& other ); 
  double distance( Circle& rhs );

  // THESE FUNCTIONS ARE NOT CLASS MEMBERS
  // THEY ARE DEFINED OUTSIDE OF THE CLASS
  // BUT REQUIRE ACCESS TO PRIVATE FIELDS
  friend ostream& operator<<(ostream& osInput, Circle& circle);
  friend ostream& operator/(ostream& osInput, Circle& circle);
  friend Circle* reduce( set<Circle*>&  circles);
};

圈子.cpp:

#include <math.h>
#include <time.h>
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <set>
#include <fstream>
#include "circle.h"


using namespace std;

  Circle::Circle( double x, double y, double radius, Circle* r1,  Circle* r2 )
  {
      centerX = x;
      centerY = y;
      this->radius = radius;
      c1 = r1;
      c2 = r2;
  }

  Circle::Circle( double x, double y, double radius )
  {
      centerX = x;
      centerY = y;
      this->radius = radius;
  }

  Circle::Circle()
  {
      srand(time(0));
      int randomX = rand() % (PAGE_DIMENSION + 1);
      int randomY = rand() % (PAGE_DIMENSION + 1);
      int randomRadius = rand() % DEFAULT_MAX_RADIUS + 1;
      centerX = randomX;
      centerY = randomY;
      radius = randomRadius;
  }

  Circle* Circle::getFirstSubcircle() const
  {
      return c1;
  }

  Circle* Circle::getSecondSubcircle() const
  {
      return c2;
  }

  int Circle::isLeaf() const
  {
      if (c1 == NULL && c2 == NULL) {
          return 1;
      }

      return 0;
  }

  ostream& operator/(ostream& osInput, Circle& circle)
  {
      if (circle.isLeaf()) {
          osInput << " <circle cx=\"" << circle.centerX << "\" cy=\"" << circle.centerY <<"\" radius=\"" << circle.radius << "\" style=\"fill:blue;stroke:black;stroke-width:.05;fill-opacity:.1;stroke-opacity:.9\"/>\n";
      }
      else {
          osInput << " <circle cx=\"" << circle.centerX << "\" cy=\"" << circle.centerY <<"\" radius=\"" << circle.radius << "\" style=\"fill:yellow;stroke:black;stroke-width:.05;fill-opacity:.0;stroke-opacity:.5\"/>\n";
          Circle* firstCircle = circle.getFirstSubcircle();
          Circle* secondCircle = circle.getSecondSubcircle();
          osInput / *firstCircle;
          osInput / *secondCircle;
      }
  }

測試.cpp:

#include <iostream>
#include <fstream>
#include <set>
#include <string>
#include <stdlib.h>
#include "circle.h"

using namespace std;

int main( int argc, char** argv ) {
    Circle* circ2_1 = new Circle(45, 65, 3);
    Circle* circ2_2 = new Circle(56, 55, 3);
    Circle* circ2 = new Circle(11, 21, 8, circ2_1, circ2_2);
    Circle* circ3 = new Circle(7, 7, 7);
    Circle* circ4 = new Circle(10, 25, 11, circ2, circ3);
    cout / *circ4;
}

當我運行上面的代碼時,我得到以下 output:

 <circle cx="10" cy="25" radius="11" style="fill:yellow;stroke:black;stroke-width:.05;fill-opacity:.0;stroke-opacity:.5"/>
 <circle cx="11" cy="21" radius="8" style="fill:yellow;stroke:black;stroke-width:.05;fill-opacity:.0;stroke-opacity:.5"/>
 <circle cx="45" cy="65" radius="3" style="fill:blue;stroke:black;stroke-width:.05;fill-opacity:.1;stroke-opacity:.9"/>
 <circle cx="56" cy="55" radius="3" style="fill:yellow;stroke:black;stroke-width:.05;fill-opacity:.0;stroke-opacity:.5"/>

以下 output 是我所期待的(基於 main 中的代碼):

 <circle cx="10" cy="25" radius="11" style="fill:yellow;stroke:black;stroke-width:.05;fill-opacity:.0;stroke-opacity:.5"/>
<circle cx="11" cy="21" radius="8" style="fill:yellow;stroke:black;stroke-width:.05;fill-opacity:.0;stroke-opacity:.5"/>
 <circle cx="45" cy="65" radius="3" style="fill:blue;stroke:black;stroke-width:.05;fill-opacity:.1;stroke-opacity:.9"/>
 <circle cx="56" cy="55" radius="3" style="fill:yellow;stroke:black;stroke-width:.05;fill-opacity:.0;stroke-opacity:.5"/>
 <circle cx="7" cy="7" radius="7" style="fill:yellow;stroke:black;stroke-width:.05;fill-opacity:.0;stroke-opacity:.5"/>

如果我將 circle.cpp 中的“ostream& operator/”function 中的最后幾行代碼切換為以下代碼:

osInput / *secondCircle;
osInput / *firstCircle;

output 將是:

 <circle cx="10" cy="25" radius="11" style="fill:yellow;stroke:black;stroke-width:.05;fill-opacity:.0;stroke-opacity:.5"/>
 <circle cx="7" cy="7" radius="7" style="fill:yellow;stroke:black;stroke-width:.05;fill-opacity:.0;stroke-opacity:.5"/>

同樣,這不是我所期望的。 問題似乎是它只是將代碼中列出的第一個圓圈附加到 ostream,無論是首先出現的“firstCircle”還是“secondCircle”。 我認為它應該產生我期待的 output 我錯了嗎? 任何將我指向正確方向的想法將不勝感激,因為我已經嘗試了我能想到的一切。

對於 3 參數Circle構造函數,您無需初始化c1c2成員。 當您稍后嘗試取消引用這些值(可能是崩潰)時,這將是未定義的行為。

1201ProgramAlarm 的評論解決了這個問題。

暫無
暫無

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM