【发布时间】:2010-10-30 00:44:33
【问题描述】:
我有一个关于模板的问题。我想要一个模板类,其中包含一个浮点数或双精度数的数组。
我可以编写一个复制它的 clone() 函数。没问题。但是,我想要另一个名为 cast() 的函数,它在 double 和 float 之间来回转换。之前已经讨论过这个问题,但我认为问题不一样:
stackoverflow.com/questions/714213/c-template-casting
我遇到的问题是编译器错误,而不是链接器错误。错误信息是:
main.cpp: In function `void caster()':
main.cpp:63: error: expected primary-expression before "double"
main.cpp:63: error: expected `;' before "double"
main.cpp:64: error: expected primary-expression before "float"
main.cpp:64: error: expected `;' before "float"
main.cpp:65: error: expected primary-expression before '>' token
main.cpp:65: error: expected primary-expression before ')' token
我转储了下面的代码。第 63、64 和 65 行是我用“Error here”注释的地方。
顺便说一下,我的编译器是“GNU C++ version 3.4.5 20051201 (Red Hat 3.4.5-2) (x86_64-redhat-linux) 由GNU C version 3.4.5 20051201 (Red Hat 3.4.5- 2)"。
google了一下,原来有人遇到过这个问题:
gcc.gnu.org/ml/gcc-help/2006-04/msg00022.html
这里有一个解决办法:
gcc.gnu.org/ml/gcc-help/2006-04/msg00023.html
但是当原发帖人问为什么它起作用时,答案不是很清楚:
gcc.gnu.org/ml/gcc-help/2006-04/msg00025.html
很遗憾,链接已失效,而且我没有第三版的 Stroustrup。现在,我有我的修复,我的代码工作。但是,Stackoverflow,为什么会起作用?
#include <stdio.h>
// =================== This would be the header ===================
template <class T>
class foo
{
public:
foo(const T val) {d_data = new double; *d_data = val;}
virtual ~foo() {delete d_data;};
foo* clone() const;
template<class U>
foo<U>* cast() const;
private:
double *d_data;
};
// =================== This would be the implementation of the class ===================
template<class T>
foo<T>* foo<T>::clone() const
{
return new foo<T>(*d_data);
}
template<class T>
template<class U>
foo<U>* foo<T>::cast() const
{
return new foo<U>(*d_data);
}
template class foo<float>;
template class foo<double>;
template foo<float>* foo<float>::cast() const;
template foo<float>* foo<double>::cast() const;
template foo<double>* foo<float>::cast() const;
template foo<double>* foo<double>::cast() const;
// =================== Using the class ===================
template <class T>
void caster()
{
foo<double> *f1 = NULL;
foo<float> *f2 = NULL;
foo<T> *f3 = NULL;
// I am looking at something that compiles
// I don't care about linking for now
// This will crash at runtime because of
// NULL, but that's just an example
f1->cast<double>(); // compiler OK
f1->cast<float>(); // compiler OK
f1->cast<T>(); // compiler OK
f2->cast<double>(); // compiler OK
f2->cast<float>(); // compiler OK
f2->cast<T>(); // compiler OK
f3->cast<double>(); // Error here
f3->cast<float>(); // Error here
f3->cast<T>(); // Error here
f3->foo<T>::template cast<double>(); // It works!
f3->foo<T>::template cast<float>(); // It works!
f3->foo<T>::template cast<T>(); // It works!
}
int main(int argc, char **argv)
{
return 0;
}
【问题讨论】:
-
抱歉链接不好,我没有足够的信誉点来发布网址