8

Challenge:

I have this code that fails to compile. Can you figure out what's wrong? It caused headache to me once.

// header
namespace values {
  extern std::string address;
  extern int port;
}

// .cpp file
std::string  ::values::address = "192.0.0.1";
int          ::values::port    = 12;

It looks correct on the first sight. How many and which are the errors!?

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
  • Do you know the answer already? It sounds like it ;) – Mathias Soeken Mar 01 '10 at 19:44
  • @Nikolai, it's more challenging to try without xD – Johannes Schaub - litb Mar 01 '10 at 19:44
  • Oh, you're just teasing us :) – Nikolai Fetissov Mar 01 '10 at 19:45
  • 3
    This question is very similar with this one (http://stackoverflow.com/questions/1642028/what-is-the-name-of-this-operator). Same problem with formatting. Should I mark it for closing as duplicate? :) – Kirill V. Lyadvinsky Mar 01 '10 at 19:56
  • 1
    @Kirill: No, they're different. In particular, note the difference in how the compiler treats the two lines in this case. – David Thornley Mar 01 '10 at 20:02
  • It's quite common in C++ – Kirill V. Lyadvinsky Mar 01 '10 at 20:10
  • @Kirill, i think it will surprise most. When i previously asked it in the IRC channel, most didn't know what was going on. So i decided i put it into a question. This is a semantic problem, whereas `-->` is purely syntactic. – Johannes Schaub - litb Mar 01 '10 at 20:22
  • http://stackoverflow.com/questions/2357662/error-in-borland-c-using-graphics-h-closed – John Dibling Mar 01 '10 at 20:35
  • 3
    The point being if this question were asked by someone with 5 rep and a name nobody recognized, it would have been hated in to oblivion in seconds. – John Dibling Mar 01 '10 at 20:35
  • 1
    @John, i'm confused. How is it different from other questions? The only difference is that i already knew the answer. These questions are encouraged. I'm biased, but surely there are worlds between this question and the one you linked to. – Johannes Schaub - litb Mar 01 '10 at 21:37
  • 1
    @John: I think there is a world of difference. This question enlightened me and I learnt something (an arcane point of C++) whereas the question you linked to is just useless. Furthermore, here we have elements to answer the question (the fragment of code). I support this kind of challenge because they bring knowledge into the fray, and unless I am mistaken spreading knowledge is the very goal of SO. – Matthieu M. Mar 02 '10 at 08:20
  • 1
    Don't get me wrong; I'm not being critical of litb's post. My commentary was more aimed at the nature of SO in general. In particular how in this question litb did not include the compiler diagnostics, which would have been met with much vitriol typically but was welcomed as a challenge here. And it bothers me when people downvote questions for being too elementary, even though SO is intended to be a one-stop shop for google searches -- but that is another rant... – John Dibling Mar 02 '10 at 14:49
  • In any case I welcome and an grateful for litb's contribution to the site, including this post. – John Dibling Mar 02 '10 at 14:50

2 Answers2

8

One error:

std::string values::address = "192.0.0.1"; 

is the proper form, otherwise the parse is

std::string::values::address = "192.0.0.1"; 

and there is no member "values" with a member "address" inside "string"...

it will work for builtin types, as they cannot ever contain members.. so int::values is an unambigous parse, int ::values, because the prior doesn't make sense.

std::string (::values::address) = "192.0.0.1"; 

works too. Note that if you typedef int sometype; that you'd have the same problem using sometype as you do with string above, but not with "int".

3

I'm late to the game, but I would have preferred to write the .cpp file as:

// .cpp file
namespace values {
  std::string  address = "192.0.0.1";
  int          port    = 12;
}

Of course that doesn't solve the problem you had with the friend declaration.

Dan
  • 5,929
  • 6
  • 42
  • 52
  • I suspect my initial example with the "address" stuff was lame :) The friend example is much better. I agree with you that this way of definition is the better one :) – Johannes Schaub - litb Mar 01 '10 at 21:35