繁体   English   中英

C++ 多态性在 Code::Blocks 17.12 中无法正常工作

[英]C++ Polymorphism does not work as it should in Code::Blocks 17.12

我使用 Code::Blocks 学习了 C++。 但是,编译器在 Daughter.h 文件的第 6 行中给了我一个错误,当我尝试实现继承时,它说"error: expected-class name before '{' token" 这让我更加困惑,因为我在本教程中使用 Code::Blocks 和编译器观看的教程没有给出任何错误,实际上它运行良好。 在这一点上,我有点沮丧。 有人知道如何使它工作吗? 这是我的所有文件:

主程序

//main.cpp
#include "Daughter.h"
#include "Mother.h"
#include <iostream>
using namespace std;

int main()
{
    Mother jelo;
    jelo.sayname();
    Daughter tina;
    tina.sayname();
}

妈妈.h

//Mother.h
#ifndef MOTHER_H
#define MOTHER_H


class Mother
{
    public:
        Mother();
        void sayname();
};

#endif // MOTHER_H

妈妈.cpp

//Mother.cpp
#include "Daughter.h"
#include "Mother.h"
#include <iostream>
using namespace std;

Mother::Mother()
{
}

void Mother::sayname() {

    cout << "I am Roberts" << endl;

}

女儿.h

//Daughter.h
#ifndef DAUGHTER_H
#define DAUGHTER_H


class Daughter: public Mother
{
    public:
        Daughter();
};

#endif // DAUGHTER_H

女儿.cpp

//Daughter.cpp
#include "Daughter.h"
#include "Mother.h"
#include <iostream>
using namespace std;

Daughter::Daughter()
{
}

Daughter.h 应该#include "Mother.h" ,因为它需要知道Mother是什么。

如果教程没有这么说,说明教程是错误的。

而是从一本好书中学习 C++。

暂无
暂无

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

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