所以我得到以下编译错误:
CMakeFiles/moje.dir/src/main/cpp/main.cpp.o:(.rodata._ZTV10ReadWriterINSt7__cxx1119basic_ostringstreamIcSt11char_traitsIcESaIcEEENS0_19basic_istringstreamIcS3_S4_EEE[_ZTV10ReadWriterINSt7__cxx1119basic_ostringstreamIcSt11char_traitsIcESaIcEEENS0_19basic_istringstreamIcS3_S4_EEE]+0x48): undefined reference to `Writer<std::__cxx11::basic_istringstream<char, std::char_traits<char>, std::allocator<char> > >::write(std::__cxx11::basic_istringstream<char, std::char_traits<char>, std::allocator<char> >&)'
CMakeFiles/moje.dir/src/main/cpp/main.cpp.o:(.rodata._ZTV6WriterINSt7__cxx1119basic_istringstreamIcSt11char_traitsIcESaIcEEEE[_ZTV6WriterINSt7__cxx1119basic_istringstreamIcSt11char_traitsIcESaIcEEEE]+0x20): undefined reference to `Writer<std::__cxx11::basic_istringstream<char, std::char_traits<char>, std::allocator<char> > >::write(std::__cxx11::basic_istringstream<char, std::char_traits<char>, std::allocator<char> >&)'
实施如下:
Writer.h
#ifndef WRITER_H
#define WRITER_H
template<typename T>
class Writer {
public:
virtual ~Writer() {}
virtual void write(T &in);
};
#endif /* WRITER_H */
Reader.h
#ifndef READER_H
#define READER_H
template<typename T>
class Reader {
public:
virtual ~Reader() {}
virtual T read();
};
#endif /* READER_H */
ReadWriter.h
#ifndef READWRITER_H
#define READWRITER_H
#include "Reader.h"
#include "Writer.h"
template<typename R, typename W>
class ReadWriter : public Reader<R>, public Writer<W>{
public:
virtual ~ReadWriter() {}
};
#endif /* READWRITER_H */
ConfigReadWriter.h
#ifndef CONFIGREADWRITER_H
#define CONFIGREADWRITER_H
#include "header/ReadWriter.h"
class ConfigReadWriter : public ReadWriter<std::ostringstream, std::istringstream> {
public:
ConfigReadWriter(Config &config) : config_(config) {}
~ConfigReadWriter() {}
std::ostringstream read() override {
std::ostringstream ss("ConfigReadWriter::read");
return ss;
};
void write(std::istringstream& in) override {
std::cout << "ConfigReadWriter::write" << std::endl;
}
private:
Config& config_;
};
#endif /* CONFIGREADWRITER_H */
Main.cpp:
#include "header/config/Config.h"
#include "header/config/ConfigReadWriter.h"
int main() {
std::string a = "1";
Config config(1, a);
ConfigReadWriter rw(config);
return 0;
}
我理解通过保持头部中的实现我不应该发生未定义引用的错误。更正了write方法中的实现以适应接口。还是有编译错误。阅读链接。
请注意,您的实现提供了带有副本的void write(std::istringstream)
方法,而未定义的引用用于获取您未在基类void write(std::istringstream &)
中实现的virtual void write(T &in);
等引用的方法。因此,您应该使用正确的签名更新实现:
virtual void write(T &in) = 0; // no implementation
...
void write(std::istringstream &) override {
std::cout << "ConfigReadWriter::write" << std::endl;
}
override
说明符确保方法签名与基类中的虚方法签名匹配。