疯狂的连接器疯狂

时间:2017-05-01 08:00:28

标签: templates visual-c++ compiler-errors

简介

我目前正在处理用VC ++编写的dependency injection example。我当前的设置基于writer(我的基类),一些测试类(我只是上传其中一个 - > writer2)和我的圣杯,即通用类({ {1}}),它应该完成整个依赖注入魔术。

但是每次我面对一些cpp代码时,链接器都不会在我的卡片中播放并跳过一些...很好......看...错误消息:

链接错误消息

genericWriter

我的代码:

PlusPlus_Example.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall genericWriter<class writer2>::print_whatever(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)const " (?print_whatever@?$genericWriter@Vwriter2@@@@UBEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)

writer.h

// writer.h #ifndef WRITER_H #define WRITER_H #include <string> #include <boost/serialization/assume_abstract.hpp> #include <boost/serialization/export.hpp> #include <boost/serialization/nvp.hpp> class writer { public: writer::writer(void){} virtual ~writer() {}; virtual void print_whatever(const std::string s) const = 0; private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { } }; BOOST_SERIALIZATION_ASSUME_ABSTRACT(writer) #endif

writer2.h

// writer.h #ifndef WRITER2_H #define WRITER2_H #include "writer.h" #include <boost/serialization/export.hpp> class writer2 : public writer { public: inline writer2(){} void print_whatever(const std::string s) const override; private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, unsigned int file_version) { ar.template register_type<writer2>(); ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(writer); } }; #endif

writer2.cpp

//writer2.cpp #include "stdafx.h" #include "writer2.h" #include <iostream> void writer2::print_whatever(const std::string s) const { std::cout << "-=" + s + "=-"; } BOOST_CLASS_EXPORT_GUID(writer2, "writer2")

genericWriter.h

// genericWriter.h #ifndef GENERICWRITER_H #define GENERICWRITER_H #include "writer.h" #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> #include <boost/serialization/export.hpp> #include <boost/serialization/shared_ptr.hpp> template <typename T> class genericWriter : public writer { public: genericWriter(): _methodOfWriting(new T) { // Yes, the double parentheses are needed, otherwise the comma will be seen as macro argument separator BOOST_STATIC_ASSERT((boost::is_base_of<writer, T>::value)); } void print_whatever(const std::string s) const override; private: writer* _methodOfWriting; friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, unsigned int file_version) { ar.template register_type<genericWriter<T>>(); ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(writer); ar & BOOST_SERIALIZATION_NVP(_methodOfWriting); } };

genericWriter.cpp

//genericWriter.cpp #include "stdafx.h" #include "genericWriter.h" template<typename T> void genericWriter<T>::print_whatever(const std::string s) const { (*_methodOfWriting).print_whatever(s); } // Exporting a template class not possible? //BOOST_CLASS_EXPORT_GUID(genericWriter<T>, "genericWriter")

CPlusPlus_Example.cpp

我期望它做什么

我的代码应该将// CPlusPlus_Example.cpp : Defines the entry point for the console application. #include "stdafx.h" #include "conio.h" #include <string> #include <boost/serialization/assume_abstract.hpp> #include "genericWriter.h" #include "writer2.h" #include <fstream> #include <boost/archive/xml_iarchive.hpp> #include <boost/archive/xml_oarchive.hpp> using namespace std; int main() { // create and open a character archive for output std::ofstream ofs("CPlusPlus_example.xml"); writer* test = new genericWriter<writer2>(); test->print_whatever("test"); // save data to archive { boost::archive::xml_oarchive oa(ofs); // write class instance to archive oa << BOOST_SERIALIZATION_NVP(test); // archive and stream closed when destructors are called } { // open the archive std::ifstream ifs("CPlusPlus_example.xml"); assert(ifs.good()); boost::archive::xml_iarchive ia(ifs); // restore the schedule from the archive ia >> BOOST_SERIALIZATION_NVP(test); } test->print_whatever("test"); getch(); return 0; } 保存到XML文件中或从XML文件中加载test(在此示例中称为&#34; CPlusPlus_example.xml &#34;)应该看起来像以下内容:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE boost_serialization>
<boost_serialization signature="serialization::archive" version="15">
<test type="genericWriter" class_id="0" tracking_level="1" version="0" object_id="_0">
    <_methodOfWriting type="writer2" class_id="1" tracking_level="0" version="0">
        <writer></writer>
    </_methodOfWriting>
    <writer></writer>
</test>
</boost_serialization>

我的嫌疑人

因为我无法提升::导出我的genericWriter,它可能无法解决?但如果是这样的话 - 有没有办法使用&#34;通用模板类&#34;?

编辑1

根据评论,我将代码更改为以下内容:

genericWriter.tpp

//genericWriter.tpp

#include "stdafx.h"
#include "genericWriter.h"

template<typename T>
void genericWriter<T>::print_whatever(const std::string s) const
{
    (*_methodOfWriting).print_whatever(s);
}

genericWriter.h

// genericWriter.h
#ifndef GENERICWRITER_H
#define GENERICWRITER_H

#include "writer.h"

#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/export.hpp>
#include <boost/serialization/shared_ptr.hpp>

#include "genericWriter.tpp"

template <typename T>
class genericWriter : public writer
{
public:

    genericWriter(): _methodOfWriting(new T)
    {
        // Yes, the double parentheses are needed, otherwise the comma will be seen as macro argument separator
        BOOST_STATIC_ASSERT((boost::is_base_of<writer, T>::value));
    }

    //void print_whatever(const std::string s) const override;

private:
    writer* _methodOfWriting;

    friend class boost::serialization::access;

    template<class Archive>
    void serialize(Archive & ar, unsigned int file_version)
    {
        ar.template register_type<genericWriter<T>>();
        ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(writer);
        ar & BOOST_SERIALIZATION_NVP(_methodOfWriting);
    }
};

(并删除了genericWriter.cpp

现在我得到更多错误\(^ - ^)/

1>e:\vsprograms\dependencyinjectionexamples\cplusplus_example\cplusplus_example\genericwriter.tpp(7): error C2988: unrecognizable template declaration/definition
1>e:\vsprograms\dependencyinjectionexamples\cplusplus_example\cplusplus_example\genericwriter.tpp(7): error C2143: syntax error: missing ';' before '<'
1>e:\vsprograms\dependencyinjectionexamples\cplusplus_example\cplusplus_example\genericwriter.tpp(7): error C2182: 'genericWriter': illegal use of type 'void'
1>e:\vsprograms\dependencyinjectionexamples\cplusplus_example\cplusplus_example\genericwriter.tpp(7): error C2059: syntax error: '<'
1>e:\vsprograms\dependencyinjectionexamples\cplusplus_example\cplusplus_example\genericwriter.tpp(8): error C2143: syntax error: missing ';' before '{'
1>e:\vsprograms\dependencyinjectionexamples\cplusplus_example\cplusplus_example\genericwriter.tpp(8): error C2447: '{': missing function header (old-style formal list?)
1>e:\vsprograms\dependencyinjectionexamples\cplusplus_example\cplusplus_example\writer2.h(12): error C2628: 'writer2' followed by 'void' is illegal (did you forget a ';'?)
1>e:\vsprograms\dependencyinjectionexamples\cplusplus_example\cplusplus_example\writer2.h(12): error C2270: 'print_whatever': modifiers not allowed on nonmember functions
1>e:\vsprograms\dependencyinjectionexamples\cplusplus_example\cplusplus_example\writer2.h(12): error C2259: 'writer2': cannot instantiate abstract class
1>e:\vsprograms\dependencyinjectionexamples\cplusplus_example\cplusplus_example\writer2.h(12): note: due to following members:
1>e:\vsprograms\dependencyinjectionexamples\cplusplus_example\cplusplus_example\writer2.h(12): note: 'void writer::print_whatever(const std::string) const': is abstract
1>e:\vsprograms\dependencyinjectionexamples\cplusplus_example\cplusplus_example\writer.h(17): note: see declaration of 'writer::print_whatever'
1>e:\vsprograms\dependencyinjectionexamples\cplusplus_example\cplusplus_example\writer2.h(15): error C2059: syntax error: 'private'
1>e:\vsprograms\dependencyinjectionexamples\cplusplus_example\cplusplus_example\writer2.h(16): error C2255: 'friend': not allowed outside of a class definition
1>e:\vsprograms\dependencyinjectionexamples\cplusplus_example\cplusplus_example\writer2.h(27): error C2059: syntax error: '}'
1>e:\vsprograms\dependencyinjectionexamples\cplusplus_example\cplusplus_example\writer2.h(27): error C2143: syntax error: missing ';' before '}'
1>e:\vsprograms\dependencyinjectionexamples\cplusplus_example\cplusplus_example\cplusplus_example.cpp(22): error C2061: syntax error: identifier 'genericWriter'

那可能是因为genericWriter的print_whatever函数的定义不再在genericWriter.h中了?

0 个答案:

没有答案