Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Trying to makes sense of unnamed namespace and extern use

c++

  • Please log in to reply
1 reply to this topic

#1 sonar87

sonar87

    CC Newcomer

  • Member
  • PipPip
  • 18 posts

Posted 25 January 2017 - 01:17 PM

I have a program with with two .cpp files (one being main, the other with a couple functions) and two .hpp files so far (one with function definitions for the one .cpp file, the other with some includes like iostream that all the .cpp files use).

 

The following is at the start of my main file

namespace
{
  extern const int max = 10;
}

And then I have the same but not assigning it a value, just ; after the word max, in the .hpp file with the includes. This is included in main and in the .cpp file for the function definitions. But I'm getting an "undefined reference to `(anonymous namespace)::max' error when one of my functions tries to use it. Is this the wrong way to use the extern thing?

 


  • 0

#2 AceInfinity

AceInfinity

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 313 posts
  • Programming Language:C, C++, C#, Perl, Visual Basic .NET, Assembly, VBScript, Others

Posted 29 January 2017 - 12:52 PM

extern is for the declaration only, not the assignment. Why do you have an unnamed namespace?


Edited by AceInfinity, 29 January 2017 - 12:53 PM.

  • 0

Microsoft MVP (2012) - .NET Programming | ®Crestron DMC-T Certified Programmer
Posted Image