0

I am having trouble with what seems like a very simple concept. I have a class like such:

class Projectile
{
public:
    int count;
    int projectiles[1][3];

    Projectile();
    void newProjectile();

};

Projectile::Projectile()
{
    count = 0;
}

void Projectile::newProjectile()
{
    projectiles[0][0] = { 1, 2, 3 };
}

I am trying to set the values inside the projectiles array, and I must be doing this incorrectly. How can I go about dynamically adding a set of values into this property?

3
  • If there is just 1 row, what is the point of having a 2 dimensional array ? Commented Sep 15, 2011 at 5:51
  • The idea will be to add additional rows later. I just need to set a size initially.. i think. Commented Sep 15, 2011 at 5:56
  • Then vector<vector<int>> is what you want, as I mentioned. Commented Sep 15, 2011 at 5:58

3 Answers 3

3

projectiles[0][0] refers to a specific location in a two-dimensional array, its type is int

If you want to dynamically add items, then you can use std::vector<int> (see here)

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

3 Comments

Please look at at link I gave. If it still doesn't help, I'll see what I can do... :-)
Ah I see. Never worked with these. It is possible to store a vector as a value in a vector? Like a 2 dim vector? What i am trying to achieve is a 2 dimensional array like structure which holds an x and y coordinate for each member of the array. Is this possible with vectors?
Yes (Mahesh pointed this out in his answer). Although there might be better alternatives, such as std::vector< std::pair<int, int> > if you want to store x, y coordinates.
2
 projectiles[0][0] = { 1, 2, 3 };

This isn't correct. Initializer lists can only given at the point of declaration. You have to assign values independently to each location of the array elements. std::vector<std::vector> twoDimArray; is what you want.

struct foo{

    std::vector<std::vector<int> > twoDimArray;
    void create(int size){

        std::vector<int> oneDimArray(size);
        // vector as of now can just accommodate size number of elements. They aren't
        // assigned any values yet.


        twoDimArray.push_back(oneDimArray); // Copy it to the twoDimArray

        // Now if you wish to increase the size of each row, just push_back element to 
        //  that row. 
        twoDimArray[0].push_back(8);
    }
};

11 Comments

Could you provide an example of how to set this up :) ? it would be much appreciated.
@Headspin - See the update. But if you are new to std::vector, first play with a vector that can hold primitive types like int. Then it is much easier to understand of vector holding another vector.
This is a great example. Just one last question. How would I store this as a property in my class?
Could you explain what exactly do you meant by "store this as a property" ?
As in, how would I declare it as a public variable accessible to the rest of the class?
|
1

try this

void Projectile::newProjectile()
{
    projectiles[0][0] = 1;
    projectiles[0][1]=2;
    projectiles[0][2]=3;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.