Thread: How to dynamically add to arrays

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    36

    How to dynamically add to arrays

    Hi Guys,

    I've got the following code:

    Code:
    float tankRoute [4][3] = {
    		
    {0.450897f, 0.066000f, 0.023682f},
    {0.203909f, 0.086718f, 0.021484f},
    {0.321878f, 0.463296f, 0.027344f},
    {0.450897f, 0.066000f, 0.023682f},
    };
    What I need to be able to do is to add additional entires dynamically ie a click on the mouse will give me 3 points that I need to append to the end of my array.


    Thought you might be able to use a vectot but failed at the first hurdle :-(

    Tried the following (firstly calling #include<vector>:
    vector <float> tankRoute2;

    but got the following errors:

    error C2065: 'vector' : undeclared identifier
    error C2062: type 'float' unexpected


    Any ideas how I can go about doing this please?

    Cheers

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    The vector container is in the std namespace. You would either need to put a using namespace std; statement towards the top of your code:

    Code:
    #include <vector>
    using namespace std;
    ...
    vector<float> tankRoute2;
    ...or put a using std::vector statement:

    Code:
    #include <vector>
    using std::vector;
    ...
    vector<float> tankRoute2;
    ...or put std:: in front of any vector variable declarations:

    Code:
    #include <vector>
    ...
    std::vector<float> tankRoute2;
    However you would probably want a vector of three floats contained within a struct perhaps:

    Code:
    #include <vector>
    ...
    struct 3D_Point
    {
        float x, y, z;
        3D_Point( float x1 = 0.0f, float y1 = 0.0f, float z1 =0.0f ) : x(x1), y(y1), z(z1) {}
    };
    ...
    std::vector<3D_Point> tankRoute2;
    ...
    tankRoute2.push_back(3D_Point()); // Push pt 0.0, 0.0, 0.0 onto vector
    tankRoute2.push_back(3D_Point(5.4f, 6.7f, 8.8f)); // Push pt 5.4, 6.7, 8.8 onto vector
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    36
    Thanks I'll have a go.

    By the way - could explain exactly whats going on here please:

    Code:
    3D_Point( float x1 = 0.0f, float y1 = 0.0f, float z1 =0.0f ) : x(x1), y(y1), z(z1) {}
    as I've only seen the following in books:

    Code:
    struct bin {
      char name [30];
      int quant;
      int cost;
    } item
    Thanks

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    36
    Thanks that seems to do the trick.

    Had some issues hence my query about what the line of code was doing. Once things worked rather obvious :-)

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    36
    Nope sorry spoke too quick :-(

    I've got the following:

    Code:
    struct points
    {
        float x, y, z;
        points( float x1 = 0.0f, float y1 = 0.0f, float z1 =0.0f ) : x(x1), y(y1), z(z1) {}
    };
    .....
    std::vector <points> tankRoute2;
    //tankRoute2.push_back(points(0.450897f, 0.066000f, 0.023682f));
    //tankRoute2.push_back(points(0.203909f, 0.086718f, 0.021484f));
    //tankRoute2.push_back(points(0.321878f, 0.463296f, 0.027344f));
    //tankRoute2.push_back(points(0.450897f, 0.066000f, 0.023682f));
    With the comments in all runs OK.

    However if I try and use the push_back method then I get a popup error message saying 'User breakpoint called from code at 0x77f75a58'.

    The last msg in the debug window says

    HEAP[Tank.exe]: Invalid Address specified to RtlValidateHeap( 08B30000, 08C315C0 )

    Whats going on here

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    structs, like classes, can have member functions and custum operators associated with them. The line in question is a constructor for the 3D_Point struct object and tells how to build a 3D_Point object.

    In particular, the constructor here takes three parameters x1, y1, and z1 that is used to initialize the struct members x, y, and z respectively. The argument list specifies default arguments, the = 0.0f parts, that are to be used if the caller declines to provide an argument. By using the default arguments here, the user has the option of specifying either 3, 2, 1 or none of the arguments when constructing a 3D_Point object, i.e.:

    Code:
    tankRoute2.push_back(3D_Point());               // Add pt 0.0, 0.0, 0.0 to the vector
    tankRoute2.push_back(3D_Point(4.2));            // Add pt 4.2, 0.0, 0.0 to the vector
    tankRoute2.push_back(3D_Point(4.2, 9.6));       // Add pt 4.2, 9.6, 0.0 to the vector
    tankRoute2.push_back(3D_Point(4.2, 9.6, 8.1));  // Add pt 4.2, 9.6, 8.1 to the vector
    The part after the : is called an initializer list and is basically just a shortcut for assigning the arguments to the member variables. It basically states "take the argument provided for x1 and assign it to x" and so on for each of the other two arguments. The two braces specify the body of the constructor which in this case is empty because we did our initialization using the initializer list. We don't have to use the initializer list, it could be removed from the constructor which could be rewritten as:

    Code:
    3D_Point( float x1 = 0.0f, float y1 = 0.0f, float z1 = 0.0f )
    {
        x = x1;  // Do our assignment in body of constructor...
        y = y1;  // instead of in initializer list
        z = z1;
    }
    As for the problem you are describing above, try to compile a minimal (and complete) program example using your struct and a vector in your environment to see if you still get that error. For example:

    Code:
    #include <vector>
    
    struct points
    {
        float x, y, z;
        points( float x1 = 0.0f, float y1 = 0.0f, float z1 = 0.0f ) : x(x1), y(y1), z(z1) {}
    };
    
    int main()
    {
        std::vector<points> tankRoute2;
        
        tankRoute2.push_back(points(0.450897f, 0.066000f, 0.023682f));
        tankRoute2.push_back(points(0.203909f, 0.086718f, 0.021484f));
        tankRoute2.push_back(points(0.321878f, 0.463296f, 0.027344f));
        tankRoute2.push_back(points(0.450897f, 0.066000f, 0.023682f));
    
        return 0;
    
    }
    As a complete program, the above sample should compile without errors (unless I'm missing something). If it compiles for you as well then it is likely that there is something else in your code that is causing this error in which case it would help to see as much of your code as possible. Also, what compiler and OS are you using just for grins.
    Last edited by hk_mp5kpdw; 09-22-2004 at 08:06 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    May 2004
    Posts
    36
    Thanks for that.

    I've tried a simple program as suggested as all compiles / runs ok.

    Look llike something else in my program doesnt like me using the push... method.

    Unfortunatley I dont think that you folks will be able to help as I'm using a developers toolkit (OIV) through most of my code.

    Thanks for response.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My Arrays!!!, My Arrays!!! Help!?
    By llcool_d2006 in forum C++ Programming
    Replies: 1
    Last Post: 12-10-2006, 12:07 PM
  2. Dynamic (Numeric) Arrays
    By DavidB in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2006, 07:34 PM
  3. Dynamically add statis text to a dialog box
    By earth_angel in forum Windows Programming
    Replies: 8
    Last Post: 06-23-2005, 01:28 PM
  4. VC++6.0 Add member function Wizard
    By 7stud in forum C++ Programming
    Replies: 5
    Last Post: 04-05-2003, 08:48 PM
  5. sorting arrays
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-13-2001, 05:39 PM