Thread: Using classes in another .cpp file

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    36

    Using classes in another .cpp file

    Hi. I have a class defined in Class.cpp, and an instance of this cannot be created in Main.cpp. How can I make main "see" this class definition? Can anyone point me towards a tutorial? The only ones I could find were just for global functions.

    Thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    3
    Define your class in head file.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    36
    Ok cool. But using this method, is it possible to do that with an anonymous class? How would I reference it using the scope operator, if possible?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    But using this method, is it possible to do that with an anonymous class? How would I reference it using the scope operator, if possible?
    Without a name, how would one would refer to the class? Just give the class a name.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by lastdestined View Post
    Define your class in head file.
    I would declare it in your header and define it in your source file(s).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    No, lastdestined's terminology is correct.

    This is a class declaration:
    Code:
    class Foo;
    This is a class definition:
    Code:
    class Foo : public Bar
    {
      int m_i;
    
    public:
      void func();
    };
    What remains is the class implementation, i.e. the definition of its member functions and static member variables. That's the part that should go into a source file.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    On what grounds do you base this on?
    It's hard to exactly say what's what, but it was discussed sometime before: http://cboard.cprogramming.com/showt...ht=declaration

    declaration - an introduction of a name into a scope. The type of the name must be specified. If the declaration also specifies the entity to which the name refers, the declaration is also a definition.
    definition - a declaration that specifies the entity to which the declared name refers.
    Declaration is the introduction of name into a scope, but it also must have a type. So does that means that your "definition" is also a declaration since it actually defines the type or is it a definition since it specifies the entity to which the declared name applies to?
    Last edited by Elysia; 12-30-2007 at 05:56 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Um, my knowledge?

    But also the C++ standard, section 9. In particular, 9/2:
    A class-specifier is commonly referred to as a class definition.
    The standard subsequently uses "class definition" the same way as I do. Class-specifier is a reference to the grammar in 9/1:
    Code:
              class-specifier:
                      class-head { member-specificationopt }
              class-head:
                      class-key identifieropt base-clauseopt
                      class-key nested-name-specifier identifier base-clauseopt
              class-key:
                      class
                      struct
                      union
    As for class declaration, see 9.1/2:
    A declaration consisting solely of class-key identifier ; is either a redeclaration of the name in the current scope or a forward declaration of the identifier as a class name.
    The standard goes on to refer to this construct as a "class declaration", e.g. in 9.1/3.

    So you see, I'm backed by good authority.

    Note that the cplusplus.com tutorial, on the other hand, refers to class definitions as class declarations. I consider this an error. Declarations introduce the name to the compiler, nothing else. It's that way with functions and with variables. Why should it be different with classes?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by CornedBee View Post
    Um, my knowledge?
    Yes, but I was wondering where you got that knowledge from

    Code:
    class MyClass;
    Class declaration.

    Code:
    class MyClass { /* Stuff here */ };
    Class definition.

    Code:
    void MyClass::foo() { /* Function body here */ }
    Class implementation.

    I believe this is what the standard says, then?
    Last edited by Elysia; 12-30-2007 at 06:08 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The first two; the third I don't think is given a name by the standard.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Registered User
    Join Date
    May 2007
    Posts
    27

    Having a similar issue...

    I have my main.cpp file, and I have a slew of headers with class declarations and their accompanying .cpp files with their code. Take, for instance, Dummy1.h and Dummy1.cpp.

    Dummy1.h:
    Code:
    #ifndef DUMMY
    
    class Dummy
    	{
    	public:
    		void DispNum();
    		int RetInt(int);
    		float RetFlo(float);
    		char RetChar(char);
    	private:
    		int iDum1;
    		float fDum2;
    		char cDum3;
    	};
    
    #define DUMMY
    #endif
    Now for Dummy1.cpp:
    Code:
    #ifndef DUMMY
    #include "Dummy1.h"
    #endif
    
    char Dummy::RetChar(char arg1)
    	{
    	return arg1;
    	}
    
    void Dummy::DispNum()
    	{
    	
    	}
    As it is, if I include Dummy1.h in the main.cpp file, everything works. However, if I want to declare something global, I cannot get all appropriate .cpp files to read it. If I try to declare a global variable in the Dummy1.h, I will get this compiler error using Visual Studio 2005:

    Shining Exp.obj : error LNK2005: "int giDum1" (?giDum1@@3HA) already defined in Dummy1.obj
    C:\Documents and Settings\HP_Administrator\My Documents\Visual Studio 2005\Projects\Shining Exp\Debug\Shining Exp.exe : fatal error LNK1169: one or more multiply defined symbols found

    If I declare it in Dummy1.cpp, the Dummy1.h file will not find it. (I am acutally using a considerably more complex header/source combination than this but I used these to simplify things.) If I declare the variable as global in main, I have that same problem. I need a .cpp file to be able to access an array of strings for purposes of object creation (character names), but the.h file needs to "see" that same array for prototypes or else it will be confused. I hope this is not too complex a question, lol. Thanks for any help you have!

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Avoid global variables if you can.
    Anyway, to solve this problem, define the variable in one .cpp file, then add "extern type yourvariable" to the header and include it and it will work fine.
    The problem is that each .cpp file is treated separately by the compiler but the linker will combine them all into one big file and if you define your variable in a header, all .cpp files which includes this header gets this variable in the .cpp file and thus the linker will find more than one global variable with the same name, which is bad, since it cannot determine which variable your program's separate parts are using, hence it is also not allowed.

    Code:
    // .h file
    extern int giDum1;
    
    // .cpp file
    int giDum1;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    May 2007
    Posts
    27
    Got it working now, thanks a lot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM