简体   繁体   中英

Class redefinition error in C++ while deriving an Abstract Class from another Abstract Class

I'm new to C++ abstract classes and I'm trying to learn how to work with it. So I started by defining an abstract class with only pure functions, let's call this class SceneObj , so far so good. Afterwards, I start by defining a new abstract class that I'm calling IScreen ; this new Class is, also, another abstract class, but it add new requirements.

Unfortunately when trying to compile this simple code I ran into the following error: error C2011: 'IScreen' : 'class' type redefinition .

I'm using Visual Studio 2012 and the code that I'm trying to compile is the following:

#include <stdlib.h>
using namespace std;

class SceneObj
{
protected:
    float center;

public:
    virtual void SetCenter(float,float,float) = 0;
    virtual void SetCenter(float) = 0;
    virtual float GetCenter() = 0;
    virtual ~SceneObj();
};

class IScreen : public SceneObj
{
public:
    virtual void SetCenter(float,float,float) = 0;
    virtual void SetCenter(float) = 0;
    virtual float GetCenter() = 0;
    virtual float GetStartCorner() = 0;

    virtual void SetSize(float,float) = 0;
    virtual void SetSize(long) = 0;
    virtual long GetSize() = 0;

    virtual ~IScreen();
};

Could someone point me what/where is the flaw in this code?

edit: Changed code to a minimal one edit2: This is in a header file and apparently if i change it to a .cpp it compiles without problems. But I needed/wanted to declare my class in headers and then define then in .cpp .

C++programs also use the preprocessor to define header guards. Header guards rely on preprocessor variables. Preprocessor variables have one of two possible states: defined or not defined. The #define directive takes a name and defines that name as a preprocessor variable. There are two other directives that test whether a given preprocessor variable has or has not been defined: #ifdef is true if the variable has been defined, and #ifndef is true if the variable has not been defined. If the test is true, then everything following the #ifdef or #ifndef is processed up to the matching #endif . We can use these facilities to guard against multiple inclusion as follows:

 #ifndef SALES_DATA_H
 #define SALES_DATA_H
 #include <string>
 struct Sales_data {
 std::string bookNo;
 unsigned units_sold = 0;
 double revenue = 0.0;
 };
 #endif  //SALES_DATA_H

For instance, in the header file you will find something LIKE the follwing:

#ifndef __*__SceneObj
#define __*__SceneObj__
//Place the abstract class here
#endif

So you have to put the abstract class in between the #define and the #endif. (This is the definition the compiler will consider). you simply don't have these etiquettes on the cpp file. That is why it works there. Additionally try to have one class per header file, so do not declare the child class on the same header file.

It means that somewhere you already have defined type IScreen. Usually the compiler gives a reference to the duplicated definition.

So investigate the error message.

As for you code snippet then it is irrelevant.

MS VS usually gives several messages if it found an error.

Another reason can be that you included the cpp module with member function definitions in the module with main.

For example

Header file: header.h

#include <stdlib.h>
using namespace std;

class SceneObj
{
   //...
};

class IScreen : public SceneObj
{
   //...
};

cpp module with member function definitions: module,cpp

#include "header.h"
//...

module with main

#include "header.h"
#include "module.cpp"
//...

Also include directive

#pragma once

in your header file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM