1

Im trying to create a vector that will hold pointers to other pointers to unsigned chars. my vector definition is as follows

std::vector<unsigned char**> slices; // data for each slice, in order

//populating vector
unsigned char a = 1;
for (int k = 0; k < size; k++)
{
    unsigned char ** tempSlice = new unsigned char*;
    slices.push_back(tempSlice);
    for (int y = 0; y < height; y++)
    {
        unsigned char * tempY = new unsigned char;
        slices[k].push_back(&tempY);
        for (int x = 0; x < width; x++)
        {
            slices[k][y][x] = a;
        }
    }
}

the error i get however is something allong the lines of:

error request for member ‘push_back’ in ‘((VolImage*)this)-     
>VolImage::slices.std::vector<_Tp, _Alloc>::operator[]<unsigned char**, 
std::allocator<unsigned char**> >(((std::vector<unsigned 
char**>::size_type)k))’, which is of non-class type 
‘__gnu_cxx::__alloc_traits<std::allocator<unsigned char**> >::value_type 
{aka unsigned 

I dont know what im doing wrong. Also, if anyone could have a look at my destructor method to see if im on the right track:

VolImage::~VolImage()
{
    std::cout<<"Destructor"<<std::endl;
    //deconstructing the vector of slices
    int size = slices.size();
    for (int k = 0; k < size; k++)
    {
        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                delete &slices[k][y][x];
            }
            delete[] slices[k][y];
        }
        delete[] slices[k];
   }
delete[] &slices;
}
alk
  • 69,737
  • 10
  • 105
  • 255

2 Answers2

3

You have an error already here:

    slices[k].push_back(&tempY);

Which is explained by the error message, but it's well accepted that gcc's C++ error messages aren't the simplest to parse.

error request for member ‘push_back’ in ‘((VolImage*)this)-
VolImage::slices.std::vector<_Tp, _Alloc>::operator[] >(((std::vector::size_type)k))’, which is of non-class type ‘a__gnu_cxx::__alloc_traits >::value_type {aka unsigned

This is telling you that you are requesting a member function ~push_back~ on (big complicated construct) which is of non-class type (more hard to parse stuff) {aka unsigned char*}

And there's youre problem: slices[k] gives you a reference to an ~unsigned char*~, and char ptrs don't have a member function push_back.

Looking at the rest of your code, it looks like you want to have a two dimensional array of char**'s. One way to accomplish this is a vector of vectors:

std::vector<std::vector<char**>> slices;

This will give you a two-d vector, and will allow your second push_back to succeed, but will cause your first push_back to fail, because you'll be trying to push a char** into a container of containers. Later on you'll fail again because you're treating a two-d vector as a three-d vector.

If you don't mind a little coaching, you're trying to jump too many levels of abstraction at a time. First experiment with vectors of char*'s, then maybe vectors of char**'s and then maybe make it into a 3D vector. That way you'll only have one class of errors (conceptual and compiler) to deal with at one time.

This will be a useful exersize as you'll get a better understanding of pointers and vectors in C++. When you are done with the learning process, you may find a different container more suitable for this purpose. For example Boost multi_array

From your comment

I understand you are meant to use a vector of char** as a 3D array... So in this case the char** has to be understood as a two dimensional array of characters ...see here for a description. Then you make a vector of these two-d arrays - here's an example:

int main() {

std::vector<unsigned char**> slices; // data for each slice, in order

const unsigned int size =5;
const unsigned int rows =2;
const unsigned int cols =3;
for (int k = 0; k < size; k++)
{
    unsigned char** twoD = new unsigned char*[rows];
    for(int i = 0; i < rows; ++i)
        twoD[i] = new unsigned char[cols];
    slices.push_back(twoD);
}
slices[0][0][0] = 'x';
std::cout <<    slices[0][0][0] << std::endl;

}
Community
  • 1
  • 1
Spacemoose
  • 3,856
  • 1
  • 27
  • 48
  • Where you pointed out is exactly where my error is, I got to that by trying different things with the same result, i'll try do it level by level and see how it goes, my concern is time, I'd like to have this done in the next few hours and I don't really have time to be learning it that way. The definition of slices is what we are to use so I can't use a vector of vectors, has to be pointers and I'm quite shaky when it com's to my knowledge in that regard – Thusoluminati Al-Zawahiri Khar Mar 23 '16 at 13:20
  • Thanks man, your input really helped, did it the long way, read your stuff, was a bit late so - 10% but it works just as it should :) – Thusoluminati Al-Zawahiri Khar Mar 23 '16 at 23:20
0

The vector must be of type void* and then you would need to typecast to char** or char* or whatever when reusing that pointer.

Careful Now
  • 260
  • 1
  • 9