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?
Printable View
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?
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.
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;
i dont want to go off topic but i see some people type .h and some dont, example:
<stdio.h>
<stdio>
does it matter??
Puting the a class in a header looks like this:
myclass.h:
myclass.cpp:Code:#ifndef INCLUDED_MYCLASS_H
#define INCLUDED_MYCLASS_H
class myclass
{
public:
void do_something();
private:
int x_;
};
#endif
some_other_cpp_file.cpp:Code:#include "myclass.h"
void myclass::do_something()
{
x_ = 0;
}
Code:#include "myclass.h"
void f()
{
myclass a;
a.do_something();
}
Your own headers:Quote:
i dont want to go off topic but i see some people type .h and some dont
#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>
Actually I have seen people did that (The class definition and implementation is in the same header file.).Quote:
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;
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.
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?
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
I believe if the class uses templates then both the declaration and implementation needs to go in the header.
Not true, though its much, much more difficult.
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.
Like I said it is possible, but I found it very hard to do. I generally just put it in the header now.
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.
Sounds like the including the .cpp at the bottom of the .h or just putting it all in the .h is the best bet.
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.