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

This is a discussion on class problem (init. struct, itierator for 2d array) within the C++ Programming forums, part of the General Programming Boards category; Hey guys, I'm having a bit of trouble with making this class for a 3d cube. I have simply made ...

  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

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    1) What are dynamic structures? C++ doesn't have such a thing.
    2) If you're talking about vectors, you can pass the size in the initialization list.
    3) There's no such thing as a multi-dimensional vector. There can be vectors whose elements are other vectors. Think about it this way and how iterators work should become clear.
    4) Why do you have a class representing a collection of cubes, but no class representing a cube? It should be the other way round: write a class cube and let the collection simply be a vector<cube>.
    5) input1, input2, ... are really, really bad names for arguments. Give them names that represent what they actually do. For example, in the shiftCube function, I can only guess from the types that the last parameter is the index of the cube to shift, while the other three are presumably the x, y, and z offsets, in order.
    6) Iterators mean a uniform iteration syntax for arbitrary sequences, including virtual ones. They don't mean less memory leaks. That's what containers are for.
    7) I don't think you mean dynamic programming when you say "dynamic programming". What is usually called dynamic programming in CS is something quite different.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    NotSoAvgProgrammer
    Join Date
    Jul 2007
    Location
    Virginia, U.S.
    Posts
    57
    Ok, I have changed my code, as well as my state of mind. Thanks for that article as I think it helped me understand a few things a little better.
    However I am still ignorant to iterators and their functionality. I know a vector is simply a object that points to the direction of memory inside a vector, but it's very fuzzy and gray. I tried, and failed miserably at making a iterator for a vector in a vector. Also My class is still very nonfunctional.

    I have revised the code much, yet it is still very bad.

    Code:
    using namespace std;
    
    class cube
    {
    public:
    	// set up parameters of the cube, such as it's height, width, and and vertices.
    	// we are also going to overload the constructor so giving height and width is optional
    	// if not giving, it will be calculated when called
    	cube::cube(vector<vector<float>> vert);
    	cube::cube(vector<vector<float>> vert, float height, float width);
    
    	// get the vertices of a cube
    	vector<vector<float>> getVertices();  // returns array
    
    	// shift the cube x amount of units in x, y, and z dirctions
    	void shiftCube(vector<float>cords);
    
    private:
    	struct cubeIndex
    	{
    
    		// clockwise on top, then clockwise on bottom
    		vector<vector<float>> vertices; 
    		
    		// parameters of the cube
    		float width;
    		float height;
    		float area; 
    	} cubeSpecs;
    	vector<vector<float>>::iterator rowIt;
    	vector<vector<float>>::iterator rowEnd;
    	vector<vector<float>>::iterator colIt;
    	vector<vector<float>>::iterator colEnd;
    	vector<float>::iterator cordIt;
    }
    
    cube::cube(vector<vector<float>> vert)
    {
    	cubeSpecs.vertices = vert;
    	rowIt = cubeSpecs.vertices.begin();
    	rowEnd = cubepecs.vertices.end();
    
    }
    
    cube::cube(vector<vector<float>> vert, float height, float width)
    {
    	cubeSpecs.vertices = vert;
    	cubeSpecs.height = height;
    	cubeSpecs.width = width;
    	rowIt = cubeSpecs.vertices.begin();
    	rowEnd = cubepecs.vertices.end();
    }
    void cube::shiftCube(vector<float> cords)
    {
    	// here is where we iterate the vector of vectors
    	for( ; rowIt != rowEnd; rowIt++)
    	{
    		colIt = rowIt->begin();
    		colEnd = rowIt->end();
    		for( ;colIt != colEnd; colIt++)
    		{
    			// iterator for vector of cords coming in
    			for(cordIt = cords.begin(); cordIt != cords.end(); cordIt++)
    			{
    				*colIt += *cordIt;
    			}
    		}
    	}
    }
    		
    
    	
    }
    
    	
    
    
    vector<vector<float>> cube::getVertices()
    {
    	return cubeSpecs.vertices;
    }

    I want to try and get an understanding of the material, so any good tutorials that will explain anything to me, that you think I could use, would be of great help. Particularly in the subjects of vector + iterators, classes, arrays(particularly receiving them and returning them in a function).


    Thanks you for your time and effort,

    Joe
    A quality job is not necessarily a *good* job. A quality job is, by definition, conformance to requirements. Therefore a *good* job, may not be a quality job.

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, 05:55 PM
  2. Replies: 1
    Last Post: 12-03-2008, 02: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, 01:32 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21