如何显式调用基类的operator =

时间:2019-07-15 16:05:49

标签: c++ operator-overloading

我试图在派生类中调用基类的operator =的重载,但它返回未定义的引用。那个电话错了吗?能见度范围是否存在问题?

我在搜索堆栈溢出内容和书籍时都发现,该调用应该是正确的。试图以不同的方式更改代码,但无论如何将无法正常工作。可能是我在做一些愚蠢的错误。

Multimedia.h

namespace Appa{


class Multimedia
{
    protected:
        char* name;
        char* language;
        bool subtitle;
        char* genre;

    public:
        Multimedia();
        Multimedia(const char*, const char*, const bool, const char*);
        Multimedia(const Multimedia&);
        ~Multimedia();

        const Multimedia& operator=(const Multimedia&);

};
}

Multimedia.cpp

#include "Multimedia.h"
namespace Appa{
const Multimedia& Multimedia::operator=(const Multimedia& M){
    if(this==&M) return *this;
    if(this->name) delete[]this->name;
    this->name=new char[strlen(M.name)+1];
    strcpy(this->name,M.name);
    if(this->language) delete[] this->language;
    this->language=new char[strlen(M.language)+1];
    strcpy(this->language,M.language);
    this->subtitle=M.subtitle;
    if(this->genre) delete[]this->genre;
    this->genre=new char[strlen(M.genre)+1];
    strcpy(this->genre,M.genre);
    return *this;
} 
}

Film.h

#include "Multimedia.h"
namespace Appa{

class Film : public Multimedia
{

    protected:
        char*production;
        int duration;

    public:
        Film();
        Film(const char*, const char*, const bool, const char*, const char*, const int);
        Film(const Film&);
        ~Film();

        const Film& operator=(const Film&);
};
}

Film.cpp

#include "Film.h"

namespace Appa{

const Film& Film::operator=(const Film& F){
    if(this==&F) return *this;
    Multimedia::operator=(F); //line where it call the undefined reference
    if(this->production) delete[]this->production;
    this->production=new char[strlen(F.production)+1];
    strcpy(this->production,F.production);
    this->duration= F.duration;
    return *this;
}

}

Makefile.win

# Project: Cosa_guardo
# Makefile created by Dev-C++ 5.11

CPP      = g++.exe
CC       = gcc.exe
WINDRES  = windres.exe
OBJ      = main.o Multimedia.o Film.o Serie.o Lista.o
LINKOBJ  = main.o Multimedia.o Film.o Serie.o Lista.o
LIBS     = -L"C:/Program Files (x86)/Dev-Cpp/MinGW64/lib" -L"C:/Program Files (x86)/Dev-Cpp/MinGW64/x86_64-w64-mingw32/lib" -static-libgcc
INCS     = -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include"
CXXINCS  = -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++"
BIN      = Cosa_guardo.exe
CXXFLAGS = $(CXXINCS) 
CFLAGS   = $(INCS) 
RM       = rm.exe -f

.PHONY: all all-before all-after clean clean-custom

all: all-before $(BIN) all-after

clean: clean-custom
    ${RM} $(OBJ) $(BIN)

$(BIN): $(OBJ)
    $(CPP) $(LINKOBJ) -o $(BIN) $(LIBS)

main.o: main.cpp
    $(CPP) -c main.cpp -o main.o $(CXXFLAGS)

Multimedia.o: Multimedia.cpp
    $(CPP) -c Multimedia.cpp -o Multimedia.o $(CXXFLAGS)

Film.o: Film.cpp
    $(CPP) -c Film.cpp -o Film.o $(CXXFLAGS)

Serie.o: Serie.cpp
    $(CPP) -c Serie.cpp -o Serie.o $(CXXFLAGS)

Lista.o: Lista.cpp
    $(CPP) -c Lista.cpp -o Lista.o $(CXXFLAGS)

错误:

C:。\ Film.o Film.cpp :(。text + 0x4af):对`Appa :: Multimedia :: operator =(Appa :: Multimedia const&)'的未定义引用

0 个答案:

没有答案