Thread: Calling a Function From A Class

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    387

    Calling a Function From A Class

    Hello :-D

    I havnt worked much with classes, and i have stumbled onto a problem calling a function from a class.

    Here is what my class looks like:

    Code:
    typedef class TILES
    {
    public:
    	void DrawTiles(HWND hwnd);
    	bool bTiles[10][10];
    
    private:
    	int		tY;
    	int		tX;
    
    	BITMAP		bm;
    	PAINTSTRUCT	ps;
    	HDC		hdc;
    	HDC		hdcMem;
    	HBITMAP		hbmOld;
    	HBITMAP		hTile;
    }Tiles;
    please do not flame me about the typedef, i think it looks purdy :)

    and my Function calling:

    Code:
    Tiles::DrawTiles(hwnd);
    and my error:
    Code:
    D:\CPP\RPGGAME\main.cpp(80) : error C2352: 'TILES::DrawTiles' : illegal call of non-static member function
            d:\cpp\rpggame\classes.h(6) : see declaration of 'DrawTiles'
    now, i tried to make the function in the class static, but then i had to make all the variables static, then i got 10 link errors (one for each of the variables i used)

    This code has been formatted from its original version to fit your screen

    [EDIT]
    .....Darn Smilies >=[
    /EDIT]
    "There are three kinds of people in the world...
    Those that can count and those that can't."

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1) There is never a need to typedef classes in C++. Well, ok, I suppose you could need to, but really you don't:

    class Something {
    };

    Something myInstance;

    So you don't really need to typedef.

    2) Where do you actually define the function?

    3) Calling a function is like:

    myInstance.myFunction( );

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    You dont need to make an instance of the class, do you?

    it works fine when i dont, (compiles fine)

    when i changed :: to a . i get 2 errors:

    Code:
    D:\CPP\RPGGAME\main.cpp(80) : error C2143: syntax error : missing ';' before '.'
    D:\CPP\RPGGAME\main.cpp(80) : error C2143: syntax error : missing ';' before '.'
    >> There is never a need to typedef classes in C++.
    Cant u see my line?
    please do not flame me about the typedef, i think it looks purdy
    "There are three kinds of people in the world...
    Those that can count and those that can't."

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    There are 2 ways to call a member function.......from an instance (or a pointer to an instance) or calling static functions.....

    You can call statics without having to create an instance of an object ....These functions are callable simply by doing this... "MyClass::MyStaticFunc()"..........

    The gotcha is that the variables accessed by a static function must also be static........Just imagine this stuff as a "skeleton staff" that operate when the object isnt instantized - the rest of the object isnt around......so the variables that are accessed must be static too!

    Usually you assign static default values outside of the class declaration like so...."int MyClass::MyStatVar = 10;".........

    Look at this as an example;

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    class MYCLASS{
    public:
    	static void statHello(){cout << statstr << endl;}
    protected:
    	static string statstr;	
    };
    
    string MYCLASS::statstr = "Hello from static";
    
    int main()
    {
    	MYCLASS::statHello();
    
    	return 0;
    }
    Remember!!....your nonstatic methods can alter your static variables.......but not the other way around.......static methods need static variables.....

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    Ok, well im gonna leave it without an instance i think, so can someone please help me fix these linker errors?
    "There are three kinds of people in the world...
    Those that can count and those that can't."

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Okiesmokie
    Ok, well im gonna leave it without an instance i think, so can someone please help me fix these linker errors?
    Did you just not pay attention here? Make it static. Make your variables static. Now you can use it without an instance. That's all. The end.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    Did you not listen to me?

    look in the first question:

    >> now, i tried to make the function in the class static, but then i had to make all the variables static, then i got 10 link errors (one for each of the variables i used) <<
    "There are three kinds of people in the world...
    Those that can count and those that can't."

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Okiesmokie
    Did you not listen to me?

    look in the first question:
    Don't try to be witty. It doesn't suit you.

    What exactly is the linker error? With this tiny fragment of code, and no real linker messags, I'm supposed to guess what's wrong in all of your functions and function calls?

    So then, what is the "10 linker errors" that you get when everything is static? Since you insist on using the function with a non instanced version of the class, you have to use static variables.

    So how about posting one of the linker errors related to that?

    Furthermore, how about posting some of the code which the linker errors refer to? There's a novel idea.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    nevermind i got it fixed, with the help of RPG n METS, i ended up using the instance way
    "There are three kinds of people in the world...
    Those that can count and those that can't."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Callback function as class method
    By schifers in forum Windows Programming
    Replies: 39
    Last Post: 05-19-2008, 03:02 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM