Why am I Getting String Does Not Name a Type Error

Why am I getting string does not name a type Error?

Your using declaration is in game.cpp, not game.h where you actually declare string variables. You intended to put using namespace std; into the header, above the lines that use string, which would let those lines find the string type defined in the std namespace.

As others have pointed out, this is not good practice in headers -- everyone who includes that header will also involuntarily hit the using line and import std into their namespace; the right solution is to change those lines to use std::string instead

string' does not name a type - c++ error

You have to use the std:: namespace scope explicitly in your header file:

class MyClass {    
public:

// this is the constructor function prototype
MyClass();

void setModuleName(std::string &); // << Should be a const reference parameter
// ^^^^^
std::string getModuleName();
// ^^^^^

private:
std::string moduleName;
// ^^^^^
};

In your .cpp file you have

using namespace std;

which is grossly OK, but better should be

using std::string;

or even better yet, also using the std:: scope explicitly like in the header.

C++ errors: ‘string’ does not name a type

You need to add:

#include <string>

cstring includes function to manipulate C-style string. This version works:

#include <list>
#include <string>

#if ECS == gnu
#include <cstring>
#define ECString string
using namespace std;
#else
#include <bstring.h>
#define ECString string
#endif

class ECArgs
{
public:
ECArgs(int argc, char *argv[]);
int nargs() { return nargs_; }
bool isset(char c);
ECString value(char c);
ECString arg(int n) { return argList[n]; }
private:
int nargs_;
int nopts_;
ECString argList[32];
list<ECString> optList;
};

int main()
{

}

string in namespace std does not name a type

You need to

#include <string>

<iostream> declares cout, cin, not string.

Compilation Error String does not name a type

You are missing the <string> header:

#include <string>

Compile error : `string' does not name a type

Other than the errors mentioned by the others, here's a semantic error:

Meeting::Meeting()
{
string speaker = " ";
string topic = " ";
string venue = " ";
string date = " ";
}

Here, you intend on assigning the class members the value " ", but what you're actually doing is defining 4 local string variables and initializing them with " ". To get expected results, you should do this:

Meeting::Meeting()
{
speaker = " ";
topic = " ";
venue = " ";
date = " ";
}

The above function body assigns the 4 class members the values as you intend to do.

Why do I get this error? [variable] does not name a type in C++ for std::string array even though it has been included and is in the same scope

You can't have "normal" statements outside of functions. Only declarations and definitions.

In your case you can solve the problem by initializing the array when you define it:

std::string const lett[26][5] = {
// A
{
" _____",
"/ \\",
"| /_\\ |",
"| | | |",
"|_/ \\_|"
},
// B
{ .... }
....
};


Related Topics



Leave a reply



Submit