Results 1 to 16 of 16

Thread: Class in .h file, how would I use it?

  1. #1

    Thread Starter
    Fanatic Member scr0p's Avatar
    Join Date
    Oct 2002
    Location
    VA
    Posts
    720

    Class in .h file, how would I use it?

    I wrote a class, right now all my code is in 1 .cpp file, I am using VS .NET, If I put the class code alone, into an .h file, then how would I include/use it?
    asdf

  2. #2
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    Why would you want to put your class in a header?

    Header is only for class declaration. The implementation of the class in in *.cpp . I think it is alright to put the the class declaration and implementation in the CPP.

  3. #3

    Thread Starter
    Fanatic Member scr0p's Avatar
    Join Date
    Oct 2002
    Location
    VA
    Posts
    720
    What do you mean? If I paste my class into the .h file, can't I just use it like
    #include <my_class.h>

    MYCLASSNAME x;

    x.age = 12;
    asdf

  4. #4
    Junior Member
    Join Date
    Dec 2002
    Location
    in a little closet
    Posts
    30
    i dont want to go off topic but i see some people type .h and some dont, example:

    <stdio.h>
    <stdio>

    does it matter??
    All Help Very Much Appreciated

  5. #5
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771
    Puting the a class in a header looks like this:
    myclass.h:
    Code:
    #ifndef INCLUDED_MYCLASS_H
    #define INCLUDED_MYCLASS_H
    class myclass
    {
    public:
        void do_something();
    private:
        int x_;
    };
    #endif
    myclass.cpp:
    Code:
    #include "myclass.h"
    void myclass::do_something()
    {
        x_ = 0;
    }
    some_other_cpp_file.cpp:
    Code:
    #include "myclass.h"
    
    void f()
    {
        myclass a;
        a.do_something();
    }
    i dont want to go off topic but i see some people type .h and some dont
    Your own headers:
    #include "my_header.h" or #include "my_header.hpp"

    C library, and other libraries:
    #include <header.h> or #include <somelib/someheader.h>

    C++ Standard library:
    #include <iostream> or #include <cstdio>

  6. #6
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    Originally posted by scr0p
    What do you mean? If I paste my class into the .h file, can't I just use it like
    #include <my_class.h>

    MYCLASSNAME x;

    x.age = 12;
    Actually I have seen people did that (The class definition and implementation is in the same header file.).

  7. #7
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    scr0p - You can do it exactly like you said, but be aware that header files can't do some functions that a .cpp file can. So the best way (at least I think so) is to do what twanvl said.

    Newer_Newbie - Generally the files that dont have the .h are newer header files. MS has started to slowly do away with some of their older not so standard headers. There was a list some where that said what the differnces were and while files didnt have the .h anymore.
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  8. #8
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    If you do it like twanvl says, how does the compiler know to look in the myclass.cpp file? Don't you need to #include it somewhere?
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  9. #9
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    Ok I will show you how it works. I just made up a quick class and windows program with four quick files, a main.cpp, main.h, myclass.cpp & myclass.h:

    Code:
    //Main.cpp
    
    #include "Main.h"
    
    
    #include "Main.h"
    
    
    HWND		g_hWnd;							
    
    HINSTANCE	g_hInst;						
    
    TCHAR		g_szClass[]	= "Test Window";	
    		
    
    
    int m_iWidth	= 500;
    int m_iHeight	= 500;
    int m_iX		= 300;
    int m_iY		= 100;
    
    
    
    
    
    int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow)
    {
    
    	MSG _msg;									
    
    	if(!hPrevInst)									
    		if(!InitApplication(hInst))		
    			return -1;								
    
    	if(!InitInstance(hInst,SW_NORMAL))		
    		return -1;									
    
    	if(g_hWnd != NULL)								
    		ShowWindow(g_hWnd, SW_NORMAL);				
    	else
    		return -1;									
    
    	CMyClass.sCaption = "Nothing";
    	CMyClass.Message();
    
    	int i; 
    	while(i = GetMessage(&_msg, NULL, 0, 0))	
    	{ 
    		if (i == -1) 
    			break; 
    
    		TranslateMessage(&_msg); 
    		DispatchMessage(&_msg); 
    	}
    
    	return ((int)_msg.wParam);			
    
    }
    
    
    
    
    
    
    
    
    BOOL InitApplication(HANDLE hInstance)     
    {
    
    	WNDCLASS  _wc;									
    
    	//Setup The Class
    	_wc.style			= 0;
    	_wc.cbClsExtra		= 0;
    	_wc.cbWndExtra		= 0;
    	_wc.lpfnWndProc		= (WNDPROC)MainWndProc;
    	_wc.hInstance		= (HINSTANCE)hInstance;
    	_wc.hbrBackground	= (HBRUSH)(COLOR_ACTIVEBORDER+1);
    	_wc.hIcon			= LoadIcon(NULL, IDI_APPLICATION);
    	_wc.hCursor			= LoadCursor(NULL, IDC_ARROW);
    	_wc.lpszMenuName	= NULL;
    	_wc.lpszClassName	= g_szClass;
    
    	return RegisterClass(&_wc);						
    }
    
    
    
    
    
    BOOL InitInstance(HANDLE hInstance, int nCmdShow)
    {
    	HWND	_hWnd;				
    	DWORD	_dwFlags;								
    
    	//Setup The Window Flags
    	_dwFlags = WS_OVERLAPPED | 
    			  WS_CAPTION | 
    			  WS_MINIMIZEBOX | 
    			  WS_SYSMENU;
    
    	
        _hWnd = CreateWindow(g_szClass, "Test Window", _dwFlags, 
                            m_iX, m_iY, m_iWidth, m_iHeight, NULL, NULL, (HINSTANCE)hInstance, NULL);
    
        if(_hWnd == NULL)								
            return FALSE;								
    
    	g_hWnd = _hWnd;									
    	g_hInst = (HINSTANCE)hInstance;					
    
        return TRUE;
    }
    
    
    
    
    
    
    
    
    LRESULT APIENTRY MainWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {	
    	PAINTSTRUCT _ps;				
    
    	switch(uMsg) 
    	{
    
    		case WM_DESTROY:
    		{
    			PostQuitMessage(0);
    			break;
    		} //WM_DESTROY
    
    		case WM_PAINT:
    		{
    			BeginPaint(hWnd,&_ps);
    			EndPaint(hWnd,&_ps);
    			break;
    		} //WM_PAINT
    
    		default:
    			return DefWindowProc(hWnd, uMsg, wParam, lParam);
    
    	}//switch
    
    	return 0L;
    }
    Code:
    #ifndef __MAIN_H__
    #define __MAIN_H__
    
    #include <Windows.h>
    #include <string>
    
    #include "MyClass.h"
    
    BOOL				InitApplication(HANDLE hInstance);
    BOOL				InitInstance(HANDLE hInstance, int nCmdShow);
    LRESULT				APIENTRY MainWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
    
    extern HWND			g_hWnd;			//Main Window hWnd
    
    extern HINSTANCE	g_hInst;		//Window Instance
    
    extern TCHAR		g_szClass[];	//Class Name
    
    
    #endif // MAIN_H
    Code:
    //MyClass.cpp
    
    #include "MyClass.h"
    
    //Global Class Var
    MYCLASS_CL CMyClass;
    
    
    MYCLASS_CL::MYCLASS_CL()
    {
    	sMessage = "Hello";
    }
    
    MYCLASS_CL::~MYCLASS_CL()
    {
    }
    
    void MYCLASS_CL::Message()
    {
    	MessageBox(NULL,sMessage.c_str(),sCaption.c_str(),MB_OK);
    }
    Code:
    //MyClass.h
    
    #ifndef __MYCLASS_H__
    #define __MYCLASS_H__
    
    #include "Main.h"
    
    class MYCLASS_CL
    {
    	public:
    		
    		MYCLASS_CL();
    		~MYCLASS_CL();
    		
    		void Message();
    
    		std::string sCaption;
    
    	private:
    		std::string sMessage;
    };
    
    extern MYCLASS_CL CMyClass;
    
    #endif // MYCLASS_H
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  10. #10
    Addicted Member
    Join Date
    Oct 2002
    Posts
    241
    I believe if the class uses templates then both the declaration and implementation needs to go in the header.

  11. #11
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    Not true, though its much, much more difficult.
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  12. #12
    Addicted Member
    Join Date
    Oct 2002
    Posts
    241
    I had a templated linked list that gave me template errors when I had the implementation in a .cpp and when I put the implementation in the .h it worked fine.

  13. #13
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    Like I said it is possible, but I found it very hard to do. I generally just put it in the header now.
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  14. #14
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771
    If you want to put the implementation of a termplate class in a .cpp file you must do one of the folowing:
    - use the 'export' keyword, which most compilers don't support
    - create template instantiations for all the forms you are going to use.
    - include the .cpp file at the bottom of the .h file.

  15. #15
    Addicted Member
    Join Date
    Oct 2002
    Posts
    241
    Sounds like the including the .cpp at the bottom of the .h or just putting it all in the .h is the best bet.

  16. #16
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Plain implementations go into cpp. Declarations, inline implementations and template implementations go into h.

    Implementation of templates in cpp files is a non-standard extension of MSVC++ and works only for template specializations.

    NEVER include a .cpp.


    As for why the compiler knows to look in your cpp: it doesn't, and it doesn't care.
    The compiler just sets a flag that there is a function call. It's the responsibility of the linker to replace the flag by an actual call. The linker knows about the cpp or lib because you tell it one way or the other.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width