Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

load_bitmap_file

C++ - can't make class object parameter for another class member

This topic is 5227 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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! :[

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
See this article.


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]

Share this post


Link to post
Share on other sites
quote:
Original post by load_bitmap_file
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! :[
Do 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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!