Thread: Problem with accessing an object's member variable with one of my cpp files.

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    14

    Problem with accessing an object's member variable with one of my cpp files.

    The main problem is with the classCPP.cpp file. I am trying to access the member variable "personName" of the object "john." (of class person) Since "john" is not declared in this cpp it gives me an error. Is there any way I can access members of object "john" in this cpp?

    This is just an example of a sort of problem I am having with a big project. I am programming a game and got about 1000 lines of code in before I decided to break it all up into smaller pieces. Unfortunately after it was all broken up into individual cpp files. I have about 10 errors caused by these kinds of problems. Is there an easy way to make this work?

    Code:
    //classCPP.cpp
    #include <iostream>
    #include <string>
    #include "classH.h"
    
    using namespace std;
    
    void vegetable::talk()
    {
    cout << john.personName << " has a " << vegetableName << endl; // possible to access object john?
    }

    Code:
    //main.cpp
    #include <iostream>
    #include <string>
    #include "classH.h"
    
    using namespace std;
    
    vegetable potato;
    person john;
    
    int main()
    {
        potato.vegetableName = "Potato";
        john.personName = "John";
        potato.talk();
        system("PAUSE");
    }
    Code:
    //classH.h
    #ifndef CLASS_H
    #define CLASS_H
    
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    class vegetable;
    
    class vegetable
    {
    public: 
    void vegetable::talk();
    string vegetableName;    
    };
    
    class person
    {
    public:
    string personName;
    };
    
    #endif

    16 C:\Dev-Cpp\classCPP.cpp `john' undeclared (first use this function)
    Last edited by Sclorch; 02-12-2009 at 01:59 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So: is there a reason to make john a global object?

    If yes: then put an "extern person john" in your header file (after the declaration of the class, naturally).

    If no: then pass the object around to functions as needed.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The problem is that a vegetable object has no association with a person object. You might want to provide a parameter so that one can pass a person to vegetable::talk(), though as far as I know only madmen and gardeners talk to vegetables
    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

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    14
    Ahh I see now

    Thank you both

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    And don't forget to indent!!
    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.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    14
    Ok I've added the 'extern person john' part. Still getting an error
    Here's the updated header file:

    Code:
    //classH.h
    #ifndef CLASS_H
    #define CLASS_H
    
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    class vegetable;
    class person;
    
    extern person john;
    
    class vegetable
    {
         public: 
         void vegetable::talk();
         string vegetableName;    
    };
    
    
    class person
    {
         public:
         string personName;
    };
    
    
    #endif
    [Linker error] undefined reference to `john'

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You're doing it wrong. Pass john to vegetable::talk.
    Then indent.
    And next time say what error you get!
    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.

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    14
    Shouldn't making it 'extern' relieve the necessity of having to pass it through the talk function? (Since its now global?)

    Indented for you!
    Last edited by Sclorch; 02-12-2009 at 02:19 PM.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Sclorch View Post
    Shouldn't making it 'extern' relieve the necessity of having to pass it through the talk function? (Since its now global?)

    Indented for you!
    That assumes you were able to answer "yes" to the question "Is there a reason to make john a global object". Since you are not able to answer "yes" to that question (because there is not a reason to make john a global object), then you should pass a person in as a parameter to the function.

    If, somehow, you are able to find a reason to make john a global object, you would still not be able to declare an object of type person until you have defined the type "person" first.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Sclorch
    Shouldn't making it 'extern' relieve the necessity of having to pass it through the talk function?
    Yes, though you need to define the variable in exactly one source file.

    But the problem is that if you use a global variable, your class would forever be tightly coupled to the global variable. Every vegetable object would be forced to only talk to person objects belonging to variables named john. If you have another person object... too bad. You have to assign that person object to john to be able to use it with vegetable::talk.

    If you use a parameter, on the other hand, this inflexible nature of global variables disappears. Sure, within the vegetable::talk() member function, the person object is accessed through some name, but the caller can call the function with a variable of any name (or possibly a temporary, or an unnamed variable via a pointer).
    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

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Your current design is like telling the man named vegetable to find the man named john somewhere in the world and ask his name before speaking that name and everything aloud.
    What we would like, however, is for you to be able to tell vegetable to find THIS individual, ask his/her name and say everything aloud.

    Makes more sense, no?
    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.

  12. #12
    Registered User
    Join Date
    Jan 2009
    Posts
    14
    Quote Originally Posted by tabstop View Post
    That assumes you were able to answer "yes" to the question "Is there a reason to make john a global object". Since you are not able to answer "yes" to that question (because there is not a reason to make john a global object), then you should pass a person in as a parameter to the function.

    If, somehow, you are able to find a reason to make john a global object, you would still not be able to declare an object of type person until you have defined the type "person" first.

    John is just a quickly typed example of the sort of problem I am having in my game (I didn't want to post hundreds of lines of source code you know what I mean...?) yes I do need to make the said objects global in my game.

    Thanks to everyone who has helped

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Even if you DO need to make the object global is no reason to make the function bound to only one object. A function should be flexible, so it should be told what object to operate on. There's no reason not to in this case.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Replies: 4
    Last Post: 10-08-2007, 11:47 AM
  3. Creating a map of objects with variable width and height
    By MrSparky in forum C++ Programming
    Replies: 6
    Last Post: 07-30-2007, 03:06 PM
  4. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 11:14 AM
  5. Sharing a variable between classes of different .CPP files
    By divingcrab in forum C++ Programming
    Replies: 5
    Last Post: 07-07-2002, 02:57 PM