Thread: class problem (init. struct, itierator for 2d array)

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    NotSoAvgProgrammer
    Join Date
    Jul 2007
    Location
    Virginia, U.S.
    Posts
    57

    class problem (init. struct, itierator for 2d array)

    Hey guys, I'm having a bit of trouble with making this class for a 3d cube. I have simply made a few functions to build the infrastructures so far, so I have a purpose for most of the things in there, even if they aren't used yet.
    I am strugling in a few different areas in which I will outline for you.
    1. How can I initialize a dynamic structure in a class. // haven't found much on it, not in classes
    2. how can I initialize a dynamic array in a class. //I don't want to specify the size of cubeNumber[amount] untill constructor.
    3. How can I use iterators for multidimensional vectors? // This just confuses me


    Now I have never made a structure in a class before. Just how am I supposed to initialize it, int the private: area, or in the constructor?
    here is the class
    Code:
    using namespace std;
    
    class cubes
    {
    public:
    	// cube amount, defualt is 20
    	cubes(int input);
    	// add a cube, with a vector of it's vertices
    	void addCube(vector<vector<float>> input, int input2);
    	// get the vertices of a cube
    	vector<float> getVertices(int input);  // input cube #
    	// shift the cube x amount of units in x, y, and z dirctions
    	void shiftCube(float input1, float input2, float input3, int input4);
    private:
    	struct cubeIndex
    	{
    		// clockwise on top, then clockwise on bottom
    		vector<vector<float>> vertices;  // there are 8 corners to a cube
    		// for future functions of class
    		vector<float> specs;     
    	// lets make an array of structures so that we can have a struct for each cube
    	} cubeNumber[20];  // **** how do I make this dynamic? ******
    	// if cubeId is over bounds, will return error vector *comint soon*
    	vector<float> errorVector;
    };
    
    
    void cubes::addCube(vector<vector<float>> input, int input2)
    {
    	cubeNumber[input2].vertices = input;
    	// make room for the specs, we will calculate them when get specs is called
    	// this way we don't have to worry about it when they call cubes::getSpecs(int cubeNumber)
    	cubeNumber[input2].specs.push_back(0);
    	cubeNumber[input2].specs.push_back(0);
    
    }
    void cubes::shiftCube(float input1, float input2, float input3, int input4)
    {
    	/* ****** how would I use an iterator here, I know that it is supposed to help
    	contain memory leaks, and is simply a better method than using the variable in the
    	for statement ********* */
    	int b = 0;
    	int k = 0;
    	// lets shift all vertices
    	for(int i = 0, i <=8, i += 3)
    	{
    		// x
    		cubeNumber[input4].vertices[b][k] += input1;
    		// y
    		cubeNumber[input4].vertices[b][(k+1)] += input2;
    		// z
    		cubeNumber[input4].vertices[b][(k+2)] += input3;
    		b++;
    		k++;
    		
    	}
    }
    Also if anyone could lead me to a good article on dynamic programming, that would be greatly appreciated.

    Thanks for your time and effort,

    Joe
    Last edited by avgprogamerjoe; 09-24-2007 at 04:37 PM. Reason: new code, fixed many errors

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  3. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  4. Replies: 4
    Last Post: 12-12-2002, 02:32 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM