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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:02:07+00:00 2026-05-31T16:02:07+00:00

Currently I am working on a c framework and I want to embed a

  • 0

Currently I am working on a c framework and I want to embed a c++ package into it. However, there are lots of naming conflicts occurs. So I decided to to add a namespace to the C++ source. Now the question is should I move the #include “header.h” within namespace { } block? I just spent some time to figure out a bug which was resulted from the following codes.

Original C++ source

In a.h

#include <unistd.h>
struct File 
{
     void func(int fd);
}; 

In a.cpp

#include "a.h"
void File::func(int fd)
{
    ::close( fd );
}

And I added the namespace like this

New a.h

namespace MyAddedNameSpace 
{ 
    #include <unistd.h>
    struct File 
    {
        void func(int fd);
    }; 
}

New a.cpp

#include "a.h"
namespace MyAddedNameSpace 
{
    void File::func(int fd)
    {
        ::close( fd );
    }
}

And compiler complains that ::close() has not been declared.

The reason Why I put the #include directive inside namespace block is because the c++ package I imported also use #ifndef flag to include header files as follows. So I think the easy way is to put all codes within namespace block {}

#ifndef 
    #include <header1.h> 
    #include <header2.h>
    ...
#else 
    #include <header3.h>
    #include <header4.h>
    ...
#endif

Now I solved this problem by adding the extra line in cpp file

#include <unistd.h>  //new added line 
#include "a.h"
namespace MyNameSpace 
{
    void File::func(int fd)
    {
        ::close( fd );
    }
}

But I am not satisfied with this solution since unistd.h header has already been included in a.h, but inside the namespace MyAddedNameSpace, or should I added the prefix MyNameSpace to all the function calls where the compiler complain no such function declared ?

Thanks for the reply.

  • 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-31T16:02:09+00:00Added an answer on May 31, 2026 at 4:02 pm

    Usually it is enough to place the directive using namespace in the .cpp file.
    Like that:

    using namespace MyAddedNameSpace;
    
        void File::func(int fd)
        {
            close( fd );
        }
    

    Hope it helps..

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

Sidebar

Related Questions

I'm working on a project that is currently using the Zend Framework 1.7.6, however
ASP.net C# 3.5 Framework, working in Visual Studio 2008 currently. What I want is
I am currently working with some users that do not have the .NET Framework
I'm currently working on a project where I use Zend Framework with Propel. I'm
I am currently working on an web application that uses ASP.NET 2.0 framework. I
Hi I'm currently working on some php - zend framework project on my osx
I'm currently working on an open source bundle for Symfony2, and really want it
Currently I am working on web project which uses JSP/Servlet and struts framework. We
So I'm writing a test framework for a large website, and am currently working
I'm currently looking into the spring-security framework - great stuff so far, pretty impressed.

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.