【发布时间】:2011-08-27 02:57:33
【问题描述】:
一个简化的例子:
有一个抽象模板类 GCont 代表一个通用容器
template <typename Item>
struct TList
{
typedef std::vector <Item> Type;
};
template <typename Item>
class GCont
{
protected:
typename TList <Item>::Type items;
public:
typedef Item type;
virtual ~GCont() = 0 {};
};
以及具有一个隐式参数的派生抽象模板类
template < typename Item, const bool par = true>
class GCont2 : public GCont <Item>
{
public:
GCont2 () : GCont <Item> () {}
virtual ~GCont2() = 0 {};
};
及其对指针的特殊化
template <typename Item, const bool par>
class GCont2 <Item *, par> : public GCont <Item *>
{
public:
GCont2 () : GCont <Item *> () {}
virtual ~Cont() {}
};
派生模板类Cont
template <typename Point, const bool par = true>
class Cont : public GCont2 <Point, par>
{
public:
Cont() : GCont2 <Point, par>() {}
virtual ~Cont() {}
};
指针的特化
template <typename Point, const bool par>
class Cont <Point *, par> : public GCont2 <Point *, par>
{
public:
Cont() : GCont2 <Point *, par> () {}
};
课点:
template <typename T>
class Point
{
protected:
T x, y, z;
};
是否可以编写一个具有通用形式参数的函数 test() 允许同时使用两者
Cont <Point <T> *> *points
和
Cont <Point <T> *, false> *points
在我的程序中
template <typename T>
void test (Cont <Point <T> *> *points)
{
std::cout << "test";
}
template <typename T>
void test2 (Cont <Point <T> *, false> *points)
{
std::cout << "test2";
}
int _tmain(int argc, _TCHAR* argv[])
{
Point <double> * p = new Point <double>();
Cont <Point <double> *, false> points;
test(&points); //Error
test2(&points); //OK
return 0;
}
翻译过程中出现如下错误:
Error 1 error C2784: 'void test(Cont<Point<T>*> *)' : could not deduce template argument for 'Cont<Point<T>*> *' from 'Cont<Point,par> *' g:\templates_example.cpp 27
感谢您的帮助...
更新状态:
我修剪了代码...但问题仍然存在...我的代码没有使用 MSVS 2010 编译时出现相同的错误。
我找到了容器的部分解决方案模板。
template <typename Container>
void test (Container *points)
{
std::cout << "test";
}
【问题讨论】:
-
我建议的第一件事是大量将此处发生的代码量减少到引发错误所需的最少。跨度>
-
您应该在这个问题中修剪代码,这样我们就不必仔细阅读所有这些了。另外,请参阅我的回答,您没有向我们提供 您 真正使用的正确代码。
标签: c++ templates default-value