類型引用
以下是引用片段:
template
class Foo
{
typedef T::SomeType SomeType;
};
這段代碼在VC++中一點(diǎn)問題也沒有,但是GCC并不允許,因?yàn)樗恢繲::SomeType是什么。你需要改為:
以下是引用片段:
template
class Foo
{
typedef typename T::SomeType SomeType;
};
通過typename T::SomeType告訴GCC,SomeType是一個(gè)類型名,而不是其他東西。
當(dāng)然,這種情況不只是出現(xiàn)在typedef中。例如:
以下是引用片段:
template
void visit(const Container& cont)
{
for (Container::const_iterator it = cont.begin(); it != cont.end(); ++it)
...
}
這里的Container::const_iterator同樣需要改為typename Container::const_iterator。
基類成員引用
以下是引用片段:
template
class Foo : public Base
{
public:
void foo() {
base_func();
m_base_member = 0;
}
};
這段代碼在VC++中同樣沒有問題,但是GCC中不能通過。因?yàn)镚CC并不知道base_func,m_base_member是什么。對于這個(gè)問題,你可以有兩種改法:
改法1:加上域作用符Base::
以下是引用片段:
template
class Foo : public Base
{
public:
void foo() {
Base::base_func();
Base::m_base_member = 0;
}
};
以下是引用片段:
template
class Foo
{
typedef T::SomeType SomeType;
};
這段代碼在VC++中一點(diǎn)問題也沒有,但是GCC并不允許,因?yàn)樗恢繲::SomeType是什么。你需要改為:
以下是引用片段:
template
class Foo
{
typedef typename T::SomeType SomeType;
};
通過typename T::SomeType告訴GCC,SomeType是一個(gè)類型名,而不是其他東西。
當(dāng)然,這種情況不只是出現(xiàn)在typedef中。例如:
以下是引用片段:
template
void visit(const Container& cont)
{
for (Container::const_iterator it = cont.begin(); it != cont.end(); ++it)
...
}
這里的Container::const_iterator同樣需要改為typename Container::const_iterator。
基類成員引用
以下是引用片段:
template
class Foo : public Base
{
public:
void foo() {
base_func();
m_base_member = 0;
}
};
這段代碼在VC++中同樣沒有問題,但是GCC中不能通過。因?yàn)镚CC并不知道base_func,m_base_member是什么。對于這個(gè)問題,你可以有兩種改法:
改法1:加上域作用符Base::
以下是引用片段:
template
class Foo : public Base
{
public:
void foo() {
Base::base_func();
Base::m_base_member = 0;
}
};

