c++ - 包含内部模板类的 C++ 模板类构造函数的正确语法

标签 c++ templates constructor function-pointers

我有一个类模板,它又包含一个模板类。该内部模板类又具有采用函数指针的构造函数。我以前使用这个内部类来加载 XML 文件,现在我正在尝试使用这个类创建 XML 转换器。问题在于为内部类提供一组足够的构造函数参数。

编辑 我已经采纳了一些关于临时常量字符串等的评论,并花时间创建了一个仍然存在我的问题的简化案例。我对问题进行了大量修改。使用下面发布的代码,现在返回的错误是

FormatConverter/main.cpp:7:   instantiated from here
FormatConverter/FormatConverter.h:52: error: no matching function for call to 'XMLLoader<MessageType1>::XMLLoader(<unknown type>, const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'
FormatConverter/XMLLoader.h:40: note: candidates are: XMLLoader<XMLTYPE>::XMLLoader(std::auto_ptr<XMLTYPE> (*)(std::istream&, std::string&, std::string&), const std::string&, const std::string&) [with XMLTYPE = MessageType1]
FormatConverter/XMLLoader.h:10: note: XMLLoader<MessageType1>::XMLLoader(const XMLLoader<MessageType1>&)

// main.cpp

#include <iostream>
#include "FormatConverter.h"
#include "MessageTypes.h"

int main (int argc, char * const argv[]) {
std::string options = "";
FormatConverter<MessageType1, MessageType2> fConverter( options );
    return 0;
}

//消息类型.h

#ifndef _MTYPES_
#define _MTYPES_

class MessageType1 
{  };

class MessageType2
{  };

#endif

//格式转换器.h

#ifndef _FORMAT_CONVERTER_
#define _FORMAT_CONVERTER_

#include "XMLLoader.h"
#include <memory>
#include <string>

template<typename T>
::std::auto_ptr<T>
dummyLoader( ::std::istream& dummyStream, std::string dummyFlags, std::string dummyProps )
{
return ::std::auto_ptr<T>( new(T) );
}

template<>
::std::auto_ptr<MessageType1>
dummyLoader( ::std::istream& dummyStream, std::string dummyFlags, std::string dummyProps )
{
return ::std::auto_ptr<MessageType1>( new(MessageType1) );
}

template<>
::std::auto_ptr<MessageType2>
dummyLoader( ::std::istream& dummyStream, std::string dummyFlags, std::string dummyProps )
{
return ::std::auto_ptr<MessageType2>( new(MessageType2) );
}

template <typename IN, typename OUT>
class FormatConverter {
public:
FormatConverter( std::string& options );
virtual ~FormatConverter( void );

void convert( std::auto_ptr<IN> input, std::string& stuff );

private:    
std::string options_;
const std::string dummyFlags;
const std::string dummyProps;

XMLLoader<IN> loader_;
};

template <typename IN, typename OUT>
FormatConverter<IN, OUT>::FormatConverter( std::string& options )
: options_(options)
, dummyFlags("")
, dummyProps("")
, loader_ ( dummyLoader<IN>,
        dummyFlags,
        dummyProps )
{ }

template <typename IN, typename OUT>
FormatConverter<IN,OUT>::~FormatConverter( void )
{ }

template<typename IN, typename OUT>
void
FormatConverter<IN, OUT>::convert( std::auto_ptr<IN> input, std::string& stuff )
{ }
#endif

//XMLLoader.h

#ifndef _XMLLoader_H
#define _XMLLoader_H

#include <memory>
#include <string>

template <typename XMLTYPE>
class XMLLoader
    {
    public:
        typedef ::std::auto_ptr<XMLTYPE> MSG_LOADER( std::istream&,
                                                 std::string&,   //flags
                                                         std::string& ); //properties

        XMLLoader(  MSG_LOADER *loader,
                        const std::string& schemaSource = "NONE",
                        const std::string& ns="" );

        virtual ~XMLLoader(void);
        virtual std::auto_ptr<XMLTYPE> load(std::istream &is);

    private:
        MSG_LOADER* loader_;
        std::string schemaSource_;
        std::string namespace_;
    };

#include <unistd.h>
#include <stdexcept>
#include "XMLLoader.h"

template<typename XMLTYPE>
XMLLoader<XMLTYPE>::XMLLoader(MSG_LOADER* loader,
                              const std::string& schemaSource,
                              const std::string& ns)
: loader_(loader)
, schemaSource_(schemaSource)
, namespace_(ns)
{ /* stuff */ }

template<typename XMLTYPE>
XMLLoader<XMLTYPE>::~XMLLoader( void )
{ }

template<typename XMLTYPE>
::std::auto_ptr<XMLTYPE>
XMLLoader<XMLTYPE>::load( std::istream& is )
{
    std::string flags = "";
    std::string props = "";
    return loader_(is, flags, props );
}

#include "MessageTypes.h"

template
XMLLoader<MessageType1>::XMLLoader(
                                   MSG_LOADER*,
                                   const std::string&,
                                   const std::string& );

template
XMLLoader<MessageType1>::~XMLLoader( void );

template
XMLLoader<MessageType2>::XMLLoader(
                                   MSG_LOADER*,
                                   const std::string&,
                                   const std::string& );

template
XMLLoader<MessageType2>::~XMLLoader( void );
#endif

XMLLoader 构造函数有 3 个参数:两个字符串和一个函数指针。它返回一个 auto_ptr 类型。我的 dummyLoader 模板似乎满足了这个要求,我尝试了一些语法上的细微变化,但错误是指 .

因此,编译器似乎无法识别我在实例化 FormatConverter 对象时尝试传递的 dummyLoader 类型。谁能解释一下为什么?

最佳答案

从字符串中删除 const:

loader_(dummyLoader, std::string("namespace"), std::string("schema_loc"))

事实上,std::string 有一个完美可用的转换构造函数:

loader_(dummyLoader, "namespace", "schema_loc")

像这样创建的临时值非常乐意绑定(bind)到常量引用。

关于c++ - 包含内部模板类的 C++ 模板类构造函数的正确语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18905590/

相关文章:

c++ - C++中带初始化参数的构造函数

C++ MPI,使用多个节点,首先在节点级别减少,然后减少到头节点

c++ - Visual Studio 11 斜体参数

c++ - 如何抛出好的异常?

java - 获取handlebar java中迭代器列表的长度

PHP 在构造函数中访问属性

c++ - 子集 vector 并对其进行排序

templates - 如何处理 Web 应用程序中的模块主题?

c++ - 转换运算符到 std::complex<double>

oop - 确定构造函数、初始化和重置方法的任务的最佳实践是什么