简体   繁体   中英

Passing a method as constructor parameter

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

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM