Struggling with using a class from one .cpp in another .cpp file

This is a discussion on Struggling with using a class from one .cpp in another .cpp file within the C++ Programming forums, part of the General Programming Boards category; Hey all, I'm gonna give you an example and an error and I'm gonna need to know A) Why I ...

  1. #1
    Unregistered
    Guest

    Struggling with using a class from one .cpp in another .cpp file

    Hey all,

    I'm gonna give you an example and an error and I'm gonna need to know A) Why I can't do it, B) How can I fix it?

    So here goes..

    I have:

    Gemstone.cpp
    class Gemstone( / Whatever / );

    Init.cpp
    void CreateStones(void)
    {
    Gemstone ruby;
    }

    Main.cpp
    int main()
    {
    ruby.CheckValue();
    }

    Now the problem I'm having is, in main.cpp it says that ruby is an undeclared identifier and so it stops compiling.

    But why? I declared it in init.cpp, why can't I use it in main.cpp?

    (BTW, I also have header files for declaring the classes and functions, so thats not an issue I don't think)

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    14
    Init.cpp
    void CreateStones(void)
    {
    Gemstone ruby;
    }
    The declaration 'Gemstone ruby; ' is local to CreateStones()

    If you place it outside the function, it will be global.
    Dharius

  3. #3
    Unregistered
    Guest
    So I took it from the function and I put it above the function and still no luck...

  4. #4
    Unregistered
    Guest
    http://www.gamedev.net/community/for...topic_id=81512

    Heres a more detailed example of my problem, its still my problem but it has all sorts of source code shown through out it, etc..

    Thanks guys.

  5. #5
    S≠ťnior Member
    Join Date
    Jan 2002
    Posts
    982
    If you want to go with your current design do what Dharius has stated and in your main.cpp declare world as extern -

    extern CTerrain world;

    However, you will be better off declaring 'world' in main, and passing it to InitGame() by reference to do the initialisation (I assume you're are actually doing more in your real InitGame() and are not just using it to declare an instance of CTerrain).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 04:53 AM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 02:19 PM
  3. #include header files or .cpp files?
    By DoctorX in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2006, 12:21 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21