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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:34:38+00:00 2026-05-23T13:34:38+00:00

I know the deal .The compiler tries to convert one object into the other

  • 0

I know the deal .The compiler tries to convert one object into the other objects’s type with the help of conversion operator .Two ways to do this .Constructor (Converts a class to the other) or conversion operator .So , this one is just to test if I am thorough with the concepts .The code below gives the error

using namespace std ;
class A
{
    int i ;
    public:
            A(int a=0){this->i=a;}
            A operator+(const A& b){
                    A c ;
                    return c(this->i+b.i);
            }
            void show()
            {
                    cout<<i<<endl;
            }
 };
int main()
{
    A a1(1),a2(2),a3;
    a3=a2+a1;
    a3.show();
    return 0;
 }

I guess the error is in the operator + .When I try to assign A(i) .There is no match for an operator which could create an A from an int .

But Then I see this A’s constructor lurking behind .It can convert an int into an A .Suppose , it does convert int into an A.Then , the call becomes A(B) .This is equivalent to the copy constructor .Hence , this call should work .But it doesn’t .All in all , am pretty confused .

Please 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-05-23T13:34:39+00:00Added an answer on May 23, 2026 at 1:34 pm

    In these two lines you are telling the compiler to construct an A object with the default constructor, then call its nonexistent operator () (int) and return its return value:

                    A c ;
                    return c(this->i+b.i);
    

    Use either

    A c(i + b.i);
    return c;
    

    or

    return A(i + b.i);
    

    On a side note, an example for an implicit conversion operator, for your class:

    operator int () const
    {
        return i;
    }
    

    But their use smells like bad design, and can cause bad stuff to happen, like implicit conversion to bool or a pointer. Use something else instead, like an int toInt () const member function.

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

Sidebar

Related Questions

I don't know how to deal with f:valueChangeListener , I want to select country
I don't really know how to deal with the next issue : So, I've
I need to implement portable code, but I do not know how to deal
(not discussing the table design, its a done deal!) I know how to do
I'm building a interpreter/compiler for a school project (well now its turning into a
I'm really confused - but really it's pretty wierd because I know two programming
I have a small problem that I don't know how to deal with. I've
I created a new object() in Javascript, put some values into it and passed
So, I'm learning C++, and I've run into something which I know how to
Know this might be rather basic, but I been trying to figure out how

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.