静态数组类变量“多个定义"; C ++ [英] static array class variable "multiple definition" C++

查看:182
本文介绍了静态数组类变量“多个定义"; C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一些代码,其中我需要具有一个静态int数组的类变量.我了解我可以使用头文件A.h中的类似方法来做到这一点:

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:

#ifndef A_H_
#define A_H_

class A
{
public:
  static const int a[];
};

const int A::a[] = {1,2};

#endif

如果我仅在另一个文件中包含此标头,则此方法就很好,例如以下main.cpp:

This works just fine if I'm then including this header in only one other file, something like the following, 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;
}

但是,假设我需要我的类A稍微复杂一点,并且我也想拥有一个A.cpp文件.我将保持我的main.cpp文件不变,然后按如下所示更改A.h(我刚刚添加了一个函数printA):

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):

#ifndef A_H_
#define A_H_

class A
{
public:
  void printA() const;
  static const int a[];
};

const int A::a[] = {1,2};

#endif

然后在文件A.cpp中:

And then in file 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;

}

使用gcc -o A.o -c A.cpp编译A.o很好.但是,在编译main.cpp(gcc -o atest main.cpp A.o)时,此链接失败,并带有'A :: a'的多个定义".

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'".

我一直在互联网上寻找解决方案,发现人们在标头中声明了变量,当他们在多个地方包含标头时出现多重定义"错误,解决方案似乎是声明变量在标头中使用extern,然后仅在一个源(非标头)源文件中对其进行定义.但是我不能同时声明一个类变量static和extern,可以吗?如果尝试尝试,或者只是将其声明为extern,则会收到有关变量不是静态的警告(当我同时尝试这两个变量时,还会出现冲突的说明符"错误).

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).

所以,我的问题是:如果头文件需要包含在多个源文件中,那么是否可以使用静态数组类变量?如果可以,怎么办?

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?

推荐答案

您违反了一个定义规则.将定义移到实现文件中:

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};

使用extern引用的解决方案适用于非成员变量.您的情况a是班级成员.

The solution you are reffering to, with extern, applies to non-member variables. In your case a is a class member.

这篇关于静态数组类变量“多个定义"; C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆