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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:44:16+00:00 2026-06-06T02:44:16+00:00

I have two files called file_utils.h and file_utils.cpp which contain some methods and variables

  • 0

I have two files called file_utils.h and file_utils.cpp which contain some methods and variables which are used by different classes. Here’s an example of how it looks:

file_utils.h:

namespace my_namespace
{
extern Foo* foo;
extern Bar* bar;

void my_function(Blah* blah);
}

file_utils.cpp

#include "file_utils.h"

void my_namespace::my_function(Blah* blah)
{
    foo = 0;    // undefined reference to my_namespace::foo
    bar = 0;    // undefined reference to my_namespace::bar
    //...
}

some_class.cpp

#include "file_utils.h"

some_function()
{
    my_namespace::my_function(blah);
    this->foo = *my_namespace::foo; // will that work ok?
}

So the errors are in the comments. If I remove the extern keyword I get multiple definition of my_namespace::foo error. What is the problem? Is that even a good idea from design standpoint or should I try to use a class with static members and methods instead?

  • 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-06T02:44:17+00:00Added an answer on June 6, 2026 at 2:44 am

    The problem is that you only declared but not defined the variables.

    You need to provide a definition in a single implementation file:

    file_utils.cpp

    #include "file_utils.h"
    
    //definition:
    namespace my_namespace
    {
       Foo* foo;
       Bar* bar;
    }
    
    //alternatively, to keep the same formatting you have
    //Foo* my_namespace::foo;
    //Bar* my_namespace::bar;
    
    void my_namespace::my_function(Blah* blah)
    {
        foo = 0;
        bar = 0;
        //...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two files which both contain a list of words. Is there an
The situation is I have two xslt files: one is called from my ASP.NET
Hallo all, I have two files, one called part1.txt and another one called part2.txt,
I have two .php files which show all contacts in a database, then allows
I have two different PHP files that both write to the same file. Each
I have two files: osm.h and osm.cpp I tried to create static lib from
We have a project here which is split into two. One for Internal and
I have two different batch files at two different path. When I call A.bat
I have two files: // event_test_delete.cpp #include <event.h> int main() { event_base* ev; ev
I have two java files. In the first I have my activity which starts

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.