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)
in stdafx there is #include class1.h, #include class2.h
int main(blah blah blah)
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"
What exactly am i doing wrong??
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().
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
void func(X& x); //OK
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
then i have as part of a class1 member 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.
(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
fred IS unknown. Unless you make it global, (bad habit normally), its not available at the point you showed us. to fix it:
void class1::function(class2 fred)
function(); // recursion? this calls class1::function()
you either meant one of those, or
fred.function(); //main has a fred, ok
or something else??
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL