C++ c++;extern类不命名类型

C++ c++;extern类不命名类型,c++,linux,compiler-errors,compilation,C++,Linux,Compiler Errors,Compilation,我试图将一个类对象声明为extern,但得到以下错误: 我已经进去看过了 及 我想我正在遵循上面提到的步骤。但是找不到问题所在 档案包括: a1.cpp #include<iostream> #include "b1.h" b1 obj_b1; int main(){ //access object from class B std::cout << " test " << std::endl; std::cout <

我试图将一个类对象声明为
extern
,但得到以下错误:

我已经进去看过了 及

我想我正在遵循上面提到的步骤。但是找不到问题所在

档案包括:

a1.cpp

#include<iostream>
#include "b1.h"

b1 obj_b1;


int main(){

    //access object from class B
    std::cout << " test  " << std::endl;
    std::cout << " obj_b1 value is   " << obj_b1.value << std::endl;
    obj_b1.value = 6;
    return 0;
}
#include <iostream>
#include "b1.h"

int b1::print_value(){
        std::cout << "value in b1 is " << value << std::endl;
}
#include<iostream>
#include "c1.h"

int c1::c1_print_value()
{
    std::cout << "in c1 , value is " << c1_value << std::endl;
    std::cout << " obj_b1.value is " << obj_b1.value << std::endl;
    return 0;
}
b1.cpp

#include<iostream>
#include "b1.h"

b1 obj_b1;


int main(){

    //access object from class B
    std::cout << " test  " << std::endl;
    std::cout << " obj_b1 value is   " << obj_b1.value << std::endl;
    obj_b1.value = 6;
    return 0;
}
#include <iostream>
#include "b1.h"

int b1::print_value(){
        std::cout << "value in b1 is " << value << std::endl;
}
#include<iostream>
#include "c1.h"

int c1::c1_print_value()
{
    std::cout << "in c1 , value is " << c1_value << std::endl;
    std::cout << " obj_b1.value is " << obj_b1.value << std::endl;
    return 0;
}
c1.cpp

#include<iostream>
#include "b1.h"

b1 obj_b1;


int main(){

    //access object from class B
    std::cout << " test  " << std::endl;
    std::cout << " obj_b1 value is   " << obj_b1.value << std::endl;
    obj_b1.value = 6;
    return 0;
}
#include <iostream>
#include "b1.h"

int b1::print_value(){
        std::cout << "value in b1 is " << value << std::endl;
}
#include<iostream>
#include "c1.h"

int c1::c1_print_value()
{
    std::cout << "in c1 , value is " << c1_value << std::endl;
    std::cout << " obj_b1.value is " << obj_b1.value << std::endl;
    return 0;
}
#包括
#包括“c1.h”
int c1::c1_打印值()
{

std::cout
b1.h
包括
c1.h
,和
c1.h
包括
b1.h
。这是一个混乱。通过使用
#indef
/
#define
组合,可以防止无限递归,但仍然是一个混乱

obj_b1
类别c1
无关,因此从
c1.h
中删除
extern b1 obj_b1;

现在
c1.h
不依赖于
b1.h
中的任何内容,因此您可以从
c1.h
中删除
#包括“b1.h”
。 出于类似的原因,您应该从
b1.h
中删除
#包括“c2.h”

另一方面,c2.cpp确实依赖于
obj_b1
(假设
obj1.name
是一个打字错误,并且应该是
obj_b1.name
),因此您应该将
extern b1 obj_b1;
放在
b1.h
中,并在
c2.cpp>中包含“b1.h”


对于一些额外的清理,请将
b1 obj_b1;
a1.cpp
移动到
b1.cpp

b1.h
包含
c1.h
c1.h
包含
b1.h
。这是一个混乱。通过使用
\indef
/define
组合,可以防止无限递归,但仍然是一个错误美国

obj_b1
类别c1
无关,因此从
c1.h
中删除
extern b1 obj_b1;

现在
c1.h
不依赖于
b1.h
中的任何内容,因此您可以从
c1.h
中删除
#包括“b1.h”
。 出于类似的原因,您应该从
b1.h
中删除
#包括“c2.h”

另一方面,c2.cpp确实依赖于
obj_b1
(假设
obj1.name
是一个打字错误,并且应该是
obj_b1.name
),因此您应该将
extern b1 obj_b1;
放在
b1.h
中,并在
c2.cpp>中包含“b1.h”


对于一些额外的清理,请将
b1 obj_b1;
a1.cpp
移动到
b1.cpp

删除
#包括“c1.h”
来自
b1.h
。我想声明并使用b1.cpp中的c1类对象。这就是在b1.h中包含c1.h的原因。然后将其移动到
b1.cpp
b1.h
不使用
c1.h
中的任何内容。只包含必要的头文件是一个好习惯。删除
#包含“c1.h”
来自
b1.h
。我想声明并使用b1.cpp中的c1类对象。这就是在b1.h中包含c1.h的原因。然后将其移动到
b1.cpp
b1.h
不使用
c1.h
中的任何内容。只包含必要的头文件是一个好习惯。