Cannot declare variable without const

Bryan Kelly 486 Reputation points
2022-08-13T16:10:12.347+00:00

Windows 11, VS 2019, C++, wxWidgets 3.2.0
I am a relative novice with VS and don’t understand some restrictions. This is becoming quite embarrassing.
It seems that a variable cannot be declared in an include file without making it a constant. That does not make sense to me. Below is the include file. I am using wxWidgets for the first time, got some starter code, and am trying to make it fit my needs.

// dialog_array.h  
// Creates an array of dialogs, eventually  
  
#pragma once  
  
#include <wx/wx.h>  
  
// all the below fails produce the same error message:  
// 1>vc_x64_mswud\wx_crypto.exe : fatal error LNK1169: one or more multiply defined symbols found  
// None of these names are repeated in the entire solution and project.  
  
// rb = radio_button  
const int rb_count  = 4;  
const int rb_length = 6;  
int       length_01;                 // fails   
const int length_02    = 10;   // fails if const not used  
  
const char rb_01    = 'x';  // fails if "const" not used  
const char rb_02[2] = "x";  // fails if "const" not used or not an array,   
  
const char rb_03[rb_count][rb_length] = { "bin",  "dec",  "hex",  "txt" }; // fails if "const" not used  
  
//const char rb_04[rb_count][rb_length];  // fails if "const" not used or not initialized  
  
class dialog_array_c : public wxFrame  
{  
public:  
    dialog_array_c(const wxString& title);  
  
    void ShowMessage1(wxCommandEvent& event);  
    void ShowMessage2(wxCommandEvent& event);  
    void ShowMessage3(wxCommandEvent& event);  
    void ShowMessage4(wxCommandEvent& event);  
  
};  
  
const int ID_INFO     = 1;  
const int ID_ERROR    = 2;  
const int ID_QUESTION = 3;  
const int ID_ALERT    = 4;  
  
  

And what tags should be used for this question? The only relevant one I can find is C++.

Developer technologies C++
{count} votes

4 answers

Sort by: Most helpful
  1. Viorel 122.5K Reputation points
    2022-08-13T17:15:49.153+00:00

    Probably you are including this header file to multiple source files, which results in repeated definitions.

    One of the solutions is to change the declaration to:

    extern int length_01;

    Then add these lines to any .cpp source file:

    #include "dialog_array.h"
    // . . .
    int length_01;

    If length_01 is needed in some single file only (not as a global variable), then simply use static int length_01; in the corresponding source file.

    0 comments No comments

  2. Bryan Kelly 486 Reputation points
    2022-08-13T18:10:29.027+00:00

    When "const" is added, it compiles ok. That would not be the case if the same name were elsewhere.
    Use the search tool as in:
    230944-find-tool.png

    It searched the entire solution and not found any instances. I am just getting started and the file count is low.

    Did I miss something from the existing replies?

    0 comments No comments

  3. WayneAKing 4,931 Reputation points
    2022-08-13T20:01:04.837+00:00

    When "const" is added, it compiles ok. That would
    not be the case if the same name were elsewhere.

    Remember that code in a header file becomes part of the
    source code in any file where it is #included. The
    preprocessor adds the code from the header to the
    source code - usually a .cpp file - before the compiler
    processes the file.

    As explained in the link that RLWA32 provided, "free"
    variables have external linkage. Therefore a non-const
    variable in a header will be "seen" by all translation
    units in the project. So a non-const variable "X" in
    header "H" will become part of each source file in
    which the header is found, and each one will have
    external linkage.

    When you make the variable const it then has internal
    linkage and is visible only within the translation unit
    where it occurs (in the cpp file via the header).

    • Wayne
    0 comments No comments

  4. Bryan Kelly 486 Reputation points
    2022-08-13T20:10:10.727+00:00

    Thank you Wayne.
    I am just starting with wxWidgets and copied the dialog stuff from a tutorial. I am now working on make everything in the two files: dialog_array.h and .cpp to be a proper class.

    Still, I make up new names that are not anywhere else in the solution and it will not compile until they are made constant.
    I don't know the cause of that.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.