将方法作为构造函数参数传递
我一直在尝试创建一个 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 ¶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);
}
该类的实现:
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您实际上并不想传递方法本身 - 您想调用该方法并将返回值传递到构造函数中。这正是你在这里所做的,应该没问题。换句话说,你所得到的已经大致相当于:
我怀疑其他地方出了问题 - 例如你的对象没有你认为它一开始就有的数据。我建议您在调试器中单步执行代码,添加一些诊断日志记录,或添加一些单元测试来验证此类,然后再验证使用它的任何内容。
(顺便说一句,对我来说,对这样的变量使用 Pascal 大小写看起来很奇怪。我还没有看到任何 C++ 约定可以做到这一点...)
编辑:我怀疑这就是问题所在:
注意你如何从不使用除了
para.Re
之外的para
的任何部分,并且根本不分配给Tmp.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:
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:
Notice how you never use any part of
para
other thanpara.Re
, and never assign toTmp.Im
at all. I suspect you want:or possibly just: