无法将变量从我的 .cpp 传递到 .h class?

Unable to pass variable from my .cpp to .h class?

我有以下设置

Square.h

#ifndef square_h
#define square_h
#include "Shape.h"
using namespace std;

class Square : public Shape
{
     public:
              Point * points = nullptr;
              int minX = 0;
              int maxX = 0;
              
              ...

              void getMinMax(Point *points, int minX, maxX)
}
# endif

Square.cpp

...

void Square :: getMinMax(Point *points, int minX, int maxX)
{
    this->minX = points[3].x;
}

...

string Square :: toString()
{
     cout << "Minimum X : " << minX << endl;
}

main.cpp

...

for(size_t i = 0; i < shapes.size(); i++)
{
    cout << shapes[i]->toString();
}

基于上面的代码片段,我在 square.cpp 中有一个函数,我试图将 minX 传回 .h minX 变量。这个想法是让这个 minX 可以在 .h 文件中声明的其他函数中使用。

然而,当我在其他函数中 cout minX 时,我总是得到 0,即使我的 points[3].x 有 > 0。

通过引用将我的 minX 传递给我的 header class 变量 minX 的正确方法是什么?

注意:.cpp 函数只是一个测试,看看我的值是否传递到.h minX

toString() 方法没有 return 任何东西。它应该 return 字符串而不是将其写入 cout。当您执行 cout << shapes[i]->toString();.

时,您将结果写入 cout
string Square :: toString()
{
    std::stringstream ret;
    ret << "Minimum X : " << minY << endl;
    return ret.str();
}