尝试在赋值中创建一个简单的网格类、非左值

发布于 2024-08-21 01:34:19 字数 971 浏览 6 评论 0原文

我正在实现一个简单的 C++ 网格类。它应该支持的一个函数是通过圆括号访问的,这样我就可以通过编写 mygrid(0,0) 来访问元素。我重载了 () 运算符,并且收到错误消息:“赋值中的非左值”。

我想要做什么:

//main

cGrid<cA*> grid(5, 5);

grid(0,0) = new cA();

摘录我的网格类实现:

模板 网格类 {

private:
    T* data;
    int mWidth;
    int mHeight;

public:
    cGrid(int width, int height) : mWidth(width), mHeight(height) { 
        data = new T[width*height]; 
    }

    ~cGrid() { delete data; }

    T operator ()(int x, int y)
    {
        if (x >= 0 && x <= mWidth) {
            if (y >= 0 && y <= mHeight) {
                return data[x + y * mWidth];
            }
        }
    }


    const T &operator ()(int x, int y) const
    {
        if (x >= 0 && x <= mWidth) {
            if (y >= 0 && y <= mHeight) {
                return data[x + y * mWidth];
            }
        }
    }

代码的其余部分涉及迭代器的实现,不应该是相关的。

I'm implementing a simple C++ grid class. One Function it should support is accessed through round brackets, so that I can access the elements by writing mygrid(0,0). I overloaded the () operator and i am getting the error message: "non-lvalue in assignment".

what I want to be able to do:

//main

cGrid<cA*> grid(5, 5);

grid(0,0) = new cA();

excerpt of my implementation of the grid class:

template
class cGrid
{

private:
    T* data;
    int mWidth;
    int mHeight;

public:
    cGrid(int width, int height) : mWidth(width), mHeight(height) { 
        data = new T[width*height]; 
    }

    ~cGrid() { delete data; }

    T operator ()(int x, int y)
    {
        if (x >= 0 && x <= mWidth) {
            if (y >= 0 && y <= mHeight) {
                return data[x + y * mWidth];
            }
        }
    }


    const T &operator ()(int x, int y) const
    {
        if (x >= 0 && x <= mWidth) {
            if (y >= 0 && y <= mHeight) {
                return data[x + y * mWidth];
            }
        }
    }

The rest of the code deals with the implementation of an iterator and should not be releveant.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮光之海 2024-08-28 01:34:19

正如 Bill 所说,运算符不应该是 const。我相信这就是编译错误的原因(即使报告的错误看起来不同)。编译器仅在赋值行遇到错误,因为它是模板类。

需要明确的是,您不能让 const 方法返回对非 const 的引用。即,
问题是声明 T &operator... const 是非法的。它应该是T &operator...const T &operator... const。当然,你可以两者兼得。

编辑:删除 const 没有帮助,因为现在两个方法具有相同的签名(出于解析调用的目的,返回类型不被视为签名的一部分)。被调用的方法是返回 T 的方法,而不是返回 T & 的方法。摆脱它(或将其替换为返回 const 引用的 const 方法。)

As Bill remarked, the operator shouldn't be const. I believe this is the reason for the compilation error (even if the error reported seems different). The compiler only encounters the error at the assignment line because it is a template class.

To be clear, you can't have a const method return a reference to a non-const. I.e., the
problem is that the declaration T &operator... const is illegal. It should be either T &operator... or const T &operator... const. Of course you can have both.

Edit: Removing the const doesn't help because now both methods have the same signature (the return type is not considered part of the signature for purposes of resolving the call). The method being called is the one returning T, not T &. Get rid of it (or replace it with a const method returning a const reference.)

守不住的情 2024-08-28 01:34:19

这个模板做什么:

 T &operator ()(int x, int y) const
{
    if (x >= 0 && x <= mWidth) {
        if (y >= 0 && y <= mHeight) {
            return data[x + y * mWidth];
        }
    }
}

如果 x & y 超出范围吗?你应该提出一个例外。您如何在构造函数中为网格分配内存?至于常量性,您需要提供此运算符的两个版本 - 一种返回常量引用的常量操作符,以及一种返回非常量引用的非常量操作符。

编辑:您在运算符中还存在一个相差一错误。编译并运行:

template <typename T>
struct Grid {
    Grid( int x, int y ) : mX(x), mY(y), mData(0) {
      mData = new T[ x * y ];
    }

    ~Grid() {
      delete [] mData;
    }

    T &operator ()(int x, int y)  {
        if (x >= 0 && x < mX) {
            if (y >= 0 && y < mY) {
                return mData[x + y * mX];
            }
        }
    throw "out of range" ;;
    }

    int mX, mY
    T * mData;
};


int main() {
    Grid <int> g(2,3);
    g(0,0) = 42;
}

What does this template:

 T &operator ()(int x, int y) const
{
    if (x >= 0 && x <= mWidth) {
        if (y >= 0 && y <= mHeight) {
            return data[x + y * mWidth];
        }
    }
}

do if x & y are out of range? You should raise an exception. And how are you allocating memory to the grid in the constructor? As regards constness, you need to supply two versions of this operator - a const one that returns a const reference, and a non-const one that returns a non-const reference.

Edit: You also have an off-by one error in the operator. This compiles and runs:

template <typename T>
struct Grid {
    Grid( int x, int y ) : mX(x), mY(y), mData(0) {
      mData = new T[ x * y ];
    }

    ~Grid() {
      delete [] mData;
    }

    T &operator ()(int x, int y)  {
        if (x >= 0 && x < mX) {
            if (y >= 0 && y < mY) {
                return mData[x + y * mX];
            }
        }
    throw "out of range" ;;
    }

    int mX, mY
    T * mData;
};


int main() {
    Grid <int> g(2,3);
    g(0,0) = 42;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文