开发者

stringstream::in syntax error

I am trying to use a stringstream to do something like this (I simplified my co开发者_StackOverflow中文版de to pinpoint the error):

Token.h

#include <stdio.h>
#include <vector>
#include <sstream>

using namespace std;

class Token {
public:
   static stringstream ss (stringstream::in | stringstream::out);
   static void set_input_string(std::string str);
};

Token.cpp

#include "Token.h"

void Token::set_input_string(std::string str)
{
   ss << str;
}

When I try to compile i get: error C2061: syntax error : identifier 'in' in Token.h on line static stringstream ss (stringstream::in | stringstream::out); and visual studios 2010 underlines in red stringstream::in |


You cannot initialize non-integral values in the class definition. You should have:

// Token.h
#include <cstdio> // thisis the C++ header
#include <vector>
#include <sstream>

// don't do this, especially not in a header file - function-scope at best
// using namespace std;

class Token {
public:
    static std::stringstream ss; // declare

    // probably want this parameter to be a const-reference
    static void set_input_string(std::string str);
};

And:

// Token.cpp
#include "Token.h"

// define
std::stringstream Token::ss(std::stringstream::in | std::stringstream::out); 

void Token::set_input_string(std::string str)
{
   ss << str;
}


You can't declare and define a member variable at the same time. The definition of ss should be outside of the class declaration:

class Token {
public:
   static stringstream ss; // declaration
   static void set_input_string(std::string str);
};

stringstream Token::ss (stringstream::in | stringstream::out); // definition in your cpp file
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜