随笔 - 51, 文章 - 1, 评论 - 41, 引用 - 0
数据加载中……

vc2005无法编译通过《从集合中枚举子集》中的代码

        前些日子整理代码,发现《从集合中枚举子集》中的代码无法在VC2005上编译通过。当时只在DevC++上编译,没有考虑VC2005gcc的区别。仔细查找,问题出在CSetIterAgent的构造函数的参数上。下面是一段简化的代码,会出现同样的问题。
#include <vector>
template
<typename Type>
class CTest
{
public:
    enum Data
    {
        A,
        B
    };
public:
    CTest(
const std::vector<Type>& set, CTest::Data data=CTest::A){};
    ~CTest(){};
};

int main(void)
{
    std::vector
<int> set;
    CTest
<int> test(set);
    CTest
<int>::Data a = CTest<int>::A;
    return 
0;
}
用vc2005编译出现的错误如下
1>d:\work\c++\vc\qt\qt\main.cpp(12) : warning C4346: 'CTest<Type>::Data' : dependent name is not a type
1>        prefix with 'typename' to indicate a type
1>        d:\work\c++\vc\qt\qt\main.cpp(14) : see reference to class template instantiation 'CTest<Type>' being compiled
1>d:\work\c++\vc\qt\qt\main.cpp(12) : error C2226: syntax error : unexpected type 'std::vector<Type>'
1>d:\work\c++\vc\qt\qt\main.cpp(12) : error C2334: unexpected token(s) preceding '{'; skipping apparent function body
1>d:\work\c++\vc\qt\qt\main.cpp(12) : fatal error C1903: unable to recover from previous error(s); stopping compilation
1>        d:\work\c++\vc\qt\qt\main.cpp(19) : see reference to class template instantiation 'CTest<Type>' being compiled
1>        with
1>        [
1>            Type=int
1>        ]
但在gcc下可以通过。如果把代码
CTest(const std::vector<Type>& set, CTest::Data data=CTest::A){};
改为
    CTest(const std::vector<Type>& set, CTest<Type>::Data data=CTest<Type>::A){};
问题同样。如果改成下面形式
    CTest(const std::vector<Type>& set){};
则vc2005和gcc都可以通过。

         有一句忘了出处的名言:“我知道万有引力是怎么工作的,但我不知道它为什么这样工作”。现在我也知道怎样写会有问题,但我不知道为什么会有这样的问题。希望能和大家多多交流,我的邮箱lemene@sina.com

posted on 2007-11-09 16:54 lemene 阅读(357) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理