【问题标题】:How can I solve this annoying error with const and reference along with STL?如何使用 const 和 reference 以及 STL 解决这个烦人的错误?
【发布时间】:2015-07-26 21:44:50
【问题描述】:

详细代码可以在here in detail找到。

Main.cpp

#include "Point2d.h"
#include "Line2d.h"
#include "Rectangle2d.h"
#include "MatrixMemory.h"
#include "Matrix.h"
#include <iostream>
#include <vector>

int main()
{
    std::vector<Point2d> points;
    std::vector<Line2d> lines;
    std::vector<Rectangle2d> rectangles;
    //std::vector<MatrixMemory> matMem;
    //std::vector<Matrix> mat;

    return 0;
}

我收到此错误消息:

1>------ Rebuild All started: Project: Matrix, Configuration: Debug Win32 ------
1>Deleting intermediate and output files for project 'Matrix', configuration 'Debug|Win32'
1>Compiling...
1>reference.fixer.cpp
1>2d.tran.cpp
1>Rectangle2d.cpp
1>Polygon2d.cpp
1>Line2d.cpp
1>Bits.cpp
1>Vector2d.cpp
1>MatrixMemory.cpp
1>.\MatrixMemory.cpp(32) : error C2662: 'MatrixMemory::GetItem' : cannot convert 'this' pointer from 'const MatrixMemory' to 'MatrixMemory &'
1>        Conversion loses qualifiers
1>Matrix.cpp
1>main.cpp
1>translation.3d.cpp
1>translation.2d.cpp
1>scaling.2d.cpp
1>rotation.2d.cpp
1>clipping.2d.weiler.atherton.cpp
1>clipping.2d.sutherland.hodgman.cpp
1>clipping.2d.midpoint.subdiv.cpp
1>clipping.2d.liang.barsky.cpp
1>clipping.2d.cohen.sutherland.cpp
1>Generating Code...
1>Build log was saved at "file://e:\Developer-Workspace\MyGraphicsLibraryImplementation\Debug\BuildLog.htm"
1>Matrix - 1 error(s), 0 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

错误信息来自以下代码段:

MatrixMemory.cpp
...................................

MatrixMemory & MatrixMemory :: operator=(const MatrixMemory  & rhs)
{
    if(rowSize!=rhs.rowSize || colSize!=rhs.colSize)
    {
        DeallocateMemory();
        AllocateMemory(rhs.rowSize, rhs.rowSize);
    }

    for(int i=0 ; i<rowSize ; i++)
    {
        for(int j=0 ; j<colSize ; j++)
        {
            this->SetItem(i, j, rhs.GetItem(i, j));
        }
    }

    return *this;
}

void MatrixMemory :: SetItem(int r, int c, double value)
{   
    if((r>=0 && r<rowSize) && rowSize>0)
    {
        if((c>=0 && c<colSize) && colSize>0)
        {
            data[r][c] = value;
        }
    }   
}

double MatrixMemory :: GetItem(int r, int c)
{
    double ret = INVALID;

    if((r>=0 && r<rowSize) && rowSize>0)
    {
        if((c>=0 && c<colSize) && colSize>0)
        {
            ret = data[r][c];
        }
    }
    return ret;
}

如您所见,不能将MatrixMemory::GetItem 声明为 const。 因为,它包含一些逻辑。

如何解决这个问题?


反对者和接近者,你能解释一下原因吗?

【问题讨论】:

  • 它包含一些逻辑,但我可以看到它不会更改任何成员变量,因此您可以在此函数上使用const 限定符。
  • 你的问题与STL无关。

标签: c++ constructor stl constants


【解决方案1】:

当您在赋值运算符中调用rhs.GetItem(i, j) 时,编译器要求rsh 是非常量。这是因为GetItem 未声明为常量成员函数。但是,rhs 被声明为常量引用,因此出现错误。

声明它const 来解决问题:

double GetItem(int r, int c) const; // line 383; also on line 466
//                           ^^^^^

这允许您在常量对象上调用GetItem 成员函数,或通过const 指针和引用。

【讨论】:

  • 但是,我没有选择将rhs 声明为非常数。 Coz,那么这将产生与重载赋值和复制 const 相关的错误。不是吗?
  • @BROY 尽管从技术上讲,您可以使rhsconst,但这不是一个好主意,因为它会严重限制您的复制构造函数的适用性。您现在拥有的复制构造函数(即带有 const 引用)是在 C++ 中创建复制构造函数的正确方法。
猜你喜欢
  • 2021-06-25
  • 1970-01-01
  • 2023-01-24
  • 2021-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-27
  • 2021-07-02
相关资源
最近更新 更多