将方法作为构造函数参数传递

发布于 2024-11-10 11:05:45 字数 2773 浏览 4 评论 0原文

我一直在尝试创建一个 Cmplx 类型的新变量(这是我的类名),其数据值为 0 和 arg()(arg 是我的方法)班级) 问题是结果变量的值为 0,0。 有什么解决方法或者我做错了什么吗?

另外,代码:

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

该类的实现:

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

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

结果是:1.28247+0i 但它应该是1.28247+0.98279i

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 ¶m)
{
    tmp<<param.Re<<"+"<<param.Im<<"i";
    return tmp;
}
friend istream& operator >> (istream &tmp, Cmplx ¶m)
{
    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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

且行且努力 2024-11-17 11:05:45

看起来您实际上并不想传递方法本身 - 您想调用该方法并将返回值传递到构造函数中。这正是你在这里所做的,应该没问题。换句话说,你所得到的已经大致相当于:

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

我怀疑其他地方出了问题 - 例如你的对象没有你认为它一开始就有的数据。我建议您在调试器中单步执行代码,添加一些诊断日志记录,或添加一些单元测试来验证此类,然后再验证使用它的任何内容。

(顺便说一句,对我来说,对这样的变量使用 Pascal 大小写看起来很奇怪。我还没有看到任何 C++ 约定可以做到这一点...)

编辑:我怀疑这就是问题所在:

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

注意你如何从不使用除了 para.Re 之外的 para 的任何部分,并且根本不分配给 Tmp.Im。我怀疑你想要:

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

或者可能只是:

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

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文