0
votes

I've been having a lot of trouble making an array that is accessible to all the functions in my class in C++/CLI. Since it's C++/CLI, std::vector doesn't work. Boost::array throws an error about unmanaged types being used with managed types. I don't want to use a pointer array because I want to get the size of the array. System::List is too slow (every ms matters in my program, but if it helps, I don't care about write speeds to the array, only read speeds of one element at a time). cliext::vector was the closest I got to getting this to work.

I created a single dimensional cliext::vector with an int, and it worked. However, when I tried to use

cliext::vector<cliext::vector<int>> test;

it failed with a similar error as the one below. Here is how I used it in my class:

The header:

cliext::vector<Color> test;

I set values for it in the constructor:

test = gcnew cliext::vector<Color>(5);
test[0] = Color(255,255,255);

I then tried to make a class that would store 3 variable for color. Here is the header file. The constructor just sets the r,g,b values:

namespace FrameCalculator {
class Color {
public:
    Color(int r, int g, int b);

    int r;
    int g;
    int b;
};

However this didn't work, and it threw the error below:

1>E:\Microsoft Visual 
Studio\2017\Community\VC\Tools\MSVC\14.11.25503\include\cliext\vector(1091): 
note: see reference to class template instantiation 
'cliext::impl::vector_base<_Value_t,false>' being compiled
1>        with
1>        [
1>            _Value_t=FrameCalculator::Color
1>        ]`

and

1>E:\Microsoft Visual 
Studio\2017\Community\VC\Tools\MSVC\14.11.25503\include\cliext\vector(615): 
error C3671: 'cliext::impl::vector_impl<_Value_t,false>::SyncRoot::get': 
function does not override 'System::Collections::ICollection::SyncRoot::get'
1>        with
1>        [
1>            _Value_t=FrameCalculator::Color
1>        ]`

There were about 5 of each of these errors. What am I doing wrong? How do I get a 2d array that's not slow, is globally accessible. I don't need both arrays to be dynamic (I'd prefer them not to). I know the inner array will have 3 elements, but I won't know the outer array size at compile time. How do I achieve this?

1
The cliext classes are quite notorious for being considerably slower than the framework collection classes. Like List. So easy solution, stop using it. - Hans Passant
@HansPassant I was not aware of that. What do you suggest I use? I think I am going to try using a different class for unmanaged types. Do you have any other suggestions? - Byte11
This hinges on "List is too slow" without any evidence by how much it is too slow. There is a vast difference between making code 20% faster and making it five time faster. There is only one decent reason why such an essential detail is missing, you have no clue whether it is really too slow, you just assume it might be. It is not slow. So make it work first, always infinitely better than code that doesn't work, then find out whether it meets the requirements, then you might still have a question whose answer might actually be useful to you. - Hans Passant

1 Answers

0
votes

You can not create a vector of .Net class object. You need a vector of handle types.

test = gcnew cliext::vector<Color^>(5);

Also your class objects must be of type ref class

ref class Color

Assignment is done with always creating a new object

test[0] = gcnew Color(255,255,255);

You can also use completely unmanaged types, but than you are forced to use pointers to this data in your C++/CLI code.

Managed types can never own unmanaged types. They may own a pointer to an unmanaged type.

So you may get all you anmanaged data into one unmanaged class and this data is accessed from the managed code.