C2084 - 功能已经有了一个身体

时间:2017-04-03 02:09:52

标签: c++ virtual

我遇到了错误 “C2084 - function'void Pet :: display(void)'已经有一个正文” 。 Dog.cpp文件出错。对此问题略有困惑。任何帮助,将不胜感激。

Pet.h

public class test {
    public static void main(String[] args) throws Exception {

    MapPanel mapPanel = new MapPanel();
    ContentPanel contentPanel = new ContentPanel((JPanel)mapPanel);
    Map map = new Map();
    mapPanel.add(map);
    map.loadMap();

    }
}
class MapPanel extends JPanel{
    public MapPanel(){
    //this.setBackground(Color.BLACK);

    }
}

class Map extends JPanel{
   BufferedImage image;
   public Map(){
        try {
            image = ImageIO.read(new File("graphics//brick_brown0.png"));
        } catch (IOException e) {
            System.err.println("can't find file.");
        }
    setLayout(new GridLayout(31,30));
    setPreferredSize(new Dimension(962,992));
}
public void loadMap(){
    for(int i = 0; i < 30; i++){
        for(int p = 0; p < 31; p++){
            add(new Tile(image));
        }
    }
    validate();
}
}

class Tile extends JComponent{

BufferedImage image;
public Tile(BufferedImage image){
    this.image = image;
    setPreferredSize(new Dimension(32,32));
}

public void paintComponent(Graphics g){
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    g2.drawImage(image, null, null);
}
}


class ContentPanel extends JPanel implements ActionListener{
Timer clock = new Timer(100, this);

public ContentPanel(JPanel mapPanel){
    clock.start();
    setLayout(new BorderLayout());
    JFrame frame = new Frame();
    frame.setContentPane(this);
    add(mapPanel);
    frame.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
    repaint();
}

private class Frame extends JFrame{ 
    public Frame(){
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setBounds(100, 100, 1000, 1000);
    }
}
}

Dog.h

#ifndef _PET_H_
#define _PET_H_

#include <string>
using namespace std;

enum Type { dog = 0, cat };

class Pet {
private:
    string name, breed; // private local variables
    Type type;

public:
    Pet(string pet_name, string pet_breed, Type pet_type); // constructor

    // accessor methods
    string getName();
    string getBreed();
    Type getType();

    virtual void display() {};
};

#endif // _PET_H_

Dog.cpp

#ifndef _DOG_H_
#define _DOG_H_

#include "Pet.h"
#include <string>

using namespace std;

class Dog : public Pet {
public:
    Dog(string pet_name, string pet_breed, Type pet_type) : Pet(pet_name, pet_breed, pet_type) {
    } // constructor

    virtual void display() = 0;

};

#endif 

3 个答案:

答案 0 :(得分:1)

function a(){} function b(){} function c(){ a(); b();} export default c 中,您已经为Pet.h定义了一个不执行任务的正文。

display()

Pet.h

class Pet { /* other class members */ virtual void display() {}; // Here is your virtual function with an empty body. };

Dog.h

将两者交换。

  • 制作class Dog : public Pet { /* other class members */ virtual void display() = 0; // This pure virtual function which is inheriting from Pet which is not purely virtual. }; &#39; s Pet,即纯虚函数。
  • 制作virtual void display() = 0&#39; Pet,即您已在Dog.cpp中实施的虚拟功能。

<强>解决方案:

virtual void display()

Pet.h

class Pet { /* other class members */ virtual void display() = 0; // Make this a pure virtual function. It does not have an implementation. };

Dog.h

答案 1 :(得分:1)

您似乎想要定义Dog :: display并忘记将Pet重命名为Dog:

void Dog::display() {
    cout << "Name: " << name << endl;
    cout << "Breed: " << breed << endl;
    cout << "Type: Dog" << endl;
}

同时从<:p>中删除“= 0”

virtual void display() = 0;

在“Dog.h”中。

在函数原型正常后“= 0”的“Pet.h”文件中,这意味着你不应该直接实例化Pet类(抽象类)。

答案 2 :(得分:0)

您在类display中声明方法Dog pure virtual,该方法派生自另一个类Pet。但是Pet类已经有一个名为display的具体方法。那就是为什么你会收到这个错误。

display类中将Pet声明为纯虚拟,然后在其子类Dog中提供具体实现。