Thomas Kowalski Thomas Kowalski - 11 months ago 47
C++ Question

Child class calling superclass constructor

I'm pretty new to C++ and I'm trying to establish a class inheriting from another one. In this case, I have a superclass DataScraper that is supposed to have two children, JSONDataScraper and XMLDataScraper. Well... it doesn't work. When I compile, I get the following message :


'identifier' : constructor initializer lists are only allowed on constructor definitions (C2250)


datascraper.h

#include <string>
#include <vector>
#include <Qstring>

class DataScraper
{
private:
std::vector<QString> GetFiles(const QString folder);

protected:
std::vector<QString> filesToAnalyze;

public:
DataScraper(const QString folderPath);
DataScraper(const std::vector<QString>& paths);

};


datascraper.cpp

#include "datascraper.h"
#include "Windows.h"

DataScraper::DataScraper(const std::vector<QString>& paths)
{
//...
}

DataScraper::DataScraper(const QString folderPath)
{
//...
}

std::vector<QString> DataScraper::GetFiles(const QString folder)
{
//...
}


And now, JSONDataScraper files.

#include "rapidjson.h"
#include "datascraper.h"
#include "streamreader.h"
#include <QString>

class JSONDataScraper : public DataScraper
{
private:
StreamReader *FileContentsReader;

public:
JSONDataScraper(QString folderPath);
JSONDataScraper(std::vector<QString> files);
};


jsondatascraper.cpp

#include "jsondatascraper.h"
#include "streamreader.h"
#include "datascraper.h"
#include <QString>

JSONDataScraper::JSONDataScraper(const std::vector<QString>& paths) : DataScraper(paths)
{
//...
}

JSONDataScraper::JSONDataScraper(const QString& pathToFile) : DataScraper(pathToFile)
{
//...
}


What can I do ?

Answer Source

The constructors for JSONDataScraper have differing parameter lists in the declaration and definition respectively. You should have another error message about this.

Your definition is thus not recognised as a declared constructor, so the ctor-initialiser is not valid there.

JSONDataScraper(std::vector<QString> files);
JSONDataScraper::JSONDataScraper(const std::vector<QString>& paths) : DataScraper(paths)
//                               ^^^^^^                    ^
{
   //...
}

JSONDataScraper(QString folderPath);
JSONDataScraper::JSONDataScraper(const QString& pathToFile) : DataScraper(pathToFile)
//                               ^^^^^^       ^
{
    //...
}

You're basically confusing the heck out of your compiler.

Fix the parameter lists to match and everything should be fine.