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 7912173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:31:50+00:00 2026-06-03T13:31:50+00:00

How to use namespaces in C++ where it is accessible in different header files.

  • 0

How to use namespaces in C++ where it is accessible in different header files. Lets say I have this below:

// namespaces
#include <iostream>
using namespace std;

namespace first
{
  int var = 5;
}

namespace second
{
  double var = 3.1416;
}

int main () {
  cout << first::var << endl;
  cout << second::var << endl;
  return 0;
}

and I want t use var variable from first namespace in another class… that is defined and implemented in another .h and .cpp file?

//server.h
    #ifndef SERVER_H
    #define SERVER_H
    class server{
       server();
    //blah
    };
    #endif SERVER_H

//server.cpp
server::server()
{
    first::var = 3;
}

is this possible to do it like this? When I try I get an error that says that my namespace is not defined. And if i put using namespace first in the .h or .cpp it says there is no namespace called first…

  • 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-03T13:31:51+00:00Added an answer on June 3, 2026 at 1:31 pm

    Besides having the namespace in a header, you need to make the variable extern:

    //header.h
    namespace first
    {
      extern int var;
    }
    
    
    //implementation.cpp
    #include "header.h"
    namespace first
    {
       int var = 5;
    }
    

    If the variable is not extern, a symbol will be generated wherever the header is included, and you’ll get linker errors.

    If you don’t want the extra header, you can just declare the variable as extern in the same namespace where you want to use it:

    //server.cpp
    namespace first
    {
       extern int var;
    }
    server::server()
    {
        first::var = 3;
    }
    

    Note some answers might claim that you should make the variable static. This is wrong, although it will compile, as then the variable won’t act as a global. A copy of it will be created for every translation unit.

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

Sidebar

Related Questions

I get this error when I try to use namespaces. I have namespace App;
I'm trying to compile such code: #include <iostream> using namespace std; class CPosition {
I have a some class, it include Smarty, but my class use namespace test,
I have a bunch of device files which are accessible by using \\.\name_of_file as
Is there any way to temporary use a namespace ? I'm using a library
In my C++ code I don't use the declarations using namespace std; or using
Can we use namespaces like in below snippet? The code compiles in both gcc
I'm trying to use namespaces. I want to extend a class inside a different
I'd like to use namespaces as one would in javascript by using the keyword
I have ClassLibrary project in C# and all my 'private classes' (under different namespace)

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.