Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 !=)
};


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];
    }
};


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!!
    }
}


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 :)
Posted

1 solution

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).

[Edit]
---------

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

MC++
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.
 
Share this answer
 
v2
Comments
JoaoPe 1-Nov-10 16:15pm    
You mean something like this?

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

The error is exactly the same.

What do you mean by "empty gcroot instances" and where am I using them?
Nish Nishant 1-Nov-10 17:31pm    
I've updated my answer with a small code snippet.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900