Thread: Array and classes problem

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    9

    Array and classes problem

    Sorry tabstop, I completed the question and made it better.
    -----------------------------------------------------------

    I have a class like such:

    Code:
    class Sphere 
    {
    public:
    	Sphere(){};
    	~Sphere() {};
    	Sphere(string m_name) : name(m_name) {};
    	void SetValues(float m_posx, float m_posy, float m_posz,
    					float m_sclx, float m_scly, float m_sclz,
    					float m_r, float m_g, float m_b,
    					float m_Ka, float m_Kd, float m_Ks, float m_Kr,
    					float m_n)
    	{
    		Ka = m_Ka; Kd = m_Kd; Ks = m_Ks; Kr = m_Kr;
    		n = m_n;
    		radius = 1.00;
    		center.x  =  0.00; center.y = 0.00; center.z = 0.00;
    		sphereColor.r = m_r; 
    		sphereColor.g = m_g;
    		sphereColor.b = m_b;
    		SetTranformMatrix(m_posx, m_posy, m_posz,
    						  m_sclx, m_scly, m_sclz);
    	};
    
    		
    	
    	         //set the transfor matrix
    	void SetTranformMatrix( float posx, float  posy, float posz,
    							float sclx, float  scly, float sclz)	
    	{
    		Trans[0][0] = sclx;
    		Trans[0][1] = 0;
    		Trans[0][2] = 0;
    		Trans[0][3] = posx;
    		Trans[1][0] = 0;
    		Trans[1][1] = scly;
    		Trans[1][2] = 0;
    		Trans[1][3] = posy;
    		Trans[2][0] = 0;
    		Trans[2][1] = 0;
    		Trans[2][2] = sclz;
    		Trans[2][3] = posz;
    		Trans[3][0] = 0;
    		Trans[3][1] = 0;
    		Trans[3][2] = 0;
    		Trans[3][3] = 1;
    	};
    
                   //should return the depth at which the the ray has intersected otherwise return -1;
    	double intersect(ray &m_ray)
    	{
    
    		double T_Inverse[4][4];
    		invert_matrix(Trans, T_Inverse);
    
                   //buncha lines here
    	}
      
    	std::string name;
    	double Trans[4][4];	//transition matrix
    	color sphereColor;
    
    private:
    	point center;
    	double radius;
    	float Ka, Kd, Ks, Kr;
    	float n;	//specular 
    
    };
    and a function like such:
    Code:
    float compute_closest_intersection(ray &m_ray, Sphere * m_spheres)
    {
    	float intersect = -1;
    	float temp_intersect;
    	for(int i=0; i < 3; i++)
    	{
    		temp_intersect = m_spheres[i].intersect(m_ray); //HERE HERE HERE HERE 
    		intersect = (temp_intersect >= intersect) ?  temp_intersect : intersect;
    	}
    	return intersect;
    }
    and in main I have
    Code:
    Sphere inputSphere[5];
    float testiii = compute_closest_intersection(Thru_Pixel,InputSphere);
    - and the problem: Trans[][] doesn't have values,
    (where it was initialized in the SetTranformMatrix() function)
    when the intersect is called from compute_closest_intersection function.

    in a nutshell

    Why doesn't Trans[][] keep the values assigned to it in the SetTranformMatrix() function.


    -Thank you
    Last edited by armen; 11-17-2008 at 04:15 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Who knows? It's not as through "set int Transform[4][4] to be constant" means anything.
    And by the way, there already is a vector in C++.... (Thank goodness for namespaces, right?)

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Does "constant" mean you add the "const" keyword to it or something else?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Apparently you can't make it constant: there would be no way to initialize values in it and you can't change them later. Hence it would be useless.

    You could, perhaps, place the array inside a struct/class, so you can initialize it with the result of a function that returns this array object.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by anon View Post
    Apparently you can't make it constant: there would be no way to initialize values in it and you can't change them later. Hence it would be useless.
    Huh? Of course there's a way to initialize constant values in a class...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Elysia
    Of course there's a way to initialize constant values in a class
    The member variable is an array, and the normal initializer syntax for arrays cannot be used in initialisation lists, if I remember correctly.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Each Sphere has its own copy of Trans; once you call SetValues on that Sphere, then it will have values in the Trans matrix. T_Inverse will go away after the function call ends.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by laserlight View Post
    The member variable is an array, and the normal initializer syntax for arrays cannot be used in initialisation lists, if I remember correctly.
    Oops, you're right. Oversight?
    But then again, perhaps std::vector or std::tr1::array would work.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Nov 2008
    Posts
    9
    Ok , but why wouldn't Trans[][] hold the values assigned to it in the SetTransformMatrix()?

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by armen View Post
    Ok , but why wouldn't Trans[][] hold the values assigned to it in the SetTransformMatrix()?
    It will. Why do you think it won't?

    Edit: I should be more specific: I've been assuming, despite the code that you've posted, that you are in fact calling SetTransformMatrix at some point (at least via SetValues).
    Last edited by tabstop; 11-21-2008 at 09:37 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of classes?
    By dream_noir in forum C++ Programming
    Replies: 2
    Last Post: 03-22-2009, 11:43 AM
  2. Overloading Array Objects for use with Classes
    By ibleedart in forum C++ Programming
    Replies: 2
    Last Post: 10-24-2007, 06:48 PM
  3. copy array of classes or explicity call constructor
    By Doodle77 in forum C++ Programming
    Replies: 5
    Last Post: 06-10-2007, 11:57 AM
  4. two-dimensional dynamic array of pointers to classes
    By Timo002 in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 06:18 AM
  5. 2d Array access by other classes
    By deaths_seraphim in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2001, 08:05 AM