English 中文(简体)
C++ 变量“多重定义” C++
原标题:static array class variable "multiple definition" C++

我正在写一些代码, 我需要有一个类变量, 它是一个静态的整数阵列。 我理解,我可以在页眉文件 A.h:

#ifndef A_H_
#define A_H_

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

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

#endif

如果我把这个信头 包含在另一个文件里, 类似下面的, 主. 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 文件。 我会保持我的主. cpp 文件相同, 但随后更改 A. h 如下( 我刚刚添加了一个函数, 打印 A ) :

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

#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. cpp 与 gcc - o A.o - c A. cpp 合并为 A. cpp 是罚款。 但是,在编译 main. cpp (gcc - o atest pain. cpp A.o) 时, 将此连接为“ `A: a: " 的多重定义失败 。

我一直在搜索互联网的解决方案,发现那些在信头中宣布变量的人,当他们把页眉包含在多个地方时,就会发现“多重定义”错误,而解决方案似乎是在信头中宣布变量外部程序,然后仅用一个源(非页眉)源文件来定义。但我无法同时声明一个类变量,既是静态的,也可以是外部的。如果我尝试,或者如果我只是宣布它外部程序,我就会得到关于变量不是静态的警告(我同时尝试“相冲突的标”错误 )。

因此,我的问题是:如果页眉文件需要包含在不止一个源文件里,那么,是否有可能有静态数组类变量?如果是,如何?

最佳回答

您正在违反 < strong > one 定义规则 。 将定义移动到执行文件中 :

//A.cpp
#include "A.h"
const int A::a[] = {1,2};

您正在回复的解决方案, 包括 < code> extern , 适用于非成员变量。 在您的情况下, < code> a 是分类成员 。

问题回答

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.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签