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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:06:25+00:00 2026-05-22T19:06:25+00:00

I’ve been trying to create a new variable of type Cmplx (which is my

  • 0

I’ve been trying to create a new variable of type Cmplx (which is my class name) whose data values are 0 and arg()(arg being a method of my class)
Thing is that the resulting variable has a value of 0,0.
Is there any work around or am I doing something wrong?

Also, the code:

#pragma once
#include <conio.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <cmath>
using namespace std;

template <typename vartype>
class Cmplx
{
public:
vartype Re, Im;
Cmplx(vartype const Real, vartype const Imag)//constructor
{
    Re=Real;
    Im=Imag;
}
long double arg()//called method
{
    return atan2(Im,Re);
}
long double abs()
{
    return sqrt(pow(Re,2)+pow(Im,2));
}
Cmplx<long double> log()//target method
{
    Cmplx<long double> IArg(0,arg());
    return IArg+log(abs());
}
Cmplx operator +(Cmplx const param)
{
    Cmplx Tmp;
    Tmp.Im=Im+param.Im;
    Tmp.Re=Re+param.Re;
    return Tmp;
}
Cmplx operator +(vartype const param)
{
    Cmplx Tmp;
    Tmp.Re=Re+param;
    return Tmp;
}
friend Cmplx operator +(vartype const param, Cmplx const para)
{
    Cmplx Tmp;
    Tmp.Re=para.Re+param;
    return Tmp;
}
friend ostream& operator << (ostream &tmp, Cmplx const &param)
{
    tmp<<param.Re<<"+"<<param.Im<<"i";
    return tmp;
}
friend istream& operator >> (istream &tmp, Cmplx &param)
{
    tmp>>param.Re;
    tmp>>param.Im;
    return tmp;
}
};
template <>
class Cmplx<string>
{
public:
Cmplx()
{
    cout << "Are you crazy or something?, a complex NUMBER with LETTERS as real part and imaginary part?"
    << "\n" << "Damn you should go to school dude." << endl;
}
};
template <>
class Cmplx<char>
{
public:
Cmplx<string> tmp;
};

template <typename type>
long double abs(Cmplx<type> param)
{
    long double tmp;
    tmp=sqrt(pow(param.Re,2)+pow(param.Im,2));
    return tmp;
}
template <typename type>
long double arg(Cmplx<type> param)
{
    return atan2(param.Im,param.Re);
}
template <typename type>
Cmplx<long double> exp(Cmplx <type> param)
{
    Cmplx<long double> tmp, exim(cos(param.Im),sin(param.Im));
    tmp=exp(param.Re)*exim;
    return tmp;
}
template <typename type>
Cmplx <long double> log(Cmplx<type> param)
{
    Cmplx<long double> IArg(0,arg(param));
    return IArg+log(abs(param));
}
template <typename type, typename paramT>
Cmplx<long double> log_b(Cmplx<type> arg, paramT param)
{
    return log(arg)/log(param);
}

An implementation of that class:

#include"cmplx oper.hpp"
using namespace std;

int main()
{
Cmplx<long double> A(2,3);
cout << log(A);
getch();
return true;
}

The result is: 1.28247+0i
But it was supposed to be 1.28247+0.98279i

  • 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-22T19:06:26+00:00Added an answer on May 22, 2026 at 7:06 pm

    It looks like you don’t actually want to pass the method itself – you want to call the method and pass the returned value into the constructor. That’s exactly what you’re doing here, and it should be fine. In other words, what you’ve got is already broadly equivalent to:

    Cmplx<long double> log()
    {
        long double tmp = arg();
        Cmplx<long double> IArg(0, tmp);
        return IArg + log(abs());
    }
    

    I suspect that something else is going wrong – such as your object not having the data you think it does to start with. I suggest you step through the code in a debugger, add some diagnostic logging, or add some unit tests to validate this class and then whatever’s using it.

    (As an aside, it looks odd to me to use Pascal-case for the variables like this. I haven’t seen any C++ conventions which do that…)

    EDIT: I suspect this is the problem:

    friend Cmplx operator +(vartype const param, Cmplx const para)
    {
        Cmplx Tmp;
        Tmp.Re=para.Re+param;
        return Tmp;
    }
    

    Notice how you never use any part of para other than para.Re, and never assign to Tmp.Im at all. I suspect you want:

    friend Cmplx operator +(vartype const param, Cmplx const para)
    {
        Cmplx Tmp = para;
        Tmp.Re += param;
        return Tmp;
    }
    

    or possibly just:

    friend Cmplx operator +(vartype const param, Cmplx const para)
    {
        return Cmplx(para.Re + param, para.Im);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
I'm looking for suggestions for debugging... If you view this site in Firefox or
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,

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.