从具有多个参数的模板类继承时出错

时间:2012-07-29 22:02:23

标签: c++

目标:从具有2个模板参数的类继承。

错误

  

错误错误C2143:语法错误:在'<'

之前缺少','

守则

template< typename Type >
class AssetManager : public IsDerivedFromBase< Type, Asset* >
{
    // Class specific code here
};

您需要了解的事项:资产是一个只使用getter / setter封装char*的类,而IsDerivedFromBase将用于基本测试是否有Type { {1}}是Asset的衍生物。这些类集在自己的小型Visual Studio 2012项目中被隔离,并且一旦所有功能都经过彻底测试,它们将被集成到主项目中。

基于评论的部分修改

感谢您的帮助到目前为止,我真的很感激。以下是一些更具体的内容:

IsDerivedFromBase.h

#ifndef ISDERIVEDFROMBASE_H
#define ISDERIVEDFROMBASE_H

#include< iostream > // For access to NULL

namespace Fenrir
{
    template< typename Type, typename Base >
    class IsDerivedFromBase
    {
    private:
        static void Constraints( Type* _inheritedType )
        {
            Base* temp = NULL;

            // Should throw compiler error if
            // this assignment is not possible.
            temp = _inheritedType;
        }

    protected:
        IsDerivedFromBase( ) { void( *test )( Type* ) = Constraints; }
    };
}

#endif

注意:这个课程是基于我在一篇文章中读到的课程。从那以后我发现了一种更好的方法来达到预期的效果;但是,我希望了解这个错误源自何处。

AssetManager.h“

#ifndef ASSETMANAGER_H
#define ASSETMANAGER_H

#include "IsDerivedFromBase.h"

namespace Fenrir
{
    template< typename Type >
    class AssetManager : public IsDerivedFromBase< Type, Asset* >
    {
        // Class specific code
    };
}

#endif

类特定代码保持在最低限度,以使此帖子尽可能整洁,如果需要更多信息,请告诉我,我可以将其添加到:)。

2 个答案:

答案 0 :(得分:0)

当编译器遇到它不期望的标识符时,该错误消息很常见,因此首先猜测IsDerivedFromBase当时<{1}} <编译器你没有包含适当的标题?)。或者,如果IsDerivedFromBase不是模板,编译器也会期望,(或;)。

答案 1 :(得分:0)

解决了我的问题,有趣的是愚蠢。所以由于评论(来自Jesse Good)我快速浏览了我的包含。由于这是一个很小的“快速鞭打”的项目,我并没有真正关注它们。我不知道错误发生的确切位置,但我发现 AssetManager 不知道 IsDerivedFromBase 所以我设置了以下代码块来解决这个问题无需写一堆 到处都是 #include 语句!

// Contact point houses all of the include files
// for this project to keep everything in one place.

#ifndef CONTACTPOINT_H
#define CONTACTPOINT_H

#include "Asset.h"
#include "Sprite.h" 
#include "IsDerivedFromBase.h"
#include "GenericManager.h"
#include "AssetManager.h"   

#endif

现在我只是将它包含在每个标题中,一切都很好。我已经用了一年多的时间编写C ++并且从未遇到过这个问题,这对于任何新的编程来说都是一个很好的教训。

感谢大家的帮助:)。

相关问题