Jump to content
  • Advertisement
Sign in to follow this  
TIMPAN

The ways of the noob edition 1

This topic is 4847 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

It has been while since I first picked up classes and constructors, as a learning speed-up I use classes everytime and pack every function I can spit out in class(es) but today I got an error sending a const char pointer into print-out-method which was made to print out a file ... this is the error "illegal call of non-static member function" To a noob what does this mean ( I am not english ) the only solution I came up for this error is to define and declear the method as a normal function PrintContents(const char *Filename); PrintContents(const char *Filename){ *this code works* } Please shed some light on the noob so I can one day become lvl 60 ! :-)

Share this post


Link to post
Share on other sites
Advertisement
Are you calling PrintContents from ClassName.PrintContents or Object.PrintContents?

ClassName is the name you gave the class, and Object means you did somewhere:

ClassName Object;
Object.PrintContents("harrow");


If you did the first one, try calling it on an instance.

Toolmaker

Share this post


Link to post
Share on other sites
No I called it this way


Classname::Function() opss

I have been told that "::" and "->" and "." is the same :-o


----------- Tested ------------------

Nope...

Console.PrintContents(Filename);

where Console is the class name and PrintContents() is the class method

gives me

error C2143: syntax error : missing ';' before '.'

Share this post


Link to post
Share on other sites
I am assuming that your code looks something along the lines of this:-



class CFoo {
public:
CFoo( void )
{
}
~CFoo( void )
{
}

bool PrintContents( const char *strFilename );
}; // end class CFoo

// .........

void main( void )
{
// print out the contents of the desired file
CFoo::PrintContents( "File.txt" );
} // end main




If so, this would be wrong, and you would get the error message that you described. The reason that you are getting this error is because you need to create an 'instance' of CFoo in memory before you can call one of its non-static functions. You would do this by declaring a variable of its type.

Here is the same code, but without the error and creating an instance of CFoo is memeory:-



class CFoo {
public:
CFoo( void )
{
}
~CFoo( void )
{
}

bool PrintContents( const char *strFilename );
}; // end class CFoo

// .........

void main( void )
{
CFoo foo; // create an instance of CFoo

// print out the contents of the desired file
foo.PrintContents( "File.txt" );
} // end main




There is a way though of calling functions from a class without creating an instance of that class. You would do this by making the function that you want to call 'static'. You do this by amending the keyword static before the function declaration.

Here is the same code, but with a static function and no instance of CFoo:-



class CFoo {
public:
CFoo( void )
{
}
~CFoo( void )
{
}

static bool PrintContents( const char *strFilename );
}; // end class CFoo

// .........

void main( void )
{
// print out the contents of the desired file
CFoo::PrintContents( "File.txt" );
} // end main




I hope this helps.

Share this post


Link to post
Share on other sites


That work thu, thanks rpg_code_master

I am building together tutorials into a single project like

PlaySound,Filefinder,Bubble sort -> a program that takes command lines and
searches for txt files to print out If it founds the file :SOUND: if not
:FAILSOUND: when it got the data in the file it will sort it with a Bubble Sort
function the it prints the file to the screen ...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!