Debugging error c2059

P

pauldepstein

I am adding to a large code base, and there is far too much code to
paste it in its entirety. Here is some code that I added:

struct MyStruct
{
MyStruct(const std::string& SomeString, const std::string&
AnotherString)
{

}

};

MyStruct("SomeWord", "AnotherWord");


Of course, the above code is not particularly useful. I was following
the technique of making it as simple as possible to try and see what
was triggering the error.

The compiler complains about MyStruct("SomeWord", "AnotherWord");
Referring to the line number where the above line occurs, it says
error C2059: syntax error : 'string'
Can anyone explain the probable cause of the error?

Thank you all very much for your help.

Paul Epstein
 
R

red floyd

I am adding to a large code base, and there is far too much code to
paste it in its entirety. Here is some code that I added:

struct MyStruct
{
MyStruct(const std::string& SomeString, const std::string&
AnotherString)
{

}

};

MyStruct("SomeWord", "AnotherWord");


Of course, the above code is not particularly useful. I was following
the technique of making it as simple as possible to try and see what
was triggering the error.

The compiler complains about MyStruct("SomeWord", "AnotherWord");
Referring to the line number where the above line occurs, it says
error C2059: syntax error : 'string'
Can anyone explain the probable cause of the error?

Thank you all very much for your help.

Yes. You didn't include <string>


Correct code:

#include <string>
struct MyStruct
{
MyStruct(const std::string& SomeString, const std::string&
AnotherString)
{

}

};

MyStruct("SomeWord", "AnotherWord");
 
P

pauldepstein

Yes.  You didn't include <string>

Correct code:

#include <string>
struct MyStruct
{
    MyStruct(const std::string& SomeString, const std::string&
AnotherString)
    {

    }

};

MyStruct("SomeWord", "AnotherWord");- Hide quoted text -

- Show quoted text -

Good guess but in fact I did use #include <string>
Thanks a lot for trying to help though.

Any more guesses?

Paul Epstein
 
P

pauldepstein

Yes.  You didn't include <string>

Correct code:

#include <string>
struct MyStruct
{
    MyStruct(const std::string& SomeString, const std::string&
AnotherString)
    {

    }

};

MyStruct("SomeWord", "AnotherWord");- Hide quoted text -

- Show quoted text -

Correct code is actually:
struct MyStruct
{
MyStruct(const std::string& SomeString, const std::string&
AnotherString)
{


}



};


MyStruct YouHaveToGiveTheMyStructVariableAName("SomeWord",
"AnotherWord");
 
I

Ian Collins

Good guess but in fact I did use #include <string>
Thanks a lot for trying to help though.

Any more guesses?
Guesses are all you're going to get. The posted code (with <string>) is
fine.

Try and form a complete example that gives the reported compiler error
and post that.
 
P

pauldepstein

 The posted code (with said:


Ian,

Are you sure that it's ok not to assign a name to the
MyStruct("SomeWord", "AnotherWord") variable?
When I replaced that line by MyStruct CallItSomething("SomeWord",
"AnotherWord");

the bug appeared to be removed.

Paul Epstein
 
I

Ian Collins

Ian,

Are you sure that it's ok not to assign a name to the
MyStruct("SomeWord", "AnotherWord") variable?
When I replaced that line by MyStruct CallItSomething("SomeWord",
"AnotherWord");
No, I think we all overlooked that omission!
 
J

James Kanze

The posted code (with <string>) is fine.
Are you sure that it's ok not to assign a name to the
MyStruct("SomeWord", "AnotherWord") variable?

Sure, as long as the expression is in a function. (You can't
use an expression statement at global scope.) Put a function
definitioin around the last line, and it compiles fine.

Of course, the real question is what you want that line to do.
You said in your first posting that the code didn't do anything
useful, it was just an experiment. Which is definitely the case
if you don't name the variable---you construct a temporary of
type MyStruct, then immediately destroy it. This can be useful
if the temporary is part of a larger expression (and is used in
the larger expression), but it's very exceptional for it to be
useful as a complete expression (except maybe to test the
constructor of a stateless object in a unit test).
When I replaced that line by MyStruct
CallItSomething("SomeWord", "AnotherWord");
the bug appeared to be removed.

If you provide a name, the line is no longer an expression
statement, but a definition. Definitions can appear both at
namespace scope and at local scope. And the variable you create
with them will not be destructed until it goes out of scope.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top