18

Here is my C++ code

// XuatChuoiBTHang.h
#pragma once
#include "BieuThuc.h"
#include "BieuThucHang.h"

class XuatChuoiBTHang
{
    public:
        virtual string xuatChuoi(BieuThucHang* btHang) = 0;     
};

// BieuThucHang.h
#pragma once
#include "bieuthuc.h"
#include "XuatChuoiBTHang.h"

class BieuThucHang : public BieuThuc
{
    private:
        XuatChuoiBTHang* xuatChuoiBTHang;
};

Ouput is:

"error C2061: syntax error : identifier 'BieuThucHang' "

How to fix it ?

Himanshu
  • 4,327
  • 16
  • 31
  • 39
DungLe
  • 265
  • 1
  • 3
  • 10

1 Answers1

40

You have a circular dependency of header files. You need to break this circular inclusion dependency by using a forward declaration in XuatChuoiBTHang.h:

class BieuThucHang;

Also, remove #include "BieuThucHang.h" from XuatChuoiBTHang.h.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • 3
    Tell him about the circular dependency before someone else posts another answer. – user123 Mar 30 '13 at 06:57
  • Tell him he shall use header quardian as well :) – Leo Chapiro Mar 30 '13 at 06:59
  • yeah, can you suggest me something about circular dependency ? Thank u very much ! – DungLe Mar 30 '13 at 07:00
  • 3
    @DungLe: Well you cannot learn C++ on a programming forum So my suggestion is to pick up a [good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). For immediate understanding [this](http://en.wikipedia.org/wiki/Circular_dependency) might be useful. – Alok Save Mar 30 '13 at 07:03
  • In a method of class BieuThuc, I wrote: `this->xuatChuoiBTHang->xuatChuoi(this);` and the ouput is "pointer to incomplete class type is not allowed". So how to fix it ? – DungLe Mar 30 '13 at 07:22
  • @DungLe - If you have another question, please post that as a new question. – Bo Persson Mar 30 '13 at 10:24