0

My code is something like this:

class cell{
 public:
 int v;
 int x[5];
}

cell **block; //initialized the size of the array as [5][5] in main

Now what I really want to do is that I want to copy values from one integer array to the v member of every object of this class above. Like:

int arr[5][5];

arr has integer values. I want to copy like this.

for(int i=0; i<5;i++)
  for(int j=0;j<3;j++)*(*(block+i)+j).v=arr[i][j];

Quite surely this is not possible as my IDE states. It says "; Statement missing." and indicates towards the

*(*(block+i)+j).v=arr[i][j];

Also this is how I am initializing block though.

  int V=5;
block = new cell*[V+1];
for(int x__=0; x__<=V; ++x__)
{
    for(int y__=0; y__<=V; ++y__)
    {
        block[x__][y__].v=0 ;
    }
}

Can anyone help me achieve this?

5
  • block[i][j].v = arr[i][j]; Commented Dec 19, 2013 at 4:57
  • @godel9 That didn't work either. Commented Dec 19, 2013 at 4:58
  • I did edit my post now for the initialization statements. @godel9 Basically the compiler is stuck with the error I mentioned. "; Statement missing" Commented Dec 19, 2013 at 5:04
  • "I am working on the pre-standard versions where vector template is unavailable." -- I'm so sorry for you. Really. Commented Dec 19, 2013 at 5:30
  • So Am I man. So am I. For myself! Commented Dec 19, 2013 at 5:39

1 Answer 1

1

You're not initializing block correctly:

int V=5;
block = new cell*[V];
for(int x = 0; x < V; ++x)
{
    block[x] = new cell;
    for(int y = 0; y < V; ++y)
    {
        block[x][y].v = 0;
    }
}

You can then copy the 2D array using:

for(int i = 0; i < V; ++i)
    for(int j = 0; j < V; ++j)
        block[i][j].v = arr[i][j];

Also, I'd recommend you look into std::vector for arrays in C++, and you should be initializing the v member variable in a constructor.

Sign up to request clarification or add additional context in comments.

10 Comments

I am working on the pre-standard versions where vector template is unavailable. So I can't use it. And I did use a constructor to initialize it. Ok I understood. Let me try this up.
If you're initializing v in the constructor, then you don't need to set v to zero when you're creating the cell ** 2D array.
Yes I just removed that statement with the loop.
Okay. That was the problem only. Thanks @godel9 .
is class cell(){..} a vaild declaration of a class name?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.