C++ - can't make class object parameter for another class member
Hello, I''m trying to get two classes, "Class1" and "Class2" to be friends with each other. (to anyone that doesn''t know what I''m talking about, this must sounds hilarious) I have the the classes stored in "class1.h" and "class2.h" respectively and the function/constructor definitions in "class1.cpp" and "class2.cpp" repsectively.
I declared class2 a friend of class1, so I tried to add a member function to class1 with class2 as a parameter.
My problem is that the compiler doesn''t know what class2 is when I try to use it as a parameter in class1. I tried to #include "class1.h" but then I get a class1 redefinition error. Does anyone know how I can get the compiler to know that class2 is a class while avoiding that error? Please help! :[
Use inclusion guards on every .h:
classX.h:
#ifndef CLASSX_H
#define CLASSX_H
...
#endif // #ifndef CLASSX_H
You won''t have redefinition errors this way.
If you still can''t make it to work try declaring one class before defining the other:
// class2.h
class class1;
class class2 {
...
}
By the way, i think you''re mistaken about the ''friend'' keyword. A friend class can use the private/protected functions and members of the other. If all you want is to have a class to be a parameter of a function of another class, they don''t need to be friends.
Hope that helps.
classX.h:
#ifndef CLASSX_H
#define CLASSX_H
...
#endif // #ifndef CLASSX_H
You won''t have redefinition errors this way.
If you still can''t make it to work try declaring one class before defining the other:
// class2.h
class class1;
class class2 {
...
}
By the way, i think you''re mistaken about the ''friend'' keyword. A friend class can use the private/protected functions and members of the other. If all you want is to have a class to be a parameter of a function of another class, they don''t need to be friends.
Hope that helps.
See this article.
“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian W. Kernighan (C programming language co-inventor)
[edited by - Fruny on February 29, 2004 12:46:36 PM]
class Foo;class Bar{public: int DoStuff(const Foo &arg); // Note - reference};class Foo{public: int DoStuff(const Bar &arg); // Note - reference};
“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian W. Kernighan (C programming language co-inventor)
[edited by - Fruny on February 29, 2004 12:46:36 PM]
quote:Original post by load_bitmap_fileDo the #ifndef, #define, #endif thing to make sure it is only included once (or simply #pragma once if you are using VC++). You shouldn''t have to declare the classes friends of each other just to have an instance of one in the other. The friend keyword is for when you want one class to be able to modify and access private members of another class without providing accessor functions.
Hello, I''m trying to get two classes, "Class1" and "Class2" to be friends with each other. (to anyone that doesn''t know what I''m talking about, this must sounds hilarious) I have the the classes stored in "class1.h" and "class2.h" respectively and the function/constructor definitions in "class1.cpp" and "class2.cpp" repsectively.
I declared class2 a friend of class1, so I tried to add a member function to class1 with class2 as a parameter.
My problem is that the compiler doesn''t know what class2 is when I try to use it as a parameter in class1. I tried to #include "class1.h" but then I get a class1 redefinition error. Does anyone know how I can get the compiler to know that class2 is a class while avoiding that error? Please help! :[
thanks for all your responses! Problem solved :]
Blew/Raloth - I''m planning to get the member function to modify the other class'' variables, but I needed to stick that other class'' object in the parameter for the function first. (And thanks for your responses).
Blew/Raloth - I''m planning to get the member function to modify the other class'' variables, but I needed to stick that other class'' object in the parameter for the function first. (And thanks for your responses).
I''ve had the same problem in the past, never knew how to solve it. I would get his error: "Missing ; before ", so lets say I had a class Ship, and a class Asteroid, and declared an ship:
Ship theShip;
"Missing ; before theShip". Because asteroid and ship were #including each other. So now you''re telling me that all I have to do is declare an instance of the other class before defining it, and that will get allow me to declare instances anywhere else without errors?
Ship theShip;
"Missing ; before theShip". Because asteroid and ship were #including each other. So now you''re telling me that all I have to do is declare an instance of the other class before defining it, and that will get allow me to declare instances anywhere else without errors?
quote:Original post by EGD Eric
So now you're telling me that all I have to do is declare an instance of the other class before defining it, and that will get allow me to declare instances anywhere else without errors?
No, you forward declare the class so that the compiler knows the name when it comes across it. You can use that name for pointers and references (as they're the same size no matter what they point or refer to).
If you want to create an instance you will need the full declaration.
[edited by - petewood on March 1, 2004 10:26:37 AM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement