C++模板类编译错误: expected init-declarator before '<' token

标签 c++ templates compiler-errors

我正在研究 C++ 中的模板,并根据本教程:http://www.cprogramming.com/tutorial/templates.html

我创建了类 CalcTempl.h

#ifndef CALC_TEMPL_H
#define CALC_TEMPL_H

template <class A_Type> class CalcTempl
{
  public:
    A_Type multiply(A_Type x, A_Type y);
    A_Type add(A_Type x, A_Type y);
};

template <class A_Type> A_Type calc<A_Type>::multiply(A_Type x,A_Type y)
{
  return x*y;
}
template <class A_Type> A_Type calc<A_Type>::add(A_Type x, A_Type y)
{
  return x+y;
}

#endif

和main.cpp

#include <iostream>
#include "CalcTempl.h"

using namespace std;

int main(){

  CalcTempl<double> c2;

  double d1 = 5;
  double d2 = 4;

  double c2r1 = c2.add(d1, d2);

  cout << " C2 Result: " << c2r1 << "\n";

  return 0;
}

在编译(g++ main.cpp -o ttest)时我得到了这个错误:

CalcTempl.h:11: error: expected init-declarator before '<' token
CalcTempl.h:11: error: expected `;' before '<' token
CalcTempl.h:15: error: expected init-declarator before '<' token
CalcTempl.h:15: error: expected `;' before '<' token

我找不到问题所在

最佳答案

您的类称为 CalcTempl,但在您实现其成员的地方,您尝试将其称为 calc。那行不通。

关于C++模板类编译错误: expected init-declarator before '<' token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17085263/

相关文章:

c++ - 将代码移出类定义时未扩展参数包

c++ - 使用动态 bool 变量实例化模板

jquery - Mustache.js - 如何连接数据?

c++ - 类元组类型(元组、对)的模板重载

java - “找不到符号”或“无法解析符号”错误是什么意思?

swift - Swift 4 中的 Watson Assistant 产生错误

c++ - 使用索引 VBO 的正确方法

c++ - 就处理器时间而言,fmod 有多昂贵?

c++ - const 到非 const 迭代器比较,它们是否有效

excel - 编译错误: Argument not optional error when I call macro from other macro