在 C++ 中实现虚函数和 Run-time 多态性时编译时错误

Compile time errors while implement Virtual Functions and Run-time polymorphism in C++

我创建了以下程序来实现 Run-time C++ 中的多态性

/* Consider a book shop which sells both books and video-tapes. Create a class know as media that storea the title
and price of a publication.*/

#include <iostream>
#include <cstring>
using namespace std;

class media // defining base class
{
protected:
    char title[50];
    float price;

public:
    media(char *s, float a)
    {
        strcpy(title, s);
        price = a;
    }
    virtual void display() {}
};

class book : public media // defining derived class 'book' which is derived from 'media'
{
    int pages;

public:
    book(char *s, float a, int p) : media(s, a) // constructor of derived class
    {
        pages = p;
    }
    void display() {}
};

class tape : public media // defining derived class 'tape' which is derived from 'media'
{
    float time;

public:
    tape(char *s, float a, float t) : media(s, a) // constructor of derived class
    {
        time = t;
    }
    void display() {}
};

void book ::display() // function to display book details
{
    cout << "\n Title: " << title;
    cout << "\n Pages: " << pages;
    cout << "\n Price: " << price;
}

void tape::display() // function to display tape details
{
    cout << "\n Title: " << title;
    cout << "\n Playtime: " << time;
    cout << "\n Price: " << price;
}

int main()
{
    char *title = new char[30]; // creating a array of characters using 'new' for storing title
    float price, time;
    int pages;

    //Book Details

    cout << "\n Enter Book Details \n";
    cout << "Title: ";
    cin >> title;
    cout << "Price: ";
    cin >> price;
    cout << "Pages: ";
    cin >> pages;

    book book1(title, price, pages);

    //Tape Details

    cout << "\n Enter Tape Details \n";
    cout << "Title: ";
    cin >> title;
    cout << "Price: ";
    cin >> price;
    cout << "Play time (mins): ";
    cin >> time;

    tape tape1(title, price, time);

    media *list[2];
    list[0] = &book1;
    list[1] = &tape1;

    cout << "\n Media Details";

    cout << "\n.....Book....";
    list[0]->display(); // display Book details
    cout << "\n.....Tape....";
    list[1]->display(); // display Tape details
    return 0;
}

它使用构造函数、'new' 内存分配运算符和虚函数来达到目的

问题是:

考虑一家同时销售书籍和录像带的书店。创建一个名为媒体的 class 存储出版物的标题和价格。

但最终出现以下错误


virtual_function.cpp:47:6: error: redefinition of ‘void book::display()’
   47 | void book ::display() // function to display book details
      |      ^~~~
virtual_function.cpp:32:10: note: ‘virtual void book::display()’ previously defined here
   32 |     void display() {}
      |          ^~~~~~~
virtual_function.cpp:54:6: error: redefinition of ‘void tape::display()’
   54 | void tape::display() // function to display tape details
      |      ^~~~
virtual_function.cpp:44:10: note: ‘virtual void tape::display()’ previously defined here
   44 |     void display() {}
      |          ^~~~~~~

我在 Ubuntu

VSCode 中使用 GNU-GCC 编译器

问题是而不是在classbooktape中声明函数display,因为你定义了它们,因为你有花括号 {}。然后您再次在 class.

之外重新定义它们

解决这个问题,只需替换:

void display() {}

void display() ;

在 class booktape 中。所以你的 class booktape 现在看起来像:

class book : public media 
{
    int pages;

public:
    book(char *s, float a, int p) : media(s, a) 
    {
        pages = p;
    }
    void display(); //a declaration. I removed the curly braces {} you had here
};

class tape : public media 
{
    float time;

public:
    tape(char *s, float a, float t) : media(s, a) 
    {
        time = t;
    }
    void display() ; //a declaration.I removed the curly braces {} you had here 
};

请注意,在上面的代码中,我删除了大括号 {} 并用 semicolon 替换了函数 display().

程序现在可以运行了(也就是说它不再有你提到的错误)可以看到 here