简体   繁体   中英

visual c++ undeclared identifier

in Ah

#pragma once
include "B.h"
class A {
    B* aPtrToB;
}

in Bh

#pragma once
include "A.h"
class B{
    A* aPtrToA;
}

visual c++ says "error C2065: 'A' : undeclared identifier"

any ideas?

thanks!

You have a cyclic inclusion. The #pragma once is preventing the infinite inclusion that would result from this, but it means that either A won't have the definition of B above it or B won't have the definition of A above it, depending on which ever was compiled first.

The solution is to not #include the header files, since you only need a forward declaration to declare a pointer:

#pragma once

class B;

class A {
    B* aPtrToB;
};

and:

#pragma once

class A;

class B {
    A* aPtrToA;
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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