0

I have a single file called main.cpp where I am trying to declare an unordered_map as shown below.

std::unordered_map<std::string, std::set<int>> firstSets;

I then try to insert a new (key, value) pair into the map as follows.

std::string decl = "decl";
std::set<int> declFirstSet = {VOID_TOK, INT_TOK, FLOAT_TOK, BOOL_TOK};
firstSets[decl] = declFirstSet;

When I do this I get the following compiler error.

C++ requires a type specifier for all declarations

firstSets[decl] = declFirstSet;

size of array has non-integer type 'std::string' (aka 'basic_string')

firstSets[decl] = declFirstSet;

So it seems to think I am declaring 'firstSets' when I am actually tring to insert into it. And it seems to treat 'firstSets' as an array instead of an unordered_map. How do I fix this?

6
  • Are those declarations/expression at global scope (outside of a function)? Commented Oct 31, 2021 at 20:23
  • Yes. @1201ProgramAlarm Commented Oct 31, 2021 at 20:25
  • Put the insert inside main and it should work Commented Oct 31, 2021 at 20:29
  • You can't have expressions in global scope. firstSets[decl] = declFirstSet; needs to be inside a function. You could also initialize it using the constructor when you define it. Commented Oct 31, 2021 at 20:29
  • ok that worked, thanks. Why can't you have expressions at the global scope? Commented Oct 31, 2021 at 20:33

2 Answers 2

1

Your std::make_pair is wrong. To get closer you need a std::set<int> instead of the std::set.

But what you really want is to just let to compiler make it for you:

    firstSets.insert(std::make_pair(decl, declFirstSet));

or use an easier syntax:

    firstSets[decl] = declFirstSet;

EDIT AFTER UNDERSTANDING THE PROBLEM On the otherhand, you want firstSets to come with initial content you can reorder the declarations:

#include <set>
#include <string>
#include <unordered_map>

std::string decl{"decl"};
std::set<int> declFirstSet{1, 2, 3, 4};
std::unordered_map<std::string, std::set<int>> firstSets{{decl, declFirstSet}};

int main() {}
Sign up to request clarification or add additional context in comments.

7 Comments

I have edited my question to reflect the changes you have suggested. @Bo R
My test of you code looke like this #include <set> #include <string> #include <unordered_map> int main() { std::unordered_map<std::string, std::set<int>> firstSets; std::string decl = "decl"; std::set<int> declFirstSet = {1, 2, 3, 4}; firstSets[decl] = declFirstSet; } Since you're not show all the code the the problem is likely earlier in the source code of macros involing VOID_TOK and the other values, or a missing header. Hard to tell.
My code is not in a main function, it is at the global scope. I will update to show more detailed code. @Bo R
@TomFinet, I know understand the problem. You cannot have an expression in a scope outside of a function. Thus firstSets[decl] = declFirstSet; is being tried as a declaration (which it is not) by the compiler. Having void somefunctionname() { firstSets[decl] = declFirstSet; } is the best you can do.
The "easier" syntax has different semantics. That might lead to subtle bugs if he thinks they do exactly the same thing...
|
1

I don't know why it is not working, but you don't need to call make_pair... Changing the insertion line to:

firstSets.insert({decl, declFirstSet});

Should solve your problem.

Here would be a full code example:

#include <set>
#include<string>
#include <unordered_map>
using namespace std;
int main()
{
    std::unordered_map<std::string, std::set<int>> firstSets;
    set<int> s = { 1, 2, 3 };
    firstSets.insert({ "key", s });   
}

But seems like you want them declared in global scope, so you would initialize like the following code:

#include <set>
#include<string>
#include <unordered_map>
using namespace std;

set<int> s1 = { 1, 2, 3 }, s2 = { 4, 5, 6 };
std::unordered_map<std::string, std::set<int>> firstSets{ {"key1", s1}, {"key2", s2}};
int main()
{
}

3 Comments

The error still persists. It still says: "unknown type name 'firstSets' " and "cannot use dot operator on a type". @vmp
@vmp, you must include <string> in you example.
oh, yeah... visual studio seems to add it under the hood, will fix that!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.