template <class T> class circuit{
private:
vector<T> components;
string type;
public:
complex<double> getImpedance();
};
complex<double> circuit<circuitComponent>::getImpedance() {
// function to calculate impedance in trivial case of components all in series/parallel
}
complex<double> circuit<circuit>::getImpedance() {}
我正試圖編寫一個程序,它能夠計算任意電阻,電容和電感電路的(複數)阻抗。遞歸模板參數
對於一個電路,其中所有組件相互串聯/並聯,這是微不足道的。但是,這通常不正確。對於具有混合串聯和並聯元件的更復雜電路,我希望能夠將電路描述爲電路集合(即子電路/嵌套電路)。
所以,我想寫一個電路類,可以包含一個向量組件,或一個電路矢量。我試圖編寫一個模板類來做到這一點,但是當我嘗試爲電路電路(我正計劃以遞歸方式)的情況下定義getImpedance()函數時,我得到一個錯誤,因爲編譯器不會'不知道什麼類型的子電路(即在電路中,內部電路沒有已知類型)。
所以,我的問題是,有沒有辦法遞歸提供模板參數?
任何幫助將不勝感激。
請問
編輯:我的完整代碼到目前爲止是
#include<iostream>
#include<string>
#include<stdlib.h>
#include<cmath>
#include<vector>
#include <type_traits>
#include<complex>
using namespace std;
// Declare pi as const.
const double pi(3.1415927);
// A class for general circuit components.
class circuitComponent {
protected:
// The component type, its impedance, and the frequency of the AC current passing through it.
string componentType;
complex<double> impedance;
double frequency;
public:
circuitComponent(string compName, complex<double> imp, double freq) : componentType(compName), impedance(imp), frequency(freq) {}
virtual ~circuitComponent() {};
virtual void setImpedance(const complex<double>&) = 0;
// Functions to return the impedance and the frequency.
virtual complex<double> getImpedance() = 0;
virtual double getFrequency() = 0;
};
// A class for resistors, derived from circuitComponent.
class resistor : public circuitComponent{
protected:
double resistance;
public:
resistor() : circuitComponent("resistor",complex<double>(0,0), 0), resistance(0) {}
resistor(double res, double freq) : circuitComponent("resistor", complex<double>(res, 0), freq), resistance(res) {}
~resistor() {}
// A function to set the impedance of the component. Also alters the resistance accordingly.
void setImpedance(const complex<double>& imp){
// Deals with case of non-pure-real impedance assignment.
if (imp.imag() != 0){
cerr << "Cannot assign impedance with non-zero imaginary component to a resistor." << endl;
}
else {
resistance = imp.real(); impedance = resistance;
}
}
// Functions to return the impedace and the frequency of the resistance.
complex<double> getImpedance() { return impedance; }
double getFrequency() { return frequency; }
};
// A class for capacitors.
class capacitor : public circuitComponent{
private:
double capacitance;
public:
capacitor() : circuitComponent("capacitor", complex<double>(0, 0), 0), capacitance(0) {}
capacitor(double cap, double freq) : circuitComponent("capacitor", complex<double>(0, -1/(2.0*pi*frequency*cap)), freq), capacitance(cap) {}
~capacitor() {}
void setImpedance(const complex<double>& imp){
if (imp.real() != 0){
cerr << "Cannot assgn impedance with non-zero real component to a capacitor." << endl;
}
else{
impedance = imp; capacitance = imp.imag(); // THIS IS INCORRECT
}
}
complex<double> getImpedance() { return impedance; }
double getFrequency() { return frequency; }
};
// A class for inductors.
class inductor : public circuitComponent{
private:
double inductance;
public:
inductor() : circuitComponent("inductor", complex<double>(0, 0), 0), inductance(0) {}
inductor(double ind, double freq) : circuitComponent("inductor", complex<double>(0,2*pi*frequency*ind), freq), inductance(ind) {}
~inductor() {}
void setImpedance(const complex<double>& imp){
if (imp.real() != 0){
cerr << "Cannot assign impedance with non-zero real component to an inductor." << endl;
}
else{
impedance = imp; inductance = imp.imag(); // THIS IS INCORRECT
}
}
complex<double> getImpedance() { return impedance; }
double getFrequency() { return frequency; }
};
template <class T> class circuit{
private:
vector<T> components;
string type;
public:
complex<double> getImpedance();
};
// For the case where the circuit (or sub-circuit) has no subcircuits, and hence is a collection of components that are ALL in parallel/series with one another.
complex<double> circuit<circuitComponent>::getImpedance() {
complex<double> impedance(0);
vector<circuitComponent>::iterator componentIter;
if (type == "s"){
for (componentIter = components.begin(); componentIter != components.end(); ++componentIter){
impedance += componentIter->getImpedance();
}
}
else if (type == "p") {
complex<double> reciprocolImpedance(0);
for (componentIter = components.begin(); componentIter != components.end(); ++componentIter){
// complex<double>(1,0) is just the number 1 expressed as a complex number.
reciprocolImpedance += (complex<double>(1,0)/componentIter->getImpedance());
}
impedance = (complex<double>(1, 0))/reciprocolImpedance;
}
return impedance;
}
// THIS is the problem line.
complex<double> circuit<circuit>::getImpedance() {}
int main(){
string exitStr;
cin >> exitStr;
return 0;
}
你的問題不完整..請發佈完整的代碼...並且還描述了什麼你想 – Arvindsinc2
閱讀http://stackoverflow.com/help/mcve,並編輯你的問題 – Sniper