Jump to content
  • Advertisement
Sign in to follow this  
Paul-K

.cpp versus .h Ans comment on my code )

This topic is 3492 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

Hello all, First of all i want to say i have just started to go use oop :) This are my main files: ---- main.cpp ---- #include "MainCore.h" int WINAPI WinMain(HINSTANCE Main,HINSTANCE PMain,LPSTR Commands,int initShow){ CoreMBOX Mbox("Test","Hey!",MB_OK | MB_ICONINFORMATION); return 0; } ---- maincore.h ---- #include <windows.h> #include "CoreBox.h" ---- CoreBox.h ---- ///////////////////////////////////////////MessageBox Class/////////////////////////////// enum ReturnTypMBOX{ PUSHED_OK = 1, PUSHED_CANCEL = 2, PUSHED_ABORT = 3, PUSHED_RETRY = 4, PUSHED_IGNORE = 5, PUSHED_YES = 6, PUSHED_NO = 7, PUSHED_AGAIN = 10, PUSHED_CONTINUE = 11 }; class CoreMBOX{ public: CoreMBOX(); CoreMBOX(LPSTR Text = "Sample Text",LPSTR Caption = "Sample Caption",UINT Type = MB_OK | MB_ICONINFORMATION); int CoreMBOXCreate(); int CoreMBOXCreate(LPSTR Text,LPSTR Caption,UINT Type); int Pushed; private: int LocalReturn; HWND _hwndholder; }; CoreMBOX::CoreMBOX(){ CoreMBOXCreate(); } CoreMBOX::CoreMBOX(LPSTR Text,LPSTR Caption,UINT Type){ CoreMBOXCreate(Text,Caption,Type); } int CoreMBOX::CoreMBOXCreate(){ MessageBox(NULL,"Sample Text","Sample Caption",MB_OK | MB_ICONINFORMATION); Pushed = 1; return 1; } int CoreMBOX::CoreMBOXCreate(LPSTR Text,LPSTR Caption,UINT Type){ int LocalReturn = MessageBox(NULL,Text,Caption,Type); Pushed = LocalReturn; return LocalReturn; } That is the first point, i get none errors but did i coded this properly? And the second thing is if i change the extention from CoreBox.h to CoreBox.cpp i get all errors: 1>c:\users\paul\documents\visual studio 2008\projects\project i-source\project i-source\corebox.cpp(24) : warning C4520: 'CoreMBOX' : multiple default constructors specified 1>Corebox.cpp 1>c:\users\paul\documents\visual studio 2008\projects\project i-source\project i-source\corebox.cpp(17) : error C2061: syntax error : identifier 'LPSTR' 1>c:\users\paul\documents\visual studio 2008\projects\project i-source\project i-source\corebox.cpp(17) : error C2535: 'CoreMBOX::CoreMBOX(void)' : member function already defined or declared 1> c:\users\paul\documents\visual studio 2008\projects\project i-source\project i-source\corebox.cpp(16) : see declaration of 'CoreMBOX::CoreMBOX' 1>c:\users\paul\documents\visual studio 2008\projects\project i-source\project i-source\corebox.cpp(19) : error C2061: syntax error : identifier 'LPSTR' 1>c:\users\paul\documents\visual studio 2008\projects\project i-source\project i-source\corebox.cpp(19) : error C2535: 'int CoreMBOX::CoreMBOXCreate(void)' : member function already defined or declared 1> c:\users\paul\documents\visual studio 2008\projects\project i-source\project i-source\corebox.cpp(18) : see declaration of 'CoreMBOX::CoreMBOXCreate' 1>c:\users\paul\documents\visual studio 2008\projects\project i-source\project i-source\corebox.cpp(23) : error C2146: syntax error : missing ';' before identifier '_hwndholder' 1>c:\users\paul\documents\visual studio 2008\projects\project i-source\project i-source\corebox.cpp(23) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 1>c:\users\paul\documents\visual studio 2008\projects\project i-source\project i-source\corebox.cpp(23) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 1>c:\users\paul\documents\visual studio 2008\projects\project i-source\project i-source\corebox.cpp(28) : error C2065: 'LPSTR' : undeclared identifier 1>c:\users\paul\documents\visual studio 2008\projects\project i-source\project i-source\corebox.cpp(28) : error C2146: syntax error : missing ')' before identifier 'Text' 1>c:\users\paul\documents\visual studio 2008\projects\project i-source\project i-source\corebox.cpp(28) : error C2761: '{ctor}' : member function redeclaration not allowed 1>c:\users\paul\documents\visual studio 2008\projects\project i-source\project i-source\corebox.cpp(28) : error C2059: syntax error : ')' 1>c:\users\paul\documents\visual studio 2008\projects\project i-source\project i-source\corebox.cpp(28) : error C2143: syntax error : missing ';' before '{' 1>c:\users\paul\documents\visual studio 2008\projects\project i-source\project i-source\corebox.cpp(28) : error C2447: '{' : missing function header (old-style formal list?) 1>c:\users\paul\documents\visual studio 2008\projects\project i-source\project i-source\corebox.cpp(32) : error C2065: 'NULL' : undeclared identifier 1>c:\users\paul\documents\visual studio 2008\projects\project i-source\project i-source\corebox.cpp(32) : error C2065: 'MB_OK' : undeclared identifier 1>c:\users\paul\documents\visual studio 2008\projects\project i-source\project i-source\corebox.cpp(32) : error C2065: 'MB_ICONINFORMATION' : undeclared identifier 1>c:\users\paul\documents\visual studio 2008\projects\project i-source\project i-source\corebox.cpp(32) : error C3861: 'MessageBox': identifier not found 1>c:\users\paul\documents\visual studio 2008\projects\project i-source\project i-source\corebox.cpp(36) : error C2065: 'LPSTR' : undeclared identifier 1>c:\users\paul\documents\visual studio 2008\projects\project i-source\project i-source\corebox.cpp(36) : error C2146: syntax error : missing ')' before identifier 'Text' 1>c:\users\paul\documents\visual studio 2008\projects\project i-source\project i-source\corebox.cpp(36) : error C2761: 'int CoreMBOX::CoreMBOXCreate(void)' : member function redeclaration not allowed 1>c:\users\paul\documents\visual studio 2008\projects\project i-source\project i-source\corebox.cpp(36) : error C2059: syntax error : ')' 1>c:\users\paul\documents\visual studio 2008\projects\project i-source\project i-source\corebox.cpp(36) : error C2143: syntax error : missing ';' before '{' 1>c:\users\paul\documents\visual studio 2008\projects\project i-source\project i-source\corebox.cpp(36) : error C2447: '{' : missing function header (old-style formal list?) So what is actually difference between those extentions, why does it matters so much? Thx for all answers

Share this post


Link to post
Share on other sites
Advertisement
Quote:

That is the first point, i get none errors but did i coded this properly?

No. You get no errors because the compiler does not compiler files ending in .h; they only get seen as a result of being #include'd into source files. Which is why you get all the errors when you change the extension to .cpp.

Do you understand what those errors are and how to fix them, or do you need help with that as well?

Share this post


Link to post
Share on other sites
That's a really simple question but I don't know where to start.

Basically the difference is that .cpp files are compiled whereas .h files are not.

.h files are meant to be included (literally copy pasted by the preprocessor) in .cpp files

This mechanism allows simplified code management. For example it would be ok to do this


// file1.cpp
struct A
{
int i;
};

// file2.cpp
struct A
{
int i;
};


Though every time you want to change A, you need to change it everywhere you declared it.

So instead we do:


// A.h
struct A
{
int i;
};

// file1.cpp
#include "A.h"

// file2.cpp
#include "A.h"


Hope this clears things up a bit.

Share this post


Link to post
Share on other sites
The next time you're posting code, surround it with [source] tags, to keep it formatted, syntax highlighted and short. See the Forum FAQ for more information on that.


As for your code, the first problem is that, in main.cpp, you're making a ComboMBOX instance, but main.cpp does not know how ComboMBOX looks, because you're not including ComboBox.h in that file.

The second problem is that you've put both the class definition and it's implementation in a header file. You'll only want the definition (the class Name { ... }; part) in the header file, so that, when other source files include it, they know what functions and variables a ComboMBOX has. In other words, it tells them how to work with that class. The implementation (what those functions actually do) goes in a source file (ComboBox.cpp) of it's own, so it will be compiled only once.


There are some more issues with your code, but that's what those error messages are pointing you at.

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!