-1

I have a problem with my code.

I have three classes, and one of them is a pure abstract class. I don't know why I receive the error:

'note' cannot instantiate abstact class.

It may be because of STL usage, or I have made a mistake and I dont see it.

The problem is I tried without STL and it works, and I don't know what is the problem here because I think it it correct.

#pragma once
class note
{
protected:
    int ziua;
    int ora;
public:
    note();
    note(int day,int hour);
    virtual void print()=0;
    virtual ~note();
};

#include "note.h"

note::note()
{
}

note::note(int day, int hour) :ziua(day), ora(hour)
{
} 

note::~note()
{
}

#pragma once
#include "note.h"
#include <iostream>

class apeluri:public note
{
    char *numar_telefon;
public:
    apeluri();
    apeluri(int day, int h, char*phone);
    void print()
    {
        printf("%d %d %s", ziua, ora, numar_telefon);
    }
    ~apeluri();
};

#pragma once
#include <iostream>
#include "apeluri.h"
#include <vector>
#include "note.h"

using namespace std;

class remainder
{
    vector<note> t;
public:
    remainder();
    void addsedinta(int zi, int ora, int durata, char*subi);
    void addapel(int zi, int ora, char*phon)
    {
        apeluri *f;
        f = new apeluri(zi, ora, phon);
        t.push_back(*f);
    }
    void show()
    {
    }
    ~remainder();
};
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Alex
  • 11
  • 1
  • 3
  • 1
    Please also can you share the exact text of the error message and indicate which line it occurs on. – Ken Y-N Aug 23 '17 at 01:18
  • error C2259: 'note' : cannot instantiate abstract class c:\program files (x86)\microsoft visual studio 12.0\vc\include\xmemory0 593 1 examen_remainder – Alex Aug 23 '17 at 01:27
  • 1
    My previous answer was wrong, so I deleted it; you cannot have a vector of abstract classes, as I indicate with the dup, and even if the class was not abstract, you cannot normally assign a subclass to a superclass, only pointers can be assigned. – Ken Y-N Aug 23 '17 at 01:29
  • Can you please fix your keyboard. Your space bar and the tab key appear to be quite broken, resulting in randomly-indented code that's pretty much unreadable, and looks like mush. You do, of course, want to make your code as easy as possible for everyone else to read, so they can figure out what's wrong with it and help you with your question, right? – Sam Varshavchik Aug 23 '17 at 02:02
  • Did you mean `vector t;`? – CinCout Aug 23 '17 at 05:18

1 Answers1

1

In your remainder class, using vector<note> is illegal. note is abstract, so the vector can't create note objects.

Even if note were not abstract, your code would still not work correctly, because it would be affected by object slicing.

To store derived objects in a container of base classes, you must use pointers instead, ie vector<note*>:

#pragma once
#include <iostream>
#include <vector>
#include "note.h"
#include "apeluri.h"

using namespace std;

class remainder
{
private:
    vector<note*> t;

    remainder(const remainder &) {}
    remainder& operator=(const remainder &) { return *this; }

public:
    remainder();

    ~remainder()
    {
        for(std::vector<note*>::iterator i = t.begin(); i != t.end(); ++i) {
            delete *i;
        }
    }

    void addsedinta(int zi, int ora, int durata, char*subi);

    void addapel(int zi, int ora, char*phon)
    {
        apeluri *f = new apeluri(zi, ora, phon);
        t.push_back(f);
    }

    void show()
    {
    }
};

If you are using C++11 or later, this would be better written as this instead:

#pragma once
#include <iostream>
#include <vector>
#include <memory>
#include "note.h"
#include "apeluri.h"

using namespace std;

class remainder
{
private:
    vector<unique_ptr<note>> t;

public:
    remainder();
    remainder(const remainder &) = delete;
    remainder& operator=(const remainder &) = delete;

    void addsedinta(int zi, int ora, int durata, char*subi);

    void addapel(int zi, int ora, char*phon)
    {
        t.push_back(std::unique_ptr<apeluri>(new apeluri(zi, ora, phon)));
        // in C++14 and later, use this instead:
        // t.push_back(std::make_unique<apeluri>(zi, ora, phon));
    }

    void show()
    {
    }
};
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770