c++ - Doesn't name a type

I've got a problem. When I try to build the following code, I get:

'keywords' does not name a type
...
'whitespace' does not name a type

At lines 18-19 and 22-24. Can anyone help please? Here is the code.

/*
 * cpp2html.h
 *
 *  Created on: Mar 6, 2014
 *      Author: vik2015
 */

#ifndef CPP2HTML_H
#define CPP2HTML_H

#include <string>
#include <vector>
#define VERSION "0.1a"

using namespace std;

vector<string> keywords;
keywords.push_back("for");
keywords.push_back("white");

vector<string> whitespace;
whitespace.push_back("\n");
whitespace.push_back("\t");
whitespace.push_back(" ");

#endif

Answers


You cannot have arbitrary expressions (such as function calls) at global scope, only your declarations are allowed there.

Your calls to push_back must be in a function, maybe in main. Alternatively, if you want to initialize those objects when they are defined, you can do this in C++11:

std::vector<std::string> keywords{ "for", "white" };

Or this in C++03:

inline std::vector<std::string> getKeywords()
{
  std::vector<std::string> keywords;
  keywords.push_back("for");
  keywords.push_back("white");
  return keywords;
};
std::vector<std::string> keywords = getKeywords();

Also, never put using namespace std; in a header. It affects all code that includes your header, even if that code doesn't want that using directive.


Need Your Help

wpf datagrid : create a DatagridNumericColumn in wpf

c# wpf validation datagrid

I have a requirement that I want to make a datagridcolumn which only accepts numeric values(integer) ,when the user enter something other than numbers handle the textbox .