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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:17:00+00:00 2026-05-19T23:17:00+00:00

For example i have two classes A and B, such that for two objects

  • 0

For example i have two classes A and B, such that for two objects a and b, i want to be able to do :
A a;
B b;
a = b;
b = a;

for this i have overloaded the = operator, and the typecast operators as:

class A{
-snip-
    operator B()const { return B(pVarA); }
};
class B{
-snip-
    operator A()const { return A(pVarB); }
};

but when i try to compile this code, gcc throws the error :
error: expected type-specifier before 'B'
for the line: operator B()const { return B(pVarA);}

my guess is, this is due to a chicken and egg problem as class B is defined after class A.

Is there a way to circumvent this while still using the overloaded typecast operators. And if not, then what might be the best way to achieve my goals.

Any help will be appreciated. Thanks in advance.

  • 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-05-19T23:17:01+00:00Added an answer on May 19, 2026 at 11:17 pm

    Try forward declaring then supplying the actual function definitions later on:

    class B;
    
    class A{
    -snip-
        operator B()const;
    };
    class B{
    -snip-
        operator A()const;
    };
    
    inline A::operator B() const
    {
        return B(pVarA);
    }
    
    inline B::operator A() const
    {
        return A(pVarB);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have for example two classes A and B , such that class
Example: I have two shared objects (same should apply to .dlls). The first shared
I have two List's which I want to check for corresponding numbers. for example
I am looking to find a way to take two objects that have identical
for example, we have two classes for parsing the resumes, one to parse Excel
I have two classes. One is a management class, which stores a bunch of
I have two classes. The base class is A. The inherited class is B.
I have two Java class hierarchies that share a common ancestor and implement a
Suppose you have two classes, as in the example below. How would you modify
I have the following issue. I have two classes that manipulate information but they

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.