0
#include <vector>
#include "Node.h" // EDIT: added
Node node1(true,11);
Node node2(true,04);
std::vector<Node> room1;
room1.push_back(node1);

I just want to add node1 to the vector of nodes named room1.To me it looks exactly how the tutorials instruct, however I am getting an error "room1 does not name a type."


Edit: Header to node has been included. All Node does is a take in (bool,int) and stores it. (The program is several hundred lines long just trying to keep the content relevant.)

3 Answers3

3

The error about room1 not naming a type comes from your code not being inside a function or object method. You can put definitions in the outer scope of your file, but not lines of arbitrary code to execute. Code needs a context to run in.

#include <vector>
#include "Node.h"

int main() {
    Node node1(true,11);
    Node node2(true,04);
    std::vector<Node> room1;
    room1.push_back(node1);
}

Note that not returning a result here from main is a very specific allowance made for main(), by the C++ specification:

What should main() return in C and C++?

Other functions you write should have a return statement if they have a return type.

Community
  • 1
  • 1
1

This is forbidden by the grammar of C++ as the only things allowed in namespace scope are declarations. These could be class, function or nested namespace declarations. Since room1.push_back(node1); doesn't fall in this category, your program is ill-formed. You probably want to put it in a function. The simplest of examples would be putting it in main:

#include <vector>
#include "Node.h" // EDIT: added
Node node1(true,11);
Node node2(true,04);
std::vector<Node> room1;

int main()
{
room1.push_back(node1);
return 0;
}

You likely want room1, node1 and node2 moved to main as well.

Pradhan
  • 15,095
  • 3
  • 39
  • 56
0

You forgot to include the header for Node or, more generally, Node is not a visible type in the current translation unit.

Shoe
  • 70,092
  • 30
  • 150
  • 251