dimanche 12 janvier 2020

C++ abstract class cannot be instantiated

I have this abstract class

 class ImageIO {
                protected:
                    ImageIO() {}
                public:
                    virtual bool load(const std::string & filename, const std::string & format) = 0
                    virtual bool save(const std::string & filename, const std::string & format) = 0;
             }

And the implementation

class Image : public Array2D<Color>, public ImageIO {

private:

static int case_insensitive(string s) {

    transform(s.begin(), s.end(), s.begin(), tolower);

    if (s.compare("ppm") == 0) {
        return 1;
    }
    return 0;
}

public:


bool load(const std::string& filename, const std::string& format) {

    if (case_insensitive(format)) {
        float* data = ReadPPM(filename.c_str(), (int*)width, (int*)height);

        for (int i = 0, j = 0; i < width * height; i++, j += 3) {
            buffer[i] = Color(data[j], data[j + 1], data[j + 2]);
        }

        delete[] data;
        return true;
    }

    return false;
}

bool save(const std::string& filename, const std::string& format) {

    if (case_insensitive(format)) {

        float* data = new float[width * height * 3];
        for (int i = 0, j = 0; i < width * height; i++, j += 3) {
            data[j] = buffer[i].x;
            data[j + 1] = buffer[i].y;
            data[j + 2] = buffer[i].z;
        }

        bool write_check = WritePPM(data, width, height, filename.c_str());
        delete[] data;
        return write_check;
    }

    return false;
}

};

And I keep getting an abstract class cannot be instantiated error if I try this

Image test = Image();

in my main. I've also tried declaring t=those two methods in a header file

class Image : public Array2D<Color>, public ImageIO {

private:
    static int case_insensitive(std::string);
public:
    bool ImageIO::load(const std::string& filename, const std::string& format);
    bool ImageIO::save(const std::string& filename, const std::string& format);
};

but then I get unresolved external symbol errors. Is there a problem in the way I'm implementing the two functions or in how my declaring the class?

Aucun commentaire:

Enregistrer un commentaire