w4etwetewtwet
w4etwetewtwet

Reputation: 1350

Why does this not work when split onto 2 lines?

This line:

std::auto_ptr<Ogre::Root> lRoot (new Ogre::Root(lConfigFileName, lPluginsFileName, lLogFileName));

works fine. However when I do this it doesn't:

std::auto_ptr<Ogre::Root> lRoot;
lRoot (new Ogre::Root(lConfigFileName, lPluginsFileName, lLogFileName));

And it reports: error: no match for call to ‘(std::auto_ptr<Ogre::Root>) (Ogre::Root*)’ As far as my limited understanding goes shouldn’t these do the same thing? Or am I missing something major?

Upvotes: 0

Views: 88

Answers (3)

The first statement is a declaration of variable lRoot with initialisation (using the syntax of initialiser in parentheses).

The second is a declaration of default-initialised variable lRoot, followed by invoking operator() on the variable. (Note that std::auto_ptr doesn't define such an operator).

To split this into two lines (still as one statement), you can just insert a line break anywhere whitespace is allowed:

std::auto_ptr<Ogre::Root> lRoot(
  new Ogre::Root(lConfigFileName, lPluginsFileName, lLogFileName));

To actually split this into a declaration and assignment (note that when split, it cannot be an initialisation), you could do this:

std::auto_ptr<Ogre::Root> lRoot;
lRoot.reset(new Ogre::Root(lConfigFileName, lPluginsFileName, lLogFileName));

Upvotes: 6

Jon
Jon

Reputation: 437914

There is very little relation between the two snippets.

The first one declares and initializes the variable lRoot, nothing to see there.

The second snippet declares and default-initializes lRoot in the first line, but then it goes on to invoke operator() on lRoot with an argument of type Ogre::root*. Since std::auto_ptr does not define an operator(), the compiler produces the given error.

Upvotes: 7

Mike Dinescu
Mike Dinescu

Reputation: 55760

They are not the same thing!

You are not just splitting the statement into two lines. The two are two different statements..

You can split the first statement onto two lines like this:

std::auto_ptr<Ogre::Root> lRoot
    (new Ogre::Root(lConfigFileName, lPluginsFileName, lLogFileName));

And it would compile just fine because the multiple white spaces are ignored.

Upvotes: 3

Related Questions