二进制'=':在VS2010中找不到使用'gcroot *'类型的右侧操作数的错误运算符 [英] binary '=' : no operator found which takes a right-hand operand of type 'gcroot *' error with VS2010

查看:119
本文介绍了二进制'=':在VS2010中找不到使用'gcroot *'类型的右侧操作数的错误运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个使用Visual Studio 2010的Windows窗体应用程序项目.话虽这么说:

我有一个不受管理的向量"实现,例如:

This is a Windows Forms Application project using Visual Studio 2010. That being said:

I''ve an unmanaged ''Vector'' implementation, something like:

template<class T>
class Vector {
public:
    T* data;
    unsigned len;

    (...constructors and operators overloading like =, [], == and !=)
};



效果很好.我之前已经做过几次测试.之后,我实现了一个非托管的矩阵",该矩阵使用以前的Vector类.像这样的东西:



It works great. I''ve done several tests before with it. After that I''ve implemented an also unmanaged ''Matrix'' that uses the previously class Vector. Something like:

template<class T>
class Matrix : public Vector<Vector<T>*> {

public:
    Matrix(unsigned height = 20, unsigned width = 20)
    : Vector(height)
    {
    for (unsigned i = 0; i < height; i++) {
            data[i] = new Vector<T>(width);
        }
    }

    Vector<T> operator[](unsigned pos) {
        return data[pos];
    }
};



它也很好用,但是使用非托管数据作为T.我的主要目标是使用[] []访问数据,按预期方式工作.

但是现在我想在托管代码中使用这个Matrix类,数据T也是托管代码,在这种情况下,已经实现了"Cell":



It also works great but with unmanaged data as T. My main objective, access data using [][], worked as expected.

But now I want to use this Matrix class inside managed code, with data T also being managed code, in this case a ''Cell'' already implemented:

public ref class Cell {
public:
    bool dead;

public:
    Cell() {
        this->dead = false;
    }
    Cell(bool dead) {
        this->dead = dead;
    }

    virtual String^ ToString() override {
        return (this->dead ? "0" : "1");
    }
};



我正在以这种方式进行初始化:



I''m doing the initialization this way:

Matrix<gcroot<Cell^>>* cells = new Matrix<gcroot<Cell^>>(height, width);

for (unsigned i = 0; i < height; i++) {
    for (unsigned j = 0; j < width; j++) {
        gcroot<Cell^> * cell = new gcroot<Cell^>;
        cells[i][j] = cell; // ERROR!!
    }
}



我将单元格分配给矩阵的行始终会在标题中返回错误.其他运算符(除了"="之外)也给我同样的错误.

我已经尝试了所有方法,但找不到解决此问题的方法.我真的避免接触Vector和Matrix类,因为它们必须不受管理,而且我必须使用gcroot在其中注入托管数据.

归根结底,Cell将是一个System :: Windows :: Forms :: Button,因此实现必须采用这种方式.

有任何想法吗?对不起,如果我无法解释自己.如果您需要Vector类的更多代码,请告知.谢谢:(



The line where I assign the cell to matrix always returns the error in the title. Other operators (aside from ''='') also gives me the same error.

I''ve tried everything but I can''t find a way to fix this. I''m really avoiding to touch Vector and Matrix classes because they''ve to be unmanaged and I''ve to use gcroot to inject managed data inside it.

At the end of the day, Cell will be a System::Windows::Forms::Button so the implementation has to be this way.

Any ideas? Sorry if I was unable to explain myself. If you need more code from Vector class please tell. Thanks is advanced :)

推荐答案

用于实例化cells的模板类型是gcroot<Cell^>,但是cell的类型是.更改cells的模板类型以匹配您想要的.顺便说一句,我不确定使用空的gcroot实例将实现什么,因为这种想法是让它们指向有效的托管对象(它们不在您的示例中).


---------

这是一些示例代码(根据您的评论).我声明了虚拟骨骼类,但核心思想是相同的:

The template type you''ve used for instantiating cells is gcroot<Cell^>, but the type of cell is gcroot<Cell^>*. Change the template type of cells to match what you want. BTW, I am not sure what you will achieve with empty gcroot instances, since the idea is for them to point to a valid managed object (which they are not in your example).


---------

Here''s some sample code (in response to your comment). I declared dummy skeletal classes, but the core idea is same:

template<typename T> class Matrix
{
    T t[100];

public:
    T& Matrix::operator[] (int index)
    {
        return t[index];
    }
};

ref class Cell{};

int main(array<System::String ^> ^args)
{
    Matrix<gcroot<Cell^>*> matrix;
    matrix[0] = new gcroot<Cell^>();
    Console::ReadKey();
    return 0;
}



请注意,此代码将编译并运行.



Note that this code compiles and runs.


这篇关于二进制'=':在VS2010中找不到使用'gcroot *'类型的右侧操作数的错误运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆