简体   繁体   English

从另一个Abstract类派生一个Abstract类时,C ++中的类重新定义错误

[英]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. 我是C ++抽象类的新手,我正在尝试学习如何使用它。 So I started by defining an abstract class with only pure functions, let's call this class SceneObj , so far so good. 因此,我首先定义了一个仅包含纯函数的抽象类,我们将其称为SceneObj类,到目前为止, SceneObj很好。 Afterwards, I start by defining a new abstract class that I'm calling IScreen ; 然后,我首先定义一个新的抽象类,我将其称为IScreen this new Class is, also, another abstract class, but it add new requirements. 这个新的Class也是另一个抽象类,但是它增加了新的要求。

Unfortunately when trying to compile this simple code I ran into the following error: error C2011: 'IScreen' : 'class' type redefinition . 不幸的是,当尝试编译此简单代码时,我遇到了以下错误: error C2011: 'IScreen' : 'class' type redefinition

I'm using Visual Studio 2012 and the code that I'm trying to compile is the following: 我正在使用Visual Studio 2012,并且尝试编译的代码如下:

#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. 编辑:将代码更改为最少的一个edit2:这在header file并且显然,如果我将其更改为.cpp它可以毫无问题地进行编译。 But I needed/wanted to declare my class in headers and then define then in .cpp . 但是我需要/想要在headers声明我的类,然后在.cpp定义。

C++programs also use the preprocessor to define header guards. C ++程序还使用预处理器定义头保护。 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. #define指令采用名称并将该名称定义为预处理器变量。 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. 还有两个指令可以测试是否已定义给定的预处理器变量:如果已定义变量,则#ifdef为true;如果未定义变量,则#ifndef为true。 If the test is true, then everything following the #ifdef or #ifndef is processed up to the matching #endif . 如果测试为真,则#ifdef#ifndef之后的所有内容都会处理到匹配的#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. 因此,您必须将抽象类放在#define和#endif之间。 (This is the definition the compiler will consider). (这是编译器将考虑的定义)。 you simply don't have these etiquettes on the cpp file. 您只是在cpp文件中没有这些礼节。 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. 这意味着您已经定义了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. 如果发现错误,MS VS通常会给出几条消息。

Another reason can be that you included the cpp module with member function definitions in the module with main. 另一个原因可能是您在具有main函数的模块中包括了带有成员函数定义的cpp模块。

For example 例如

Header file: header.h 头文件:header.h

#include <stdlib.h>
using namespace std;

class SceneObj
{
   //...
};

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

cpp module with member function definitions: module,cpp 具有成员函数定义的cpp模块:module,cpp

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

module with main 主模块

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

Also include directive 还包括指令

#pragma once

in your header file. 在您的头文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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