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 9005151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:04:16+00:00 2026-06-16T01:04:16+00:00

I have the following two classes: Foo which has constructor Foo(Bar*, int, int) and

  • 0

I have the following two classes:

Foo which has constructor Foo(Bar*, int, int)

and Bar which calls new Foo(this, int, int)

I assumed that forward declaring Foo in Bar.h and/or Bar in Foo.h would solve the issue.

(The error returned is “undefined reference” on new Foo)

I”m using Clang to compile.

The linking order as it stands (but the same error happens in both cases) is Foo then Bar.

Any thoughts as to what I am doing wrong?

The code is roughly as follows. I cannot display any real code fragments unfortunately

  #include Bar.h 

 class Bar ; 

 class Foo { 
 public: 
 Foo(Bar* bar, int arg1, int arg2) ; 
 void method1()  {
     I access bar->field here
 }

And the code for Bar is then

  #include Foo.h 



  class Bar { 
  public:

   Bar() { 
    Cache* cache = new Cache(this, 0, 0) ; 
  } 
  • 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-16T01:04:17+00:00Added an answer on June 16, 2026 at 1:04 am

    It should look something like this (leaving out include guards):

    foo.h

    class Bar;
    
    class Foo {
      public:
        Foo(Bar*, int, int);
    };
    

    bar.h

    class Bar {
      public:
        Bar();
    };
    

    foo.cc

    #include "foo.h"
    // Note: no need to include bar.h if we're only storing the pointer
    
    Foo::Foo(Bar*, int, int) { ... }
    

    bar.cc

    // Note: the opposite order would also work
    #include "bar.h"
    #include "foo.h"
    
    Bar::Bar() {
      new Foo(this, int, int);
    }
    

    If you’re getting “undefined reference” from the linker, you probably declared Foo::Foo with a different signature than you defined it with, or did not define it at all, or are not linking with the object file it’s compiled into.

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

Sidebar

Related Questions

Consider that I have the two following nested classes: public class Foo { public
I have the following two classes. public class Foo { public Bar Args {
I have the following problem. I have two classes, in this case A and
I have two classes: public class Foo { public int FooId {get;set;} public virtual
I have the following two classes: public class Animal { public Animal() { this(Animal->Horse);
I have three classes organized in following manner. Foo is a template class, Bar
In C++ I have the following two classes that I expose (using Boost) to
I have the following issue. I have two classes that manipulate information but they
I have the following two classes, one inherits from the other Class A{ void
I have the following two classes: public class Address { public string AddressLine1 {

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.