Advertisement

String parameters in classes

Started by October 10, 2002 06:16 PM
7 comments, last by rugal 22 years, 1 month ago
Hi I started some OO programming and have come across a strange bug. I have a character class and a bodypart class, where the character will be composed of a number of bodypart objects. //the method called is: void CBodypart::CInitBodypart(char *Filename) //I Call: hair.CInitBodyPart("data/hair.txt"); //This initialises the hair mesh //where hair is a bodypart instance: CBodypart hair; //I get the error: no matching function for call to CBodypart::CInitBodyPart (const char[19]) When this wasn''t classes it worked. But now that I''ve put the code in classes it doesn''t work. Why does it pass as const char[19] and not be compatible with plain old char* ? Heres the source:
  
class CCharacter // 3D model

{
    public:
        int		Cnumtriangles;				// number in current model

        CBodypart hair;                       //ONE BODYPART!!!

	...
	...
	CBodypart lboot				      //The left boot bodypart



        CCharacter();
        void Cinitchar();      //This initialises the body parts

};
where:
void CCharacter::Cinitchar()
{
   hair.CInitBodyPart("data/hair.txt");       //This initialises the hair mesh

					 //THIS GIVES THE ERROR

   ...
   ...
}
---------------------------------------------------------
class CBodypart // Each limb

{
    public:
        CBodypart();
        CBodypart(char *Filename);

        void CInitBodypart(char *Filename);

    private:
        POLY *mesh;
};

CBodypart::CBodypart(char *Filename)
{
 mesh = ReadModel(Filename);
 numpolys = Resetvariables();
}

void CBodypart::CInitBodypart(char *Filename)
{
 mesh = ReadModel(Filename);
 numpolys = Resetvariables();
}
  
Thanks in advance for your consideration.
It''s just because your parameter (char *Filename) isn''t constant. The compiler just needs to know you won''t be messing with the string that is sent to the function. You just need a "const char *Filename". I don''t know if that''s the only problem, but it should fix that error.

Jiia
Advertisement
no didn''t work, and I got an extra error

In method `void CCharacter::Cinitchar()'':
no matching function for call to `CBodypart::CInitBodyPart (const char[19])''

In method `void CBodypart::CInitBodypart(const char *)'':
passing `const char *'' as argument 1 of `CBodypart::ReadModel(char *)'' discards qualifiers
Try declaring CBodypart before CCharacter.
Actually, everything you have is correct in the classes. I used to make that same mistake when I first started out programming and when I figured out what I did wrong, I feel pretty stupid - and you''ll probably feel the same too after you hear my explanation:

You have a function in the class called
CBodypart:CInitBodypart()
but, in your main or where ever you are calling it from, you called it:
CInitBodyPart()

CInitBodyPart() is not defined, only CInitBodypart() is.
quote: Original post by LyLFox
Actually, everything you have is correct in the classes.


No the char* part. C string litterals are const char* not char*.

Non-const char pointer initialisation through litteral constants is deprecated and must not be used in new code.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
Thanks. I can't believe I made that case mistake. Doh.
BTW if I use const I get the error:
passing `const char *' as argument 1 of `CBodypart::ReadModel(char *)' discards qualifiers

What does that mean?

Anyway, theres another issue in regards to this I'd like some help on. It's a bit long, but simple, just read on:

In main program I create a character object:
CCharacter Samus;

In CCharacter I create a hair object as follows:
CBodypart hair;

In the main program I call:
Samus.Cinitchar();

Which is defined as:
void CCharacter::Cinitchar()
{
hair.CInitBodypart("data/hair.txt");
}

---------------------------------
Now CInitBodypart is defined as:
void CBodypart::CInitBodypart(char *Filename)
{
mesh = ReadModel(Filename);
numpolys = Resetvariables();
}

The thing is, for 10 limbs I have 10 definitions and 10 inits.
So I made the following constructor which is the same as the init:

CBodypart::CBodypart(char *Filename)
{
mesh = ReadModel(Filename);
numpolys = Resetvariables();
}
---------------------------------

This way I don't need the init. Just the definition, which is:
CBodypart("data/hair.txt") hair;

The thing is when I call
i = Samus.hair.numpolys;
the first way with a definition and an init it works.

But with my own constructor: CBodypart(char *Filename);
i.e. just a definition I get the following 2 errors:

parse error before string constant

In function `void DrawFrame()':
`class CCharacter' has no member named `hair'

Again, thanks for the help before and thanks in advance for any help here.

[edited by - rugal on October 10, 2002 10:40:17 PM]

[edited by - rugal on October 10, 2002 10:43:13 PM]
quote: Original post by rugal
Thanks. I can''t believe I made that case mistake. Doh.
BTW if I use const I get the error:
passing `const char *'' as argument 1 of `CBodypart::ReadModel(char *)'' discards qualifiers


That means you''re passing a ''const char*'' to a function taking a ''char*'' and that it does not like it. Make the function take a ''const char*'' too (that''s what const-correctness is all about, helping the compiler prevent you from modifying things you should not modify)

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Why are you using C style strings in C++ code anyway?
std::string is what you should be using.
"I don''t know with what weapons the third world war will be fought, but I know the fourth will be fought with sticks and stones." Einstein

This topic is closed to new replies.

Advertisement