Michael
Michael

Reputation: 22947

missing type specifier - int assumed. Note: C++ does not support default-int

I have a cpp file that contains the following:

char const* types[] = { "char", "short", "int", "long", "float", "double", "void"};
std::set<std::string> ReservedWords;
ReservedWords.insert(std::begin(types),std::end(types));

this gives an error missing type specifier - int assumed. Note: C++ does not support default-int

I have read that you can't write statements in a global scope, is this the case here ?

I don't completely understand the rule, and would like to know where its best to put this code ? (header file, inside a function etc...)

Upvotes: 1

Views: 22568

Answers (3)

justcoder
justcoder

Reputation: 76

This error appears when you don't include the correct files. Make sure to add
#include <string.h>

And yes, you must remove this line from global scope:

ReservedWords.insert(std::begin(types),std::end(types));

Try putting it in the main function.

Upvotes: 1

CB Bailey
CB Bailey

Reputation: 791849

char const* types[] = { "char", "short", "int", "long", "float", "double", "void"};
std::set<std::string> ReservedWords;
ReservedWords.insert(std::begin(types),std::end(types));

The first two lines here are declarations because they declare variables (types and ReservedWords). The third line is not a declaration, it's just an expression statement so it's not legal for it to appear outside a function.

You could do something like:

char const* types[] = { "char", "short", "int", "long", "float", "double", "void"};
std::set<std::string> MakeReservedWords() {
    std::set<std::string> tmp;
    tmp.insert(std::begin(types), std::end(types));
    return tmp;
}

std::set<std::string> ReservedWords(MakeReservedWords());

Given that you are using C++11 you should be able to do this:

std::set<std::string> ReservedWords { "char", "short", "int", "long", "float", "double", "void"};

If your compiler doesn't support this part of C++11 you will have to settle for something like this (as suggested by @juanchopanza):

char const* types[] = { "char", "short", "int", "long", "float", "double", "void"};
std::set<std::string> ReservedWords(std::begin(types), std::end(types));

Upvotes: 2

Luchian Grigore
Luchian Grigore

Reputation: 258608

First, not that std::begin and std::end are C++11, so are you sure you have a compatible compiler and that you're compiling with C++11 support?

I don't believe this is the error though. Are you including:

#include <string>
#include <set>
#include <iterator>

?

Upvotes: 2

Related Questions