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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T16:49:07+00:00 2026-05-14T16:49:07+00:00

I am a C++ beginner. I have the following code, the reult is not

  • 0

I am a C++ beginner. I have the following code, the reult is not what I expect. The question is why, resp. what is wrong. For sure, the most of you see it at the first glance.

struct Complex {
    float imag;
    float real;
    Complex( float i, float r) {
        imag = i;
        real = r;
    }
    Complex( float r) {
        Complex(0, r);
    }
    std::string str() {
        std::ostringstream s;
        s << "imag: " << imag << " | real: " << real << std::endl;
        return s.str();
    }
};
class Complexes {
    std::vector<Complex> * _complexes;
public:
    Complexes(){
        _complexes = new std::vector<Complex>;
    }
    void Add( Complex elem ) {
        _complexes->push_back( elem ); 
    }
    std::string str( int index ) {
        std::ostringstream oss;
        Complex c = _complexes->at(index);
        oss << c.str();
        return oss.str();
    }
};
int main(){
    Complexes * cs = new Complexes();
    //cs->Add(123.4f);
    cs->Add(Complex(123.4f));
    std::cout << cs->str(0); return 0; }

for now I am interested in the basics of c++ not in the complexnumber theory 😉
it would be nice if the “Add” function does also accept one real (without an extra overloading) instead of only a Complex-object is this possible?

many thanks in advance
Oops

  • 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-14T16:49:08+00:00Added an answer on May 14, 2026 at 4:49 pm

    You can’t call one constructor in the body of another one like that:

    Complex( float r) {
            Complex(0, r);
        }
    

    In C++ it creates a temporary object of class Complex which immediately gets destroyed.

    You could use default parameters in the constructor or some private method that would be called by constructors

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

Sidebar

Related Questions

This is a beginner level question for asp.net MVC I have the following code
Excuse the beginner level of this question. I have the following simple code, but
I'm following a tutorial. (Real World Haskell) And I have one beginner question about
iOS beginner here. I have the following code: [facebook authorize:nil delegate:self]; NSString *string1=[facebook accessToken];
I am a beginner of python and have a question, very confusing for me.
I'm an absolute beginner, you see. Say I have a string object on the
I have the following code: $(document).ready(function() { var id = 'cbx'; var idPrefix =
I have the following code: using System; using System.Collections.Generic; using System.Linq; using System.Text; using
The following code compiles fine, but does not allow the user to choose whether
i have the following code trying to save the contents of a JTextPane as

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.