0
即時STUDING在C++的模板,並根據這個教程:http://www.cprogramming.com/tutorial/templates.htmlC++模板類編譯錯誤:預期的init聲明符之前 '<' 令牌
我提出的類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;
}
上編譯(克++ main.cpp中-o t檢驗)我得到這個錯誤:
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
我不能發現什麼是錯的
謝謝,多麼愚蠢的錯誤!我認爲編譯器的輸出讓我走錯了路。 – user2479113
是的,模板錯誤可能會造成混淆。嘗試使用Clang而不是GCC,它通常具有更好的錯誤消息。或者將你的GCC升級到4.8,這也有很大的改進。順便說一句,如果答案解決了您的問題,請點擊表決下方的勾號將其標記爲已接受。 –
@SebastianRedl你能解釋一下init-declarator是什麼意思嗎? –