Search code examples
c++stringstream

"Declaration has no storage class or type specifier" error when using stringstream


#include <iostream>
#include <string> 
#include <sstream> 
#include <string>

int coins = 0;
std::stringstream ss;
ss << 100 << ' ' << 200;

When I hover over ss I get the error "declaration has no storage class or type specifier" and when I hover over << I get the error expected a ;".


Solution

  • C++ does not allow executable statements outside functions.

    The first two lines are declarations; they are allowed, although I doubt that you made them global on purpose. The last line, however, must be placed inside a function, e.g. main:

    int main() {
        int coins = 0;
        std::stringstream ss;
        ss << 100 << ' ' << 200;
    }