lundi 11 septembre 2017

Erros while trying to make factroy method with c++

I am trying to use this example code: http://ift.tt/2gYFK0j in order to make it work for me for a program I try to improve. Here is my code according the files: VCFReader.h

#pragma once

#include <fstream> // files
#include <locale> // unicode chars and strings
#include <cstdlib> // for exit()
#include <vector> // vector of VCFRecords
#include "VCFRecord.h"
#include "VCFReader3_0.h"
#include "VCFReader2_1.h"

class VCFReader 
{
public:
    static VCFReader *makeVCFReader(std::wstring version); // factory method
    virtual vector<VCFRecord> loadVCFRecordsFromFile(std::wstring pathToFile) = 0;
};

VCFReader.cpp:

#include "VCFReader.h"

VCFReader *VCFReader::makeVCFReader(std::wstring version)
{
    if (version == L"VERSION:3.0")
    {
        return new VCFReader3_0;
    }
    else if (version == L"VERSION:2.1")
    {
        return new VCFReader2_1;
    }
    else
    {
        std::string throwMessage = "Unsupported version: " + std::string(version.begin(), version.end());
        throw throwMessage.c_str();
    }
}

VCFReader3_0.h (the same as VCFReader2_1.h):

#pragma once

#include "VCFReader.h"

class VCFReader3_0 : public VCFReader
{
    virtual vector<VCFRecord> loadVCFRecordsFromFile(std::wstring pathToFile);
};

But I get these errors:

Error C2440 'return': cannot convert from 'VCFReader2_1 *' to 'VCFReader *' vcfreader.cpp 11

Error C2440 'return': cannot convert from 'VCFReader3_0 *' to 'VCFReader *' vcfreader.cpp 7

and multiple times

Error C2504 'VCFReader': base class undefined vcfreader2_1.h 5

Error C2504 'VCFReader': base class undefined vcfreader3_0.h 5

As I did a little search I found that the error c2504 is caused by circular definitions. But I cannot think of a way to not include the base class in the derived ones as they derive from it and at the same time if I don't include the derived classes in the base class then I cannot return new derived classes in the static function. Also - why I get this error - C2440 and how to clear it?

Thanks in advance for your help!

Aucun commentaire:

Enregistrer un commentaire