a string problem

Hello, i have a litlle problem. Here compiler gcc says "error ... expected constructor, destuctor or type conversion before += ...".
Why is that? Maybe because it runs out of a function?

1
2
3
4
5
6
7
#include <string>
#define EXODOS "out.txt"
using namespace std;
std::string output_file=EXODOS;
output_file+="jjjjj";
       or
output_file.append("jjj");


You can't have code that runs outside of a function unless it is constructing a global variable.

1
2
std::string output_file=EXODOS;  // this works because you're constructing output_file
output_file += "jjjjj";  // this fails because you're not constructing anything (output_file is already constructed) 
Yes but output_file take the "out.txt" basic string, then with output_file += "jjjjj";, the output_file string must be "out.txtjjjj"
Why we have ...
1
2
string name ("John");
name += " K. ";         // c-string 


This way must output_file implements with "jjjj".
Last edited on
yes, but you can't do that outside of a function.

The closest you can do is this:

1
2
3
4
5
6
7
string name = string("John") + " K. ";  // all done when constructing the object

// or this:
string name = "John" " K. ";

// or this:
string name = "John K. ";



Executable code must be in a function. You must call it. The only things that can be outside of functions are declarations and the like.
Ok, i just understud this.
Many thanh's.
Jim
Topic archived. No new replies allowed.