CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Threaded View

  1. #4
    Join Date
    May 2009
    Posts
    2,413

    Re: Confused with linking header file in C++

    Quote Originally Posted by ritika_sharma82 View Post
    But now, I wanna call a "const int" data declared in header1.h from main.cpp.
    I tried including header1.h in main.cpp, it shows the error. It says redefined.
    Strange. I should say that the "const int" isn't initialized.

    A const declaration should look like this,

    const int someData = 43;

    and you should be able to include it in many compilation units (like main.cpp and function.cpp) you then link.

    It's only if someData isn't const you should need to declare it extern. In that case you make sure that this is in one compilation unit only,

    int someData;

    and that all compilation units who want to use someData incude this,

    extern int someData;
    Last edited by nuzzle; February 9th, 2011 at 03:43 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured