Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7975883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:46:02+00:00 2026-06-04T08:46:02+00:00

I’m writing some code where I need to have a class variable that’s a

  • 0

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

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

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

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;

}

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

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T08:46:03+00:00Added an answer on June 4, 2026 at 8:46 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.