Make objects talk to each other!!


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Make objects talk to each other!!

  1. #1
    Join Date
    Mar 2005
    Posts
    3

    Angry Make objects talk to each other!!

    Hey, i'm slowly teaching myself cpp and have come unstuck! (getting very annoying) :mad:

    Basically what i need to know is, are there rules as to which objects are 'visible' to other objects.

    Hypotheticall you have a program:

    class1.h (class1 declaration)
    class1.cpp (class1 definitions)
    class2.h (class2 declaration)
    class2.cpp(class2 definitions)

    in stdafx there is #include class1.h, #include class2.h



    #include "stdafx.h"

    int main(blah blah blah)
    {

    class1 bob;
    class2 fred;
    }


    The probelem i then have is that the member functions of these objects fred and bob can't see each other. So when i try to compile i just get whole heap of errors "bob, undeclated identifier" .. "left of .killfred() must be class/struct/union type"

    etc etc

    What exactly am i doing wrong??

  2. #2
    Join Date
    Dec 2003
    Posts
    3,366
    first, stdafx is custom generated for every program. Its nonstandard, and unless you are making a MFC program, its not needed.

    the objects are declared in main, anywhere else, they do not exist. This sounds like your error message. If you get missing storage class errors, its not seeing you include or .cpp. Remember to include the .h and add the .cpp to the project.

    You can't do fred.bobfunction() unless you made the classes friends or inhreited or the like.

    The only thing this program can currently do is, inside main, call either bob.somebobfunction() or fred.somefredfunction().

  3. #3
    Join Date
    Nov 2003
    Posts
    4,118
    if you need to refer to a class X from another class Y, you have to use a forward declaration of X before you use it in Y. Alternatively, you can #include X.h before you #include Y.h. If the dependeny is circular, i.e., X refers to Y and vice versa, then a forward declartion is unavoidable:

    class X; //fwd decl

    class Y
    {
    public:
    void func(X& x); //OK
    };
    Danny Kalev

  4. #4
    Join Date
    Mar 2005
    Posts
    3
    Ok, i am still confuzzled.

    basically i have 2 classes,
    both are declared in their own header files and bother have their function definitions in their own header files. class1.h class1.cpp etc

    and appropriate include stuffs

    then i have
    main()
    {
    class1 bob;
    class2 fred;
    }

    then i have as part of a class1 member function

    void class1::function();
    {
    fred.function();
    }


    and then it tells me that fred is an unknown identifier. Is that because i have done something fundamentally wrong, or because when it compiles it gets to the includes and goes through the class files before it gets to the main() function where the objects are created??

    Either way, how would i go about programming so that either it compiles knowing that the object exists, or create the function in a more sophisticated manner.

    Thanks for your help.
    Alex

  5. #5
    Join Date
    Apr 2004
    Posts
    136
    (maybe im totaly wrong but....)

    fred is not declared as an object of class1 but your trying to utilise class 1's functions. For the object(fred) to interact with class 1's functions u need to create a pointer to class 1 (whether it be dynamic/local/global or static is your choice)

    Now at this point of creating pointers i my-self am currently still confused lol...I just have not had time to study as much as i would like to lately.

    Im sure Danny or Jonin can clear this up better for u

  6. #6
    Join Date
    Dec 2003
    Posts
    3,366
    fred IS unknown. Unless you make it global, (bad habit normally), its not available at the point you showed us. to fix it:

    option 1:
    void class1::function();
    {
    class2 fred;
    fred.function();
    }

    option 2:

    void class1::function(class2 fred)
    {
    fred.function();
    }

    option 3:

    void class1::function();
    {
    function(); // recursion? this calls class1::function()
    }

    you either meant one of those, or
    inside main:

    fred.function(); //main has a fred, ok

    or something else??

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center