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

  • Home
  • SEARCH
  • 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 8975031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:47:07+00:00 2026-06-15T18:47:07+00:00

i have two classes A and B A.hpp #include <vector> #include <algorithm> #include B.hpp

  • 0

i have two classes A and B

A.hpp

#include <vector>
#include <algorithm>
#include "B.hpp"

class A {
public:
  void sortTrans() { std::sort(trans_.begin(), trans_.end(), sortStruct); }
  unsigned int name() { return name_; }
private:
  std::vector<B*> trans_;
  unsigned int name_;
};

B.hpp:

class A;

class B {
  A& source_;
  A& dest_;
  unsigned int choice_;
};

Now I want to sort trans_ by the values of choice and name, therefore i wrote

struct sort {
  bool operator()(B* t1, B* t2) {
    if (t1->choice < t2->choice)
        return true;
    if (t1->dest_.name() < t2->dest_.name())
        return true;
    return false;
  }
} sortStruct;

But now I’m facing a problem to break the circular dependency. The definition of A is in A.hpp and the one of B in B.hpp. In B.hpp I use a forward decleration of A and A includes B.hpp. But where (or how) do i have to put the sortStruct, since it uses the definition of either, A and B. And I’m always getting the error

Wrong usage of forward declaration A

Thanks for help.

  • 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-15T18:47:08+00:00Added an answer on June 15, 2026 at 6:47 pm

    Both headers can use forward declation, since neither really (needs to) depend on the other.

    A.hpp

    #ifndef A_HPP
    #define A_HPP
    #include <vector>
    
    class B;
    
    class A {
    public:
      void sortTrans();
      unsigned name();
    private:
      std::vector<B*> trans_;
      unsigned int attr1_;
      unsigned int attr2_;
    };
    #endif
    

    B.hpp

    #ifndef B_HPP
    #define B_HPP_
    class A;
    
    class B {
      A& source_;
      A& dest_;
      unsigned choice_;
    };
    #endif
    

    A.cpp

    #include "A.hpp"
    #include "B.hpp"
    #include <algorithm>
    
    // I can't really define this with your B as given ...
    struct SortB {
        bool operator()(B *x, B *y) {
            if (x->choice_ < y->choice_)
                return true;
            if (x->dest_.name() < y->dest_.name())
                return true;
            return false;
        }
     };
    
    void A::sortTrans()
    {
        std::sort(trans_.begin(), trans_.end(), SortB());
    }
    

    Note I still haven’t shown how B::choice_ and B::dest_ are accessed, because this is a design decision and I don’t have enough information to make a good guess.

    You can either expose them as public (in which case B is basically a struct), add accessor members to B, or forward-declare SortB in B.hpp as a friend.

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

Sidebar

Related Questions

I have two classes: public class CourseModule { // attributes... List<Course> courses; public void
I have two classes : import android.cla; public class CW { public static void
I have two classes: class Object { public: Object(); virtual void update(); virtual void
I have two classes: class Player { public string Id { set; get; }
I have two classes. UserModel and UserController. file: localhost/controllers/user.controller.php <?php class UserController { public
I have two classes below: public class Module { public int Id { get;
I have two classes, and want to include a static instance of one class
I have two classes public class A { public int BaseA {get;set;} } public
I have two classes: class A { public: int i; }; class B :
I have two classes defined like public class PostleitzahlList : ObservableCollection<Postleitzahl> { } public

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.