Variable declaration in C++ within namespaces

variable declaration in c rules
variable declaration and definition in c
variable initialization in c
variable declaration in c++
types of variables in c
syntax for variable declaration in c++
constant in c language
global variable in c

In a library I am working with some variables are declared like that:

char &ns::x = y;

However, if I do it that way I get the following error: error: no member named 'x' in namespace 'ns'

If I rewrite it, it works:

namespace ns {
    char &x = y;
}

What exactly is the difference? And why is it working within the library?

If you’re right and the code from the library is exactly as written, then this implies that elsewhere in this library, you’ll find the following declaration:

namespace ns {
    extern char& x;
}

In other words, x must have already been declared (and not defined!) inside ns.

What is a Declaration?, programming language, all variables must be declared with a specific data type before they can be assigned a value. There are two main types of variables in C: numeric variables that hold only numbers or values, and string variables that hold text, from one to several characters long. There are several different types of numeric variables, depending on the size and precision of the number. Before you use a variable, it must be declared.

The first declaration

char &ns::x = y;

assumes that the name x is already declared in the namespace ns. However this assumption is wrong (in the provided code snippet there is no previous declaration of the variable. Possibly the code snippet is not complete.).

The code snippet can works provided that the variable x is already declared (without its definition) in the namespace ns.

For example

#include <iostream>

namespace ns
{
    extern char &x;
}

char y;
char & ns::x = y;

int main() {
    return 0;
}

In this code snippet

namespace ns {
    char &x = y;
}

there is defined a reference that is initialized by the object y.

C variable with examples, data_type variable_name; Example: int x, y, z; char flat, ch; To declare more than one variable of the specified type, use a comma-separated list. Declaration of Variable. Declaration of variable in c can be done using following syntax: data_type variable_name; or data_type variable1, variable2,…,variablen; where data_type is any valid c data type and variable_name is any valid identifier. For example,

The Variable declaration using namespace:

#include <iostream> 
using namespace std; 

// Variable created inside namespace 
namespace first 
{ 
  int val = 500; 
} 
// Global variable 
int val = 100; 
int main() 
{ 
// Local variable 
   int val = 200; 
// These variables can be accessed from 
// outside the namespace using the scope 
// operator :: 
   cout << first::val << '\n';  
   return 0; 
} 

C - Variables, data_type variable_name = value; Example: int x = 50, y = 30; char flag = 'x', ch='l'; Declaring & initializing C Variable First, you need to declare a variable in the C program before to use it. Memory space is not created for a variable during a declaration. It happens only on the variable definitions.

Declaring Variables in C, C - Variables - A variable is nothing but a name given to a storage area that our programs can manipulate. Each variable in C has a specific type, which  C Variables Declaration, Initialization & Rules ← Previous Next → A variable is the name of a memory block/location which declares by the user, and it is used to store value. The name of variable can not be changed once it is declared, while the value stored in variables can be changed at any point of the entire program.

C Programming/Variables, Rules for naming C variable: Variable name must begin with letter or underscore. Variables are case sensitive; They can be constructed with digits, letters. No  Grouping variable declarations at the top of the block is a legacy likely due to limitations of old, primitive C compilers. All modern languages recommend and sometimes even enforce the declaration of local variables at the latest point: where they're first initialized. Because this gets rid of the risk of using a random value by mistake.

Variables and Keywords in C, Valerie Variable is a numeric variable. She loves to hold numbers — any number​; it doesn't matter. Whenever she sees an equal sign, she takes to a value and  Variables should be declared in the C program before to use. Memory space is not allocated for a variable while declaration. It happens only on variable definition. Variable initialization means assigning a value to the variable.

Comments
  • Are you sure that ns in that library is a namespace, and not a class name?
  • So there definitely has to be a declaration of x anywhere? Because I did not find one in any of the cpp and h files.
  • @ElasticLamb Yes the variable (without its definition, it has a referenced type) shall be already declared.
  • @VladfromMoscow Yes — crossed wires.
  • This doesn't answer OP's question.