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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:15:19+00:00 2026-06-07T20:15:19+00:00

I am now building a C++ DLL library. Today I have met a confusing

  • 0

I am now building a C++ DLL library. Today I have met a confusing problem: in this library I can define class but not functions. To be more specific, I give the following codes to illustrate my problem:

namespace fundamental
{

    class Tree
    {
    public:
        Tree() {};
        ~Tree() {};
        int x;
    };

     /*int anyfunction()
    {
        return 1;
    }*/

}

The above definition is in the header file, and this file will be invoked by other files. My problem is that if I commented the function part (int anyfunction()) everything was fine, but if I added this function, I would get the following errors:

page_analysis.obj : error LNK2005: "int __cdecl fundamental::anyfunction(void)" (?anyfunction@fundamental@@YAHXZ) already defined in geo_box.obj
1>pa_region_properties.obj : error LNK2005: "int __cdecl fundamental::anyfunction(void)" (?anyfunction@fundamental@@YAHXZ) already defined in geo_box.obj

My question is why I will get LNK2005 error only for functions but not for classes. Any ideas?

  • 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-07T20:15:20+00:00Added an answer on June 7, 2026 at 8:15 pm

    If you define something in a header file, then that definition will be duplicated in any translation unit (roughly speaking, every source file) that includes that header. Sometimes, multiple definitions are an error.

    Classes can be defined in multiple translation units, as long as the definitions are identical; indeed, they must be defined in any translation unit that uses them.

    Functions usually can’t, but you can allow it by declaring it inline:

    inline int anyfunction() {return 1;}
    

    or you could move the definition to a single source file, and only declare it in the header:

    // header
    namespace fundamental {
        int anyfunction();
    }
    
    // source file
    int fundamental::anyfunction() {return 1;}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

With reference to this programming game I am currently building. I have a Class
I have a self hosted TCP based WCF service. I am now building a
I have an MVC3/EF4.1 app that I'm building which now works fine on my
i am building a php based website but now i need to give user
I'm building a little Android game using libgdx. For now I have a copy
I am building a class library and using its default namespace as System. There
i have a problem with global variables in a c++ shared library project. my
I am building an application using NHibernate. Because I could not add the DLL's
I have an issue with building silverlight project on appharbor (some dll are missed).
I have an old DLL file which was built with VC++ 6. Now I

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.