Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8176467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:11:22+00:00 2026-06-06T23:11:22+00:00

I have made two identical classes X and Y, with a pointer to each

  • 0

I have made two identical classes X and Y, with a pointer to each other. See the code below for X.h, Y.h is identical with all X’s and Y’s interchanged. This code gives however an error in my method Connect (error C2027: use of undefined type ‘Y’). In X.h, I have forward declared the class Y, but it doesn’t know that Y has a method named SetXPointer. Therefore I also need to forward declare this method, correct?

If I try to do this (adding the line Y::SetXPointer(X* pX_in); under the line class Y;), I get a compiler error C2761: ‘void Y::SetXPointer(X *)’ : member function redeclaration not allowed. Is there a way to use a public method of class Y in class X?

// X.h

#pragma once

#include "Y.h"

// Forward declaration
class Y;

class X
{
public:
    X(void) : data(24) {};
    ~X(void) {};
    int GetData() { return data; }
    void SetYPointer(Y* pY_in) { pY = pY_in; }
    Y* GetYPointer() { return pY; }
    void Connect(Y* Y_in) { pY = Y_in; Y_in->SetXPointer(this); }
private:
    int data;
    Y *pY;
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T23:11:24+00:00Added an answer on June 6, 2026 at 11:11 pm

    Don’t include the method body in the class body. Write both classes, and after both classes are complete, write the method implementations:

    class Y;
    class X {
      …
      void Connect(Y* Y_in);
      …
    };
    class Y {
      …
      void Connect(X* X_in);
      …
    };
    inline void X::Connect(Y* Y_in) {
      pY = Y_in;
      Y_in->SetXPointer(this);
    }
    inline void Y::Connect(X* X_in) {
      pX = X_in;
      X_in->SetXPointer(this);
    }
    

    That way, full information about how the objects of the class will be layed out in memory is available by the time the Connect method is implemented. And as a method in the class body and a method declared inline will both be inlined the same way, performance will be the same as well.

    The only downside is that you won’t be able to split these two classes over two headers in a reasonable way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made a simple test application for the issue, two winforms each containing
i have made two view classes in first view class i am making more
I have made two objects in googlesketchup. link1 .On the link you can see
I have two UITextFields in the App as IBOutlets and I have made a
I have made a list view containing two childs. Now i am trying to
I have two branches: trunk, production. I have found a problem in trunk, made
I have these two images. And... I made them into graysclae images and then
I have made an application for IPad in objective C. In this I am
I have made a code that read data from flash Nand (without filesystem). fd
I have three UITableViews that should look identical. Each is placed inside a different

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.