0

I'm new to c++, and therefore hope my question isn't too stupid, but it is quite blocking, and I've looked around and failed to find an answer...

I'm blocked on the definition of a variable with its value inside parentheses, like in this simple example :

int main()
{
    int x(0);
}

It doesn't work when I try it, nor does this :

    int x=0;

Xcode tells me the variable is "unused", and I have to add a line like this to make it work :

    int x;
    x=0;

I'd ignored this minor problem after trying in vain to find a solution. But it appears to be more crippling than I thought at first, for example when trying to reference a variable to another, since neither:

int& y(x);

nor:

int& y;
y = x;

works. I've tried several combinations but I guess there is something here I really don't get, even though it's really basic; so, I would be very grateful if someone could help !

Thank you very much !

  • When you say it doesn't work, do you mean you get a warning about an unused variable? – Useless Jul 22 '13 at 13:58
  • You'll get a *warning* if you don't use a variable, since that usually means you've made a mistake. But a warning doesn't mean that it "doesn't work". Why are you declaring it at all if you don't want to do anything with it? – Mike Seymour Jul 22 '13 at 13:59
  • Your assignments are working fine; it's just a compiler warning, which can be worked around: http://stackoverflow.com/questions/5451123/how-can-i-get-rid-of-an-unused-variable-warning-in-xcode – trojanfoe Jul 22 '13 at 13:59
  • 1
    Yep, it's probably just a warning. However, `int& y; y = x;` wont work. A reference must be initialized when declared. – Xaqq Jul 22 '13 at 14:05
  • As a side-note: Beware of the "most vexing parse" when declaring/defining variables this way. – arne Jul 22 '13 at 14:06
  • Thanks a lot for your quick answers ! It is indeed only a warning, and it does work, so now I can try doing what I wanted to do with it. – user2606858 Jul 22 '13 at 14:08

0 Answers0