English 中文(简体)
如何具体规定,C++级的具体方法使用模板?
原标题:How does one specify that a specific method of a C++ class uses templates?

I m带有C++模板的问题。 这里是我试图做的事的解释,以便每个人都能更好地了解我的问题。

我的框架有一个基类,即<代码>Component ,我的框架用户将产生Component,以创建具体<代码>Components,例如TransformComentAudioComponent。 AComponentComposite 页: 1 Components that a givenuser has produced.

我试图通过<代码>boost:any和boost:any_cast:

下面是<代码>ComponentComposite和我的boost:any/code>清单中的两种方法。

    class ComponentComposite {

        public:
            ComponentComposite();
            template<class T> bool addComponent(T* component);
            template<class T> T* getComponent();

        private:
            QList<boost::any*>* m_components;
    }

这是<代码>GameObject,即<代码>ComponentComposite。 我正试图在<条码>上添加两个<代码>Component,然后试图进入两个<代码>。 添加的构成部分。 这样做是<代码>ComponentComposite的共同使用案例。

    GameObject::GameObject() : ComponentComposite()
    {
        addComponent<Components::AudioComponent>(new Components::AudioComponent());
        addComponent<Components::TransformComponent>(new Components::TransformComponent());
        Components::TransformComponent* transform= getComponent<Components::TransformComponent>();
        Components::AudioComponent* audio= getComponent<Components::AudioComponent>();
    }

这样做可能会造成4个错误(每个职能要求一个):

  1. ...undefined reference to `bool BalaurEngine::Composites::ComponentComposite::addComponent<BalaurEngine::Components::AudioComponent>(BalaurEngine::Components::AudioComponent*)
  2. ...undefined reference to `bool BalaurEngine::Composites::ComponentComposite::addComponent<BalaurEngine::Components::TransformComponent>(BalaurEngine::Components::TransformComponent*)
  3. ...undefined reference to `BalaurEngine::Components::TransformComponent* BalaurEngine::Composites::ComponentComposite::getComponent<BalaurEngine::Components::TransformComponent>()
  4. ...undefined reference to `BalaurEngine::Components::AudioComponent* BalaurEngine::Composites::ComponentComposite::getComponent<BalaurEngine::Components::AudioComponent>()

如果有人愿意的话,我可以张贴我方法的源代码template<class T>bool AddComponent(T*部分);template<class T> T* 获得答复;

问题回答

这一点现已得到答复——见最后评论:

@Mutmansky,我不知道在C++中实施的模板功能必须与模板功能的定义一起放在标题上。 在我这样做之后,根据Scott Langham的建议,编辑错误消失。 如果你想看到档案的源代码,我很乐意把它放在这里/交给你。 让我知道!





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签