C++ C++;不命名使用类的类型

C++ C++;不命名使用类的类型,c++,class,extern,C++,Class,Extern,我想在“update”函数中修改类“abc”的对象“t1”,该类在不同的文件(temp2.cpp)中定义,而不是在定义“t1”的文件(temp1.cpp)中定义。我试图使用extern,但结果是错误的。请建议一个好的方法 temp1.ccp #include<iostream> #include "test2.cpp" using namespace std; class abc{ public: int x; char y; void printxy(

我想在“update”函数中修改类“abc”的对象“t1”,该类在不同的文件(temp2.cpp)中定义,而不是在定义“t1”的文件(temp1.cpp)中定义。我试图使用extern,但结果是错误的。请建议一个好的方法

temp1.ccp

#include<iostream>
#include "test2.cpp"

using namespace std;
class abc{
  public:
    int x;
    char y;
    void printxy(){
      cout<<x<<y<<endl;
    } 
};

abc t1;
int main(){
  update();
return 0;
}
在test.cpp:2:0:test2.cpp:1:8中包含的文件中:错误:`abc'未命名类型 外用abct1; ^

test2.cpp:在函数“void update()”中:


test2.cpp:3:2:error:'t1'未在此范围内声明t1.x=5

在声明类
abc
之前包含
test2.cpp
:包含的文件只是在出现
#include
的位置展开。您可能希望将代码与编译器的
-E
选项一起使用,以查看预处理后文件的外观(在这种情况下,您可能还希望省略
#include
,因为它将产生大量输出)


一般来说,包含
.cpp
文件不是一个好主意。您的意思是声明是一个头文件(例如,
test2.h
),并且包含在
test2.cpp
类型中吗?在这种情况下,声明的顺序就可以了。

我终于得到了它。感谢您的评论:)

h班

class abc{
  public:
    int x;
    char y;
    void printxy(){
      std::cout<< x << y <<std::endl;
    }; 
};
主文件

#include <iostream>
#include "class.h"
#include "func.h"

abc t1;

int main(){
  update();

return 0;
}
#包括
#包括“h类”
#包括“func.h”
abct1;
int main(){
更新();
返回0;
}
输出

5A


test2.cpp对类及其内部结构没有概念/概念-如果您提供了定义它的头文件,它会有概念/概念。永远不要包含
.cpp
文件。这是错误的方法!!你想解决/实现什么?
extern abc t1;
void update(){
  t1.x=5;
  t1.y='A';
  t1.printxy();
};
#include <iostream>
#include "class.h"
#include "func.h"

abc t1;

int main(){
  update();

return 0;
}