缺少模板参数错误

时间:2011-10-31 17:25:21

标签: c++ templates arguments

我有以下代码:

#ifndef CURRENCY_H_   
#define CURRENCY_H_   

class currency   
{   
public:   
    enum signType {plus, minus};   
    currency(signType theSign = plus, unsigned long theDollars = 0, unsigned int theCents = 0);    
    ~currency(){};   
    void setValue(signType, unsigned long, unsigned int);   
    void setValue(double);   
    signType getSign() const {return sign;};   
    unsigned long getDollars() const {return dollars;};    
    unsigned int getCents() const {return cents;};   
    currency add(const currency&) const;   
    currency& increment(const currency&);   
    void output() const;   

private:   
    signType sign;   
    unsigned long dollars;   
    unsigned int cents;   
}; 

#endif   

构造函数和方法setValue的实现是:

currency::currency(signType theSign, unsigned long theDollars, unsigned int theCents)
{
    setValue(theSign, theDollars, theCents);
}

void currency::setValue(signType theSign, unsigned long theDollars, unsigned int theCents)
{
    if(theCents > 99)
        throw invalid_argument("Cents should be < 100");

    this.sign = theSign;
    dollars = theDollars;
    cents = theCents;
}

当我尝试创建一个货币对象时:

currency cur = currency(minus, 2, 25);

我收到了错误:

error: expected primary-expression before ‘(’ token

我可以创建一个空的货币对象(没有错误),如:

currency cur;

但是当我调用方法setValue:

cur.setValue(minus, 2, 25);

错误再次出现:

error: missing template arguments before ‘,’ token

有任何建议/想法吗?

3 个答案:

答案 0 :(得分:3)

你有重叠的符号名称。您的编译器认为您想要的minusstd::minus<T>。您想使用currency::minus,因此您必须明确要求:

currency cur = currency(currency::minus, 2, 25);

答案 1 :(得分:2)

如果我没弄错的话,你在货币类之外使用你的代码示例。如果是这样,则枚举值“减号”未定义(或者除了您以外的其他东西)。要实际引用你的signType枚举,你必须使用正确的范围,即:

cur.setValue(currency::minus, 2, 25);

编辑:构造函数相同:

currency(currency::minus, 2, 25);

在课程内部,您当然可以参考 minus

答案 2 :(得分:2)

尝试currency::minus

你有using namespace std吗?其中有一个std::minus