Jump to content
  • Advertisement


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


Definition problem :(

This topic is 5682 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''ve got a little problem that you probably all know to fix, since I''m pretty new with c++. I have defined a type struct and a class... In the class I use this type, but in the type struct constructor the class is given in the parameters. My problem: Either the class doesn''t know the type or the type doesn''t know the class Marty

Share this post

Link to post
Share on other sites
Just some advice:
It sounds like you are attempting to create member functions for a structure. If you find yourself doing this, consider making the structure a class. Overloading operators is an exception to this rule.
You might want to reconsider your design if you come across a, "chicken or the egg" scenario. Think in terms of how the objects relate. Does one use the other? Only in very rare cases would two classes require mutual access.
However, it is possible to do what you have described. I believe the following code is what you were attempting:

// A Strucure with a member function is a bad idea.
// Overloading an operator for a structure is ok.
struct MyStruct {
MyStruct(class CFoo &);
virtual ~MyStruct();

MyStruct::MyStruct(class CFoo &rFoo)


class CFoo {
virtual ~CFoo();

// Allows MyStruct to access Private data.
friend class MyStruct;
MyStruct *pMs;

pMs = new MyStruct(*this);

if (pMs)
delete pMs;

// End of code.

Again, seriously reconsider your design. This code should be used as a last resort. Classes that use one another is a sign of relationship issues. When designing classes, think in terms of, "Has a" or "Is a" relationships. Hope this helps.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!