strange problem with global string object

This might be something simple, but I just didn't get what.
GCC 4.5 outputs "error: ā€˜s’ does not name a type" for this code (line 4):

1
2
3
4
5
6
7
8
#include <string>

std::string s;
s = "IO";

int main(){
  return 0;
}
Shouldn't you be doing std::string s; s = "IO"; inside main function??
I can't say why the compiler is talking about types, but the thing is that you can't do assignments outside functions in C/C++. std::string s = "IO"; should be fine though. That's initialization - a slightly different thing.
the thing is that you can't do assignments outside functions in C/C++

I missed that... So it's just a function code outside of any function and object...
I can't say why the compiler is talking about types,


It's because the compiler thinks this must be a declaration, but it doesn't know what an object of type s is.
Topic archived. No new replies allowed.