简体   繁体   English

仅C ++模板类的实现

[英]C++ only implementation of template class

I do not have access to c++11 for this project. 我无权为此项目使用c ++ 11。

I have a C++ class; 我有一个C ++课; lets call it Bar, it looks something like this: 让我们称之为Bar,它看起来像这样:

Header FooBar.h: 标头FooBar.h:

#include "Fancy.h" // <--- Dependency
namespace Foo
{
    template<typename T>
    class Bar : public Fancy::FancyClass<T, ALLOCATER<HARD_CODED_VALUE>>
    {
    };
}

Cpp: CPP:

None 没有

Fancy is a lib that is being statically linked into a dll. 花式是一个静态链接到dll的库。 The problem is that when other projects want to use the dll they include "FooBar.h" it says that it cannot find "Fancy.h" 问题是,当其他项目要使用dll时,它们包含“ FooBar.h”,它表示找不到“ Fancy.h”。

Basically I want projects that include the dll to be able to use this class without needing any additional libraries or headers. 基本上,我希望包含dll的项目能够使用此类而不需要任何其他库或标头。

My solution was to just put it in a cpp and have no header file. 我的解决方案是将其放在cpp中,并且没有头文件。 Unfortintly nothing can find it then? 不幸的是什么也找不到呢? I have tried creating a blank header file that did not work either. 我尝试创建一个空白的头文件,该文件也不起作用。

I tried to forward declare it in the header but to do that I need the .h to understand what FancyClass is. 我试图将其在标头中进行声明,但为此,我需要.h来了解FancyClass是什么。 I have also tried 我也尝试过

namespace Fancy
{
    class FancyClass;
    class ALLOCATER;
}

namespace Foo
{
    template<typename T>
    class Bar : public Fancy::FancyClass<T, ALLOCATER<HARD_CODED_VALUE>>;
}

in the header. 在标题中。 But that causes many errors. 但这会导致许多错误。

Short answer, you must include the header in your executable file in order to use the provided library. 简短的答案,您必须在可执行文件中包含标头才能使用提供的库。

Long(er) answer, the compiler needs to know the structure of the class defined in the library. 较长的答案,编译器需要知道库中定义的类的结构。

Imagine that there is a class called MyAwesomeClass that is part of a library you want to use in your executable. 想象一下,有一个名为MyAwesomeClass的类,它是您要在可执行文件中使用的库的一部分。 The compiler doesn't know anything about that class when you say MyAwesomeClass awesome; 当您说MyAwesomeClass awesome;时,编译器对该类MyAwesomeClass awesome; . How big is it? 它有多大? What methods can you call on it? 您可以调用什么方法? Does it inherit from anything? 它从任何东西继承吗?

This information is contained in the header file where the class is declared. 此信息包含在声明该类的头文件中。

class MyAwesomeClass
{
    private:
        std::string _name;
    public:
        MyAwesomeClass();
        MyAwesomeClass(const std::string& name);
        void print_name();
};

The implementation is defined in the DLL or static library, but when using the type MyAwesomeClass , the compiler needs to know at least its structure. 该实现是在DLL或静态库中定义的,但是使用MyAwesomeClass类型MyAwesomeClass ,编译器至少需要知道其结构。 Without including the header, the compiler will complain that it doesn't know what type MyAwesomeClass is (ie. undefined class errors). 如果不包含标头,编译器将抱怨它不知道MyAwesomeClass是什么类型(即, undefined class错误)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM