Archived

This topic is now archived and is closed to further replies.

Joan Roca Vidrira

Object array inicialitzation problem in C++

Recommended Posts

I have a problem might like http://www.gamedev.net/community/forums/topic.asp?topic_id=200983 I have one array of objects Color:
Color Cont[100];
 
Color have copy constructor and assign operator, and work fine standalone. (one instance)

Color* Alone

But when i do that:
      
for (int i=0;i<50;i++)
Cont[i] = *Alone;

The Cont[1] and Cont[2].. have diferents values that Alone

Then i try that:
   
Cont[2] = *Alone;

And now Cont[2] i the same that *Alone Whats wrong? (sorry I have poor c++ and English knowledges) [edited by - Joan Roca Vidrira on April 21, 2004 12:46:10 PM] [edited by - Joan Roca Vidrira on April 21, 2004 12:46:42 PM] [edited by - Joan Roca Vidrira on April 21, 2004 12:47:39 PM]

Share this post


Link to post
Share on other sites
I think quite a bit of your code got eaten by the forum. Try putting code tags around it.

actually, i guess only the i subscript after Cont got eaten. In that case the loop looks fine. Are you sure Alone points to a valid object?

[edited by - marijnh on April 21, 2004 11:58:38 AM]

Share this post


Link to post
Share on other sites
no, thats all code
(but i sorry for don't put the tag)

alone points to a valid object, (i try to print it)

i will put color.h and operators

class Color
{
public:
Color(double R, double G, double B);
Color(void);
Color::Color(Color& a);
Color& Color::operator = (Color& a) ; /// Revisar !!

int Color::operator == (Color& a) ;

inline double* GetColor(void) { return db_color;}
inline double GetColorR(void) { return db_color[0];}
inline double GetColorG(void) { return db_color[1];}
inline double GetColorB(void) { return db_color[2];}
inline void SetColor(double R, double G, double B) {db_color[0]=R; db_color[1]=G; db_color[2]=B;}

private:

double db_color[2];

};

Color::Color(Color& a) // Operador Copia


{
for(int i = 0; i<3; i++)
db_color[i] = (a.GetColor()[i]);
}



Color& Color::operator = (Color& a) // Assignació


{
db_color[0] = a.GetColorR();
db_color[1] = a.GetColorG();
db_color[2] = a.GetColorB();
return *this;
}



Sorry i forget the tag source twice

[edited by - Joan Roca Vidrira on April 21, 2004 12:44:03 PM]

Share this post


Link to post
Share on other sites
I think this should read 'double db_color[3];'

private:
double db_color[2];
};


Also, check your copy constructor
   
Color::Color(Color& a) // Operador Copia

{
for(int i = 0; i<3; i++)
db_color = (a.GetColor()[i]);
}


See that assignment? I think it should read

   
db_color[i] = (a.GetColor()[i]);


Hope this helps,

-- Jani

[edited by - The Scytheman on April 21, 2004 12:34:32 PM]

[edited by - The Scytheman on April 21, 2004 12:35:41 PM]

Share this post


Link to post
Share on other sites
@The Scytheman - the forum was interpreting his [i] as an italics tag.

@Joan Roca Vidrira - I can''t see specifically what''s wrong, but currently, your class doesn''t even need a specialized copy constructor or assignment operator. The defaults will work just fine. But if you have them there in case of future additions that might require customized versions, then I have another suggestion. Within the functions, you don''t need to call a.GetColor()[i] or a.GetColor_(). Just access a.db_color[i] itself. Since you''re within the class anyway, it is perfectly valid (and often more efficient) to just access the data directly, instead of using accessor functions. And it may be that GetColor() for some reason isn''t working (but they do look fine to me).


Most of the greatest evils that man has inflicted upon man have come through people feeling quite certain about something which, in fact, was false.

-Bertrand Russell, Unpopular Essays, "Ideas That Have Harmed Mankind"

Share this post


Link to post
Share on other sites