[英]Using a class/struct/union over multiple cpp files C++
I am trying to create a class in C++ and be able to access elements of that class in more than one C++ file. 我试图在C ++中创建一个类,并能够在多个C ++文件中访问该类的元素。 I have tried over 7 possible senarios to resolve the error but have been unsuccessful.
我已经尝试了7种以上的解决方案来解决该错误,但均未成功。 I have looked into class forward declaration which doesen't seem to be the answer (I could be wrong).
我研究了类向前声明,但这似乎不是答案(我可能错了)。
//resources.h
class Jam{
public:
int age;
}jam;
//functions.cpp
#include "resources.h"
void printme(){
std::cout << jam.age;
}
//main.cpp
#include "resources.h"
int main(){
printme();
std::cout << jam.age;
}
Error 1 error LNK2005: "class Jam jam" (?jam@@3VJam@@A) already defined in stdafx.obj
Error 2 error LNK1169: one or more multiply defined symbols found
I understand the error is a multiple definiton because I am including resources.h
in both CPP files. 我知道该错误是一个多重定义,因为我在两个CPP文件中都包含
resources.h
。 How can I fix this? 我怎样才能解决这个问题? I have tried declaring the
class Jam
in a CPP file and then declaring extern class Jam jam;
我曾尝试声明
class Jam
在CPP文件,然后声明extern class Jam jam;
for each CPP file that needed to access the class. 对于需要访问该类的每个CPP文件。 I have also tried declaring pointers to the class, but I have been unsuccessful.
我也尝试过声明指向该类的指针,但是我一直没有成功。 Thank you!
谢谢!
You need to separate your definition from your declaration. 您需要将定义与声明分开。 Use:
采用:
//resources.h
class Jam{
public:
int age;
};
// Declare but don't define jam
extern Jam jam;
//resources.cpp
// Define jam here; linker will link references to this definition:
Jam jam;
You're declaring the structure Jam
and creating a variable called jam
of this type. 您要声明结构
Jam
并创建一个名为jam
的变量。 The linker is complaining that you've got two (or more) things called jam
, because your header causes each .cpp
file to declare one of its own. 链接器抱怨您有两个(或更多个)名为
jam
东西,因为您的标头使每个.cpp
文件声明一个自己的文件。
To fix this, change your header declaration to: 要解决此问题,请将标头声明更改为:
class Jam{
public:
int age;
};
extern Jam jam;
And then place the following line in one of your .cpp
sources: 然后将在你的一个下面一行
.cpp
来源:
Jam jam;
The variable jam
is defined in the H file, and included in multiple CPP classes, which is a problem. 变量
jam
在H文件中定义,并且包含在多个CPP类中,这是一个问题。
Variables shouldn't be declared in H files, in order to avoid precisely that. 为了避免这种情况,不应在H文件中声明变量。 Leave the class definition in the H file, but define the variable in one of the CPP files (and if you need to access it globally - define it as
extern
in all the rest). 将类定义保留在H文件中,但在一个 CPP文件中定义变量(如果需要全局访问它-在所有其余文件中将其定义为
extern
)。
For example: 例如:
//resources.h
class Jam{
public:
int age;
};
extern Jam jam; // all the files that include this header will know about it
//functions.cpp
#include "resources.h"
Jam jam; // the linker will link all the references to jam to this one
void printme(){
std::cout << jam.age;
}
//main.cpp
#include "resources.h"
int main(){
printme();
std::cout << jam.age;
}
As a first step you should separate the definition of the class and declaration of an instance. 首先,您应该将类的定义和实例的声明分开。 Then use extern in resources.h and declare the instance in a CPP.
然后在resources.h中使用extern并在CPP中声明该实例。
Like this: 像这样:
//resources.h
class Jam{
public:
int age;
};
extern Jam jam;
//functions.cpp
#include "resources.h"
void printme(){
std::cout << jam.age;
}
//main.cpp
#include "resources.h"
Jam jam;
int main(){
printme();
std::cout << jam.age;
}
Use the pragma once directive, or some defines to make sure a header doesn't get included more than once in your code. 使用一次编译指示指令或某些定义来确保标头不会在您的代码中被多次包含。
Example using pragma directive (example.h): 使用pragma指令(example.h)的示例:
#pragma once
// Everything below the pragma once directive will only be parsed once
class Jam { ... } jam;
Example using defines (example.h): 使用定义的示例(example.h):
#ifndef _EXAMPLE_H
#define _EXAMPLE_H
// This define makes sure this code only gets parsed once
class Jam { ... } jam;
#endif
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.