views:

62

answers:

5
#include <iostream>
#include <string>

class c1
{
 public:
  static std::string m1;
  static unsigned int m2;
};

//std::string c1::m1 = std::string;
unsigned int c1::m2 = 0;

void main()
{
 c1 a;
 //std::cout<<a.m1<<std::endl;
 std::cout<<a.m2<<std::endl;
}

In this program enabling the two remarked lines causes an error on the first.

error C2275: 'std::string' : illegal use of this type as an expression

What am I doing wrong?

+1  A: 

The error is due to the right-hand use of std::string on that line - you're trying to initialise the value of m1 to the type std::string.

You should find that a line like std::string c1::m1 = "Wee - a string!"; will work.

Chris
Beat me by 18 seconds :)
ohadsc
+2  A: 
std::string c1::m1 = std::string;

should be something like

std::string c1::m1 = "";
ohadsc
+2  A: 

The error says it all, you are using the type std::string as the value to be assigned.

To fix this you can do:

std::string c1::m1 = std::string();
                                ^^

or just

std::string c1::m1;
codaddict
+4  A: 

Because "std::string" is a type, not a value. Here is an example that might make this more obvious:

#include <iostream>
#include <string>

class c1
{
public:
  static unsigned int m2;
};

unsigned int c1::m2 = int; // error: int is a type, not a value

void main()
{
  c1 a;
  std::cout<<a.m2<<std::endl;
}
Merlyn Morgan-Graham
+1: I like very much your obvious comparison!
Didier Trosset
A: 

Thanks. In other contexts this sort of syntax is good. I guess that the devil is in the detail. Pleasantly easily fixed. I want it to be the default string so I will use the () solution.

alan2here
then accept the answer. :)
Donotalo
Thanks. I have done this now, im new here. I would vote up a few of the answers as well but im not alloud yet.
alan2here