0

I am trying to implement inheritance in a program but the compiler gives me a series of errors. "Pubblicazioni" is the base class and the class "Libri" inherits the constructor and the function "stampa()" which, precisely, prints the information. On the function "stampa()" we perform an override, I have the impression that the error is there. Thank you very much in advance.

Pubblicazioni.h this is the base class

#ifndef PUBBLICAZIONI_H
#define PUBBLICAZIONI_H


#include <iostream>
#include <string>
#include <vector>
#include <stdexcept>
#include <sstream>
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::vector;
using std::invalid_argument;
using std::ostringstream;


class pubblicazioni{
    public:
        Pubblicazioni(const string&, const string&, int);   //Nome, autore/i, anno pubblicazione
        ~Pubblicazioni()=default;
        //funzioni set
        void setNome(const string&);
        void setAutori(const string&);
        void setAnno(int);
        //Funzioni get
        string getNome() const;
        string getAutori() const;
        int getAnno() const;
        //Funzione stampa
        virtual string stampa() const;
    protected:
        string n, aut;
        int a;
};
#endif

pubblicazioni.cpp, implementation of the base class

#include "pubblicazioni.h"

Pubblicazioni::Pubblicazioni(const string& nome, const string& autori, int anno){
    setNome(nome);
    setAutori(autori);
    setAnno(anno);
}


void Pubblicazioni::setNome(const string& nome){
    n=nome;
}

void Pubblicazioni::setAutori(const string& autori){
    aut=autori;
}

void Pubblicazioni::setAnno(int anno){
    if(anno>=1650)
        a=anno;
    else
        throw invalid_argument("L'anno deve essere maggiore o uguale a 1650.");
}

string Pubblicazioni::getNome() const{
    return n;
}

string Pubblicazioni::getAutori() const{
    return aut;
}

int Pubblicazioni::getAnno() const{
    return a;
}

string Pubblicazioni::stampa() const{
    ostringstream s;
    s<<"Nome opera: "<<getNome()<<"\nAutori: "<<getAutori()<<"\nAnno di pubblicazione: "<<getAnno();
    return s.str();
}

libri.h this is the derived class

#ifndef LIBRI_H
#define LIBRI_H
#include "pubblicazioni.h"


class Libri : public Pubblicazioni{
    public:
        Libri(const string&, const string&, int, const string&, const string&);
        ~Libri()=default;
        //Funzioni set
        void setCasaEditrice(const string&);
        void setISBN(const string&);
        //Funzioni get
        string getCasaEditrice() const;
        string getISBN() const;
        //Override
        virtual string stampa() const override;
    protected:
        string cEdit, isbn;
};

#endif

libri.cpp implementation of the derived class

#include "libri.h"

Libri::Libri(const string& nomi, const string& autori, int anno, const string& CasaEditrice, const string& codice) : Pubblicazioni(nomi, autori, anno){
    setCasaEditrice(CasaEditrice);
    setISBN(codice);
}



void Libri::setCasaEditrice(const string& CasaEditrice){
    cEdit=CasaEditrice;
}

void Libri::setISBN(const string& codice){
    isbn=codice;
}

string Libri::getCasaEditrice() const{
    return cEdit;
}

string Libri::getISBN() const{
    return isbn;
}

string Libri::stampa() const{
    ostringstream s;
    s<<Pubblicazioni::stampa()<<"\nCasa editrice: "<<getCasaEditrice()<<"\nISBN: "<<getISBN()<<endl;
    return s.str();
}

makefile

CC=g++
CFLAGS=-std=c++11
DEPS=pubblicazioni.h 
OBJ=pubblicazioni.o main.o

%.o: %.cpp $(DEPS)
    $(CC) -c -o $@ $< $(CFLAGS)

esegui: $(OBJ)
    $(CC) -o $@ $^ $(CFLAGS)

make clean:
    rm -f *exe
    rm -f *o

These are the errors

main.o:main.cpp:(.text+0x1d7): undefined reference to `Libri::Libri(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
main.o:main.cpp:(.text+0x240): undefined reference to `Libri::stampa[abi:cxx11]() const'
main.o:main.cpp:(.text$_ZN5LibriD1Ev[__ZN5LibriD1Ev]+0xa): undefined reference to `vtable for Libri'
collect2.exe: error: ld returned 1 exit status
make: *** [esegui] Error 1
  • 3
    You didn't include `Libri.cpp` in your make file – more precisely: It's object file within `OBJ` variable. – Aconcagua May 23 '22 at 09:54
  • 1
    Your dependencies should include `libri.o`. I.e `OBJ = libri.o pubblicazioni.o main.o` . – G.M. May 23 '22 at 09:55
  • 1
    Off-topic: I strongly recommend writing all of your code in pure English (including comments) – apart from such wild language mixture being unaesthetic you *will* some day share your code with non-italophones, and if only here on SO. Not having to guess the intention of a variable, function, class, ... will make it much easier for us to spot logical errors, for colleagues to understand your code – or for you posting questions not having to explain each single bit of all this to us. – Aconcagua May 23 '22 at 09:59
  • 2
    Unrelated: Your `make clean` target looks wrong and dangerous. Make the target name `clean` and then `rm -f esegui.exe $(OBJ)` – Ted Lyngmo May 23 '22 at 09:59
  • 1
    General recommendation: Learn to read compiler/linker output. `undefined reference to ` always is a linker error telling you that some functions or (global, static class, etc) variables haven't been available to link, which almost always means that you've forgotten to provide a library or an object file to the linker – and if you get that far your CPP code (usually) has been fine so far... OK, sometimes you might have forgotten to implement the function at all, too ;) – Aconcagua May 23 '22 at 10:06
  • Thank you all! I solved it the way you told me. I am a novice, distraction errors happen most frequently at this stage. I welcome all your advice. I ask another question, how can I remove the .exe file with the clean command? – Castrese Basile May 23 '22 at 10:08
  • 1
    *'How can I remove [...]'* – see Ted's comment... – Aconcagua May 23 '22 at 10:09

0 Answers0