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::coutb1.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.cppb1.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
中的任何内容。只包含必要的头文件是一个好习惯。