[英]Passing a method as constructor parameter
我一直在嘗試創建一個Cmplx
類型的新變量(這是我的 class 名稱),其數據值為 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);
}
class 的實現:
#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
看起來您實際上並不想傳遞方法本身 - 您想調用該方法並將返回的值傳遞給構造函數。 這正是你在這里所做的,應該沒問題。 換句話說,你所擁有的已經大致相當於:
Cmplx<long double> log()
{
long double tmp = arg();
Cmplx<long double> IArg(0, tmp);
return IArg + log(abs());
}
我懷疑還有其他問題 - 例如您的 object 沒有您認為它開始的數據。 我建議您在調試器中單步執行代碼,添加一些診斷日志記錄,或添加一些單元測試來驗證這個 class,然后再使用它。
(順便說一句,對像這樣的變量使用 Pascal-case 對我來說看起來很奇怪。我還沒有看到任何 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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.