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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:39:39+00:00 2026-06-04T20:39:39+00:00

Okay, so I’m running into trouble with Forward Declarations in Visual Studios C++ (C++/CLI).

  • 0

Okay, so I’m running into trouble with Forward Declarations in Visual Studios C++ (C++/CLI).
The code is as follows:

A.h

#include "B.h"

#ifdef B_H
#pragma once

public ref class A : public base_class  //base_class is public, memory managed 
{
    B^ b;
}

#endif

B.h

#define B_H

#pragma once

ref class A;

ref class B 
{
    A^ a;
}

#include "A.h"

The #ifdef/#pragma guards should keep be keeping a *.h from being read twice, and forcing b.h to be read first, and from the compiler output I’m pretty sure they are. (I’m not even sure the #ifdef/#define is needed with the #pragma once and #include placement)

But, the complier complains of a path/a.h: error C2011: ‘class’ type redefinition.
See file path/B.h

Should I be doing something with the forward declaration of A because it’s a derivative class in the actual class definition, or am I barking up the wrong tree?

  • 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-04T20:39:40+00:00Added an answer on June 4, 2026 at 8:39 pm

    Two changes needed:

    1. Add semicolons after the closing brace of the class definitions.
    2. In A.h, move the #pragma once to be the very first line in the file. It’s getting screwed up by having this inside the #ifdef block.

    Also, note that a simpler way to do this would be to not have either header file include the other, and use forward declarations in both files:

    A.h:

    #pragma once
    ref class B;
    public ref class A : public base_class  //base_class is public, memory managed 
    {
        B^ b;
    };
    

    B.h

    #pragma once
    ref class A;
    ref class B 
    {
        A^ a;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay so my question is this. Say I have a simple C++ code: #include
Okay, I've set up a bit of code which searching for all the pages
Okay, so I have the weirdest problem right now. My code is fine! It
Okay, I have a WinForms C# program I am writing in Visual Studio 2010
Okay, I'm reading about Linux kernel development and there are some code snippets using
Okay, so I'm running a small test webserver on my private network. I've got
Okay, so I'm doing my first foray into using the ADO.NET Entity Framework. My
Okay so I got my page up and running and grabbing JSON and outputting
Okay so I have two import pieces of code involved in this. This first
Okay, I'm porting some code that used the GitHub API v2 to v3 and

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.