I'm writing some code where I need to have a class variable that's a static int array. I understand that I can do this with something like this in the header file, A.h:
我正在编写一些代码,我需要一个类变量,它是一个静态int数组。我知道我可以在头文件中用这样的东西做这个,A.h:
#ifndef A_H_
#define A_H_
class A
{
public:
static const int a[];
};
const int A::a[] = {1,2};
#endif
This works just fine if I'm then including this header in only one other file, something like the following, main.cpp:
如果我将此标题只包含在另一个文件中,如下所示,main.cpp:
#include "A.h"
#include <iostream>
using namespace std;
int main()
{
A myA;
cout << "0: " << myA.a[0] << endl;
cout << "1: " << myA.a[1] << endl;
}
But suppose I need my class A to be a bit more complicated, and I want to have an A.cpp file as well. I'll keep my main.cpp file the same, but then change A.h as follows (I've just added a function, printA):
但是假设我需要我的A类有点复杂,我想要一个A.cpp文件。我将保持我的main.cpp文件相同,但然后更改A.h如下(我刚刚添加了一个函数,printA):
#ifndef A_H_
#define A_H_
class A
{
public:
void printA() const;
static const int a[];
};
const int A::a[] = {1,2};
#endif
And then in file A.cpp:
然后在文件A.cpp中:
#include "A.h"
#include <iostream>
using namespace std;
void A::printA() const
{
cout << "Printing in A.cpp." << endl;
cout << "A.0: " << a[0] << endl;
cout << "A.1: " << a[1] << endl;
}
Compiling A.o with gcc -o A.o -c A.cpp is fine. But linking this when compiling main.cpp (gcc -o atest main.cpp A.o) fails with "multiple definition of `A::a'".
使用gcc -o A.o -c A.cpp编译A.o很好。但是在编译main.cpp(gcc -o atest main.cpp A.o)时将其链接失败,并且“a :: a'的多重定义”。
I've been scouring the internet for solutions, and found people who have variables declared in their headers who get the "multiple definition" error when they include the header in multiple places, and the solution seems to be to declare the variable extern in the header, then define it in only one source (non-header) source file. But I can't declare a class variable both static and extern, can I? If I try, or if I just declare it extern, I get a warning about the variable not being static (also a "conflicting specifiers" error when I try both).
我一直在互联网上搜索解决方案,并发现在标题中声明变量的人在多个地方包含标题时会出现“多重定义”错误,解决方案似乎是在变量extern中声明标头,然后在只有一个源(非标头)源文件中定义它。但是我不能声明一个静态和外部的类变量,可以吗?如果我尝试,或者我只是声明它是extern,我得到一个关于变量不是静态的警告(当我尝试两者时也是一个“冲突的说明符”错误)。
So, my question: is it possible to have static array class variables in the case that the header file needs to be included in more than one source file? If so, how?
所以,我的问题是:在头文件需要包含在多个源文件中的情况下,是否可以使用静态数组类变量?如果是这样,怎么样?
2 个解决方案
#1
17
You're violating the one definition rule. Move the definition inside an implementation file:
您违反了一个定义规则。在实现文件中移动定义:
//A.cpp
#include "A.h"
const int A::a[] = {1,2};
The solution you are reffering to, with extern
, applies to non-member variables. In your case a
is a class member.
您使用extern进行解决的解决方案适用于非成员变量。在你的情况下,a是一个类成员。
#2
7
You should remove the "const int A::a[] = {1,2};" line from the header file. Put this definition line in one of you .cpp files. Then you can include the header file several times where you need it.
你应该删除“const int A :: a [] = {1,2};”头文件中的行。将此定义行放在您的.cpp文件中。然后,您可以在需要的位置多次包含头文件。
#1
17
You're violating the one definition rule. Move the definition inside an implementation file:
您违反了一个定义规则。在实现文件中移动定义:
//A.cpp
#include "A.h"
const int A::a[] = {1,2};
The solution you are reffering to, with extern
, applies to non-member variables. In your case a
is a class member.
您使用extern进行解决的解决方案适用于非成员变量。在你的情况下,a是一个类成员。
#2
7
You should remove the "const int A::a[] = {1,2};" line from the header file. Put this definition line in one of you .cpp files. Then you can include the header file several times where you need it.
你应该删除“const int A :: a [] = {1,2};”头文件中的行。将此定义行放在您的.cpp文件中。然后,您可以在需要的位置多次包含头文件。