English 中文(简体)
从衍生的班子转换到基级点,在档案被分为pp和h文档时,则不工作。
原标题:Conversion from a derived class pointer to a base class pointer is not working when files are split into .cpp and .h files

Shape.h

#include"圆环。"
#include"广场"
class Shape {
public:
    virtual void Draw() = 0;        
    static Shape* Create(std::string type);
};

Shape.cpp

#include "Shape.h"
Shape* Shape::Create(string type) {
    if ( type == "circle" ) return new Circle();
    if ( type == "square" ) return new Square();
    return NULL;
}

圆环。

#include "圆环。"
void Circle::Draw() {
    cout << "I am circle" << endl;
}

圆环。

#include"Shape.h"
class Circle:public Shape {
public:
    void Draw();
    friend class Shape;
};

广场。

#include "广场"
void Square::Draw() {
    cout << "I am Square" << endl;
}

广场

#include"Shape.h"
class Square:public Shape {
public:
    void Draw();
    friend class Shape;
};

It throws this error: 广场 error: expected class-name before { token [for the inheritance it does not identify Shape]

But the same code works if it is in a monolithic file(without the .cpp and .h) a single main.cpp file What is that I am missing with including header files?

预付款。

最佳回答

您需要删除<代码>#include“Square.h”和#include “Circle.h” from Shape.h

Square Circle取决于对Shape的界定,因为它们都继承了<条码>但<条码>;Shape可以而且必须在<条码>之前加以界定。

问题回答

你创造了一个循环包容。 Shape.h包括Circle.hSquare.h。 这没有任何意义。

显然,你也使用某种警卫,你没有在这里向我们表明这一点(如果你说错误会有所不同)。 包括警卫人员确保不将<代码>Shape.h本身包括在内。 结果是,在<代码>Shape的定义之前,将<代码>Square.h列入Shape>h,这就是为什么你出现错误的原因。

通知的列入总是无用的。 为什么

#include "Circle.h"
#include "Square.h"

载于Shape.h? 包括做些什么? 您必须确保不将<代码>Circle.h和Square.h列入Shape.h





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签