c++ - 出了什么问题 - '{' 标记之前的预期类名

标签 c++

这段代码有什么问题?我得到 [Error] expected class-name before '{' token (Pralka.h 第 14 行)

我知道这里有很多类似的问题。我也用谷歌搜索过,但我无法克服它。所以我想向您展示我的代码..

我写了这段非常简单的代码来训练自己继承和虚函数..

ma​​in.cpp:

   #include <iostream>
    #include <fstream>
    #include <string>

    #include "AGD.h"

    using namespace std;

    int main() {        
    Pralka p1("polar", 1250); 
    AGD *A;
    A = &p1; 
    }

AGD.h:

#ifndef AGD_H
#define AGD_H

#include <iostream>

#include "Pralka.h"

class AGD {

    private:

        static int liczba_sprzetow;

    public:

        AGD(){
            liczba_sprzetow++;
        }

        ~AGD(){
            liczba_sprzetow--;
        }


        static int  get_liczba_sprzetow() {
            return liczba_sprzetow;
        }

        virtual double get_cena() {
        }

};

#endif

Pralka.h:

#ifndef PRALKA_H
#define PRALKA_H

#include <iostream>
#include <string>



using namespace std;

class Pralka : public AGD
 {
    private:

        string marka;
        double cena;

    public:

        Pralka(string m, double c): marka(m), cena(c){
        }

        Pralka(){
        }
        ~Pralka(){
        }


        string get_marka() const{
            return marka;
        }

        double get_cena() const{
            return cena;
        }

        Pralka& operator=(const Pralka& Q){
         marka=Q.marka;
        cena=Q.cena;
        }

};

#endif

我也得到了[Error] cannot convert 'Pralka*' to 'AGD*' in assignment 但为什么呢?我不明白(main.cpp 第 29 行)。

最佳答案

AGD.h 包含 Pralka.h,但应该反过来(Pralka.h 应该包含 AGD.h).

原因是Pralka需要看到AGD声明才能继承它。 AGD 不需要看到 Pralka 声明。

关于c++ - 出了什么问题 - '{' 标记之前的预期类名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21454733/

相关文章:

c++ - 重构构建系统以使用 Autotools

c++ - GCC6.3.0 : flag "--coverage" not functioning (no gdca files generated)

c++ - 在 QLineEdit 中用点替换逗号

javascript - 在 JavaScript 中打印字节数组的十六进制或十进制值

c++ - GNU 将所有 .cpp 文件编译为 .o 并包含 .h 文件

c++ - 如何使用多态性使复合对象成为派生类的全局对象?

c++ - 位交换和指针

c++ - 这种类型的双关语定义明确吗?

c++ - 使用boost线程的多线程中的段错误(核心转储)

c++ - constexpr 数组成员是否编译时间常数?