|
-
Apr 4th, 2003, 07:45 PM
#1
Thread Starter
Fanatic Member
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?
-
Apr 5th, 2003, 12:12 AM
#2
Hyperactive Member
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.
-
Apr 5th, 2003, 09:47 AM
#3
Thread Starter
Fanatic Member
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;
-
Apr 5th, 2003, 10:47 AM
#4
Junior Member
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 
-
Apr 5th, 2003, 12:21 PM
#5
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>
-
Apr 5th, 2003, 01:20 PM
#6
Hyperactive Member
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.).
-
Apr 7th, 2003, 10:34 AM
#7
Frenzied Member
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

-
Apr 7th, 2003, 03:08 PM
#8
Fanatic Member
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
-
Apr 7th, 2003, 03:33 PM
#9
Frenzied Member
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

-
Apr 7th, 2003, 08:54 PM
#10
Addicted Member
I believe if the class uses templates then both the declaration and implementation needs to go in the header.
-
Apr 8th, 2003, 12:33 AM
#11
Frenzied Member
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

-
Apr 8th, 2003, 03:29 PM
#12
Addicted Member
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.
-
Apr 8th, 2003, 03:46 PM
#13
Frenzied Member
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

-
Apr 9th, 2003, 07:51 AM
#14
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.
-
Apr 9th, 2003, 04:26 PM
#15
Addicted Member
Sounds like the including the .cpp at the bottom of the .h or just putting it all in the .h is the best bet.
-
Apr 15th, 2003, 05:51 AM
#16
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|