• Register
search
Log In

There is not a thing like perfect code. Every code has errors and we are familiar with many of them. Error saying that xxx was not declared in this scope has its own specific meaning and all other errors have their own specific meaning. In this article we will take a look at the error “string does not name a type”.

 

Example

#include<iostream>

class class InvBin
{
private:
      string description;  // this line is causing the error
        };
int main()
{
    return 0;
}

Error

stringname.cpp:3:7: error: expected identifier before 'class'

 class class InvBin

       ^

stringname.cpp:6:7: error: 'string' does not name a type

       string description;  // item name

       ^

stringname.cpp:7:9: error: multiple types in one declaration

         };

         ^

 

Cause & Solution

C++ provides many standard operations like cout, cin and many more. If you have to make use or define anything standard then you have to use the syntax std:: then the name of the operation you want to have. For example std::cout, you will be thinking that I am stupid since you can use cout without the syntax std:: but trust me try doing that without including the using namespace std; then you will get the error that will be saying that cout was not declared in this scope even when you have included the header file iostream for the basic usage. To overcome this we have to use the using namespace std; syntax and it reduces our work load of writing standard in every line.

If you do not use the using namespace std; syntax then you will the error string does not name a type. To solve this issue you have to use the syntax using namespace std; or do include the std:: in starting of the data type string like this std::string description;

posted Jun 24 in c++ 100 points