我的代码中出现了一些错误,我无法弄明白。我将代码缩减为非常基本的简单函数/类调用,但仍然存在问题。
#include <iostream>
using namespace std;
template <class T>
class FC
{
private:
double netprofit, costofinvest;
double curras, invent, curliab;
public:
void ROI(double np, double ci)
{
netprofit = np; costofinvest = ci;
}
double getROI()
{
return (netprofit - costofinvest) / costofinvest;
}
void ATR(double ca, double inv, double cl)
{
curras = ca; invent = inv; curliab = cl;
}
double getATR()
{
return (curras - invent) / curliab;
}
};
int main()
{
FC ROI, AcidTestRatio;
ROI.ROI(27, 288);
cout << ROI.getROI() << endl;
AcidTestRatio.ATR(77, 2l, 344);
cout << AcidTestRatio.getATR() << endl;
return 0;
}
我得到的错误如下:
In function 'int main()':
39:22: error: missing template arguments before 'ROI'
41:2: error: 'ROI' was not declared in this scope
44:2: error: 'AcidTestRatio' was not declared in this scope
答案 0 :(得分:4)
你需要给出一个模板参数'T':
FC<float> ROI, AcidTestRatio;
但正如alain指出的那样,您还没有使用T,所以您也可以改为从代码的开头删除template <class T>
。
或者,您可能希望使用T作为成员的类型而不是double
:
template <class T>
class FC
{
private:
T netprofit, costofinvest;
T curras, invent, curliab;
public:
void ROI(T np, T ci)
{
netprofit = np; costofinvest = ci;
}
T getROI()
{
return (netprofit - costofinvest) / costofinvest;
}
void ATR(T ca, T inv, T cl)
{
curras = ca; invent = inv; curliab = cl;
}
T getATR()
{
return (curras - invent) / curliab;
}
};
<强>更新强>: Full example here