3

I've looked for tutorials of adding a class to a C++ project in VS C++ 2010 , but I could't find any useful information . Can you please tell me how to add a class to C++ project . When I add the class into source files it creates 2 files (.cpp and .h) where I should write my code and how to include the class in my main.cpp ?

Sam379
  • 271
  • 2
  • 4
  • 13
  • Welcome to SO! You will get better feed back here if you have specific questions. (see http://stackoverflow.com/help). For example this would be a better question if you showed us a skeleton of your class and described what you a) thought it should do and b) what it _is_ doing. – tacaswell Jun 25 '13 at 00:34

3 Answers3

7

You should declare your class in .h files. Write the code for your methods in separate .cpp files which include your header file and then include your header in the main .cpp file. For exemple

header.h

#ifndef _H_
#define _H_
class Foo{
public:
    void someMethod();
};
#endif

fntcn.cpp

#include "header.h"

void Foo::someMethod() {
};

main.cpp

#include "header.h"
int main(){
Foo foo;
foo.someMethod();
return 0;
}

EDIT:

a common practice is to name your files with the same name as the class declared inside. For example, since I declared a class named Foo, the name of the header file should be Foo.h and of the .cpp file: Foo.cpp

Alexandru Barbarosie
  • 2,952
  • 3
  • 24
  • 46
  • 2
    Don't forget to use include guard! "#pragma once" in the header file or #ifdef INC_A. https://en.wikipedia.org/wiki/Pragma_once – TCS Jun 23 '13 at 11:07
  • @TCS: A very good point! – Leo Chapiro Jun 23 '13 at 11:10
  • @TCS yes, got out of my head, thank you for reminding. – Alexandru Barbarosie Jun 23 '13 at 11:10
  • 1
    the compiler says cannot open header file .. – Sam379 Jun 23 '13 at 11:15
  • You created your header file using `Header files` -> `add` -> `new item`? All your files should be in the same directory. – Alexandru Barbarosie Jun 23 '13 at 11:21
  • 1
    I have added a class into my source files (in solution explorer) and it automatically added the .h file into header files – Sam379 Jun 23 '13 at 11:28
  • You must have mixed something up while creating it. Tryed on my VS2010, just create the `class` and then `#include class.h` in my `main.cpp` the program compiles and runs. Try adding each file separatly. – Alexandru Barbarosie Jun 23 '13 at 11:32
  • Currently my function ion the class must return bool ,`bool is_active (int mx ,int my , ALLEGRO_BUTTON_PARAM params){ }` , this is the "prototype" in the .h file . But the compiler says that is_active must return a value . I'm completely messed up – Sam379 Jun 23 '13 at 14:43
  • well yes, it does return a `bool` value. Now your are facing problems with your code because from what I understand the programs "sees" your function. Do you have a `return`in your function? – Alexandru Barbarosie Jun 23 '13 at 15:32
  • yes I have two if statements and everyone returns true or false – Sam379 Jun 23 '13 at 16:01
  • 1
    Try remooving the `else` from the if statement making that way your second `return` also a default return. This might solve the problem. If you have any other question regarding the coding I advice you posting it in a separate Q&A. – Alexandru Barbarosie Jun 23 '13 at 16:05
2

Add this to your main.c : #include "MyClass.h"

To be sure take a look: http://msdn.microsoft.com/en-us/library/c2088962.aspx

To add a generic C++ class to a project In Class View, right-click the project to which you want to add the new class, click Add, and then click Class. In the Add Class dialog box, in the templates pane, click C++ Class. Click Add to display the Generic C++ Class Wizard. In the wizard, provide a class name, and then define settings or accept the defaults. To close the wizard and view the new generic C++ class in the project, click Finish.

[EDIT] Last but not least as TCS said above, don't forget to use include guard! "#pragma once" in the header file!

Leo Chapiro
  • 13,678
  • 8
  • 61
  • 92
0

If you want to import an existing class, you should right click your project, go to add and click Class...This should pop up a window asking you to import the .cpp and the .h file...

If you want to add a new .cpp or a .h file you should click New item, instead of Class, and this will make a new file where you could write your code..

If you want to know the difference between .cpp and .h file, .cpp is where you create your classes so they are definitions, while .h files are where you declare them, so they are declarations.. That's the reason you should add #include "MyNewClass.h" on the class you want to include your New Class...

For more informations about the difference check the asnwer here

Community
  • 1
  • 1
Jack
  • 115
  • 1
  • 15