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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:56:17+00:00 2026-06-04T10:56:17+00:00

If two C++ files have different definitions of classes with the same name, then

  • 0

If two C++ files have different definitions of classes with the same name, then when they are compiled and linked, something is thrown out even without a warning. For example,

// a.cc
class Student {
public:
    std::string foo() { return "A"; }
};
void foo_a()
{
    Student stu;
    std::cout << stu.foo() << std::endl;
}

// b.cc
class Student {
public:
    std::string foo() { return "B"; }
};
void foo_b()
{
    Student stu;
    std::cout << stu.foo() << std::endl;
}

When compiled and linked together using g++, both will output “A” (if a.cc precedes b.cc in the command line order).

A similar topic is here. I see namespace will solve this problem but I don’t know why the linker doesn’t even shoot a warning. And if one definition of the class has extra function that isn’t defined in another, say if b.cc is updated as:

// b.cc
class Student {
public:
    std::string foo() { return "B"; }
    std::string bar() { return "K"; }
};
void foo_b()
{
    Student stu;
    std::cout << stu.foo() << stu.bar() << std::endl;
}

Then stu.bar() works well. Thanks to anyone who can tell me how the compiler and linker work in such situation.

As an extra question, if classes are defined in header files, should they always be wrapped with unnamed namespace to avoid such situation? Is there any side effects?

  • 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-04T10:56:19+00:00Added an answer on June 4, 2026 at 10:56 am

    This is a violation of the one definition rule (C++03, 3.2/5 “One definition rule”), which says (among other things):

    There can be more than one definition of a class type (clause 9), …
    in a program provided that each definition appears in a different
    translation unit, and provided the definitions satisfy the following
    requirements. Given such an entity named D defined in more than one
    translation unit, then

    • each definition of D shall consist of the same sequence of tokens;

    If you violate the one definition rule, the behavior is undefined (which means that strange things can happen).

    The linker sees multiple definitions of Student::foo() – one in a’s object file and one in b’s. However it doesn’t complain about this; it just selects one of the two (as it happens, the first one it comes across). This ‘soft’ handling of duplicate functions apparently happens only for inline functions. For non-inline functions, the linker will complain about multiple definitions and will refuse to produce an executable (there may be options that relax this restriction). Both GNU ld and MSVC’s linker behave this way.

    The behavior makes some sense; inline functions need to be available in every translation unit they’re used in. And in the general case they need to have non-inline versions available (in case the call isn’t inlined or if the function’s address is taken). inline is really just a free pass around the one-definition rule – but for it to work, all the inline definitions need to be the same.

    When I look at dumps of the object files, I don’t see anything obvious that explains to me how the linker knows that one function is permitted to have multiple definitions and others aren’t, but I’m sure there’s some flag or record which does just that. Unfortunately, I find that the workings of the linker and object file details aren’t particularly well documented, so the precise mechanism will probably remain a mystery to me.

    As for your second question:

    As an extra question, if classes are defined in header files, should
    they always be wrapped with unnamed namespace to avoid such situation?
    Is there any side effects?

    You almost certainly don’t want to do this each class would be a distinct type in each translation unit, so technically instances of the class they couldn’t be passed from one translation unit to another (by pointer, reference or copying). Also, you’d end up with multiple instances of any static members. That probably wouldn’t work well.

    Put them in different, named namespaces.

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

Sidebar

Related Questions

I have a small problem with Smarty... I have two different template files in
What does this mean. Is it because I have two different .DBML files that
I have two different files, Foo1.exe and Foo2.dll. When I try to use an
I have two different batch files at two different path. When I call A.bat
I have one HTML files on one server and two files on different server,
I have two different modules that need access to a single file (One will
I have a Visual Studio 2010 solution and two files have mysteriously appeared in
Say I have two files where there is one number per line File 1
I have two files which both contain a list of words. Is there an
I have two files, A is a subset of B. Both A and B

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.