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

  • Home
  • SEARCH
  • 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 6079639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:58:11+00:00 2026-05-23T10:58:11+00:00

Here’s my code: main.cpp #include color.h int main ( int argc , char **argv

  • 0

Here’s my code:

main.cpp

#include "color.h"

int main ( int argc , char **argv ) 
{
    Highlight h;
    return 0;
}

color.h

#ifndef HIGH_H
#define HIGH_H
#include <iostream>

using namespace std;

struct colorPattern_t
{
    int c1 , c2;

    colorPattern_t ( int a , int b )
    {
        c1 = a; c2 = b;

        cout << "colorPattern_t() with " << c1 << " , " << c2 << endl;
    }

    void say()
    {
        cout << "say() from colorPattern_t" << endl;
    };
};

class Highlight
{
    public:
    Highlight ();
};

#endif

Now color.cpp

#include "color.h"

extern colorPattern_t colors[2] = 
{
    {
        1,
        2
    } ,
    {
        4,
        5
    }
};

Highlight::Highlight()
{
    for ( int i = 0 ; i < sizeof(colors) / sizeof(colorPattern_t) ; i ++ )
    {
        colors[i].say();
    }
};

When compiling with:

g++ main.cpp color.cpp -o main

I see:

color.cpp:3:31: warning: ‘colors’ initialized and declared ‘extern’
color.cpp:13:1: warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x
color.cpp:13:1: warning: extended initializer lists only available with -std=c++0x or – std=gnu++0x

I there any suggestion for my colorPattern_t{} initialization method? I wanted them to be mounted in my code without using -std=c++0x to fix the symptom.

  • 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-05-23T10:58:11+00:00Added an answer on May 23, 2026 at 10:58 am

    In your example, I don’t see anything outside of color.cpp accessing colors, so there is no reason to have the extern on its definition.

    If other files are going to have access to this global data, then declare in colors.h that there exists a colors data structure:

    extern colorPatterns_t colors[];
    

    Then in your color.cpp drop the extern and initialize it as you’re doing. The extern tells the compiler that the variable is declared (and possibly initialized) somewhere else.

    If you do access colors from another file outside of color.cpp, the sizeof colors won’t work, so you’ll have to signal its size in some other way. Either define the number of colors

     /* in colors.h */
     #define N_COLORS 2
     extern colorPatterns_t colors[];
    
     /* in color.cpp */
     colorPatterns_t colors[N_COLORS] = { ...
    

    Or you can put in a marker in the last slot (e.g. -1 or some other obvious illegal value).

    Of course, global variables are in general evil and you should just supply a set of routines/methods in color.c to access / manipulate the colors structure so that you’re free to change or update its implementation without affecting the rest of your code base.

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

Sidebar

Related Questions

Here's my code: // Not all headers are relevant to the code snippet. #include
Here is my code, which takes two version identifiers in the form 1, 5,
Here is a table CarID| Attribute | Value 1 | Color | Red 2
Here is the code I'm using http://jsbin.com/evike5/edit When the jQuery UI dialog is fired
Here's some of my production code (I had to force line breaks): task =
Here's my function: static Map AddFormation(Map _map, Tile tile, int x, int y, int
Here is a great article about GC may occur at unexpected point of code
Here is my javascript code for a cursor focus function to go to username
here are 2 screen shots when i try to debug my code in visual
Here a simple question : What do you think of code which use try

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.