Thread: How to use classes in seperate C++ files

  1. #1
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283

    How to use classes in seperate C++ files

    In C#, I got use to putting each class into its own file. During compilation, it would link all these files together and make a final executable program. While going back to C++, I've been trying to achieve the same process. My files include...

    winmain.cpp // obvious main start of the program
    winmain.h // header

    Common.cpp // my character's class

    In Common.cpp, the simple file contains the following:

    Code:
    #include <iostream>
    
    namespace MyNamespace
    {
    	class Common
    	{
    	public:
    		int x, y;
    	};
    }
    I create the Common object named PC in my winmain.cpp file like this:

    MyNamespace::Common PC;

    I try compiling the project now. I recieve the following errors:

    error C2653: 'MyNamespace' : is not a class or namespace name
    error C2065: 'Common' : undeclared identifier
    error C2146: syntax error : missing ';' before identifier 'PC'
    error C2065: 'PC' : undeclared identifier


    What am I missing here?

  2. #2
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    headers.

    the c++ compilation model is primitive compared to, well pretty much any other language!

    you need a Common.h which has the class definition i.e.
    Code:
    namespace MyNamespace
    {
    	class Common
    	{
    	public:
    		int x, y;
    		void DoStuff(void);
    	};
    }
    winmain.cpp should #include Common.h

    note that you can put the definition of Common:oStuff() in common.cpp (unless you want it inline or it's a template, but that's not relevant here)
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    put the class in a *.h file then include that file in all *.cpp files in which it will be used. Very similar to how you use your compiler's standard header files, but enclose the filename in quotes, not angle brackets
    Code:
    #include "MyHeaderFile.h"

  4. #4
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283
    Ah, I see. So basically the header is the signature of the class, then the constructors, methods etc all go into the .cpp file... thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  2. files
    By Raven Arkadon in forum C++ Programming
    Replies: 2
    Last Post: 02-26-2005, 02:18 PM
  3. Dos commands hehe
    By Carp in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 01-17-2003, 02:51 PM
  4. reinserting htm files into chm help files
    By verb in forum Windows Programming
    Replies: 0
    Last Post: 02-15-2002, 09:35 AM
  5. Sharing Classes using .DLL files?
    By Laos in forum C++ Programming
    Replies: 2
    Last Post: 08-30-2001, 04:58 AM