[c++] string in namespace std does not name a type

This may just be a simple mistake that I'm not seeing, but I think I'm simply doing something wrong. Don't worry I'm not using namespace std in my header functions or anything which seemed to be this person's issue [Question I read similar to mine][1] [1]: Why am I getting string does not name a type Error?

I am getting 4 errors right now:

C:\Documents and Settings\Me\My Documents\C++Projects\C++\RandomSentence\Nouns.h|8|error: 'string' in namespace 'std' does not name a type|

C:\Documents and Settings\Me\My Documents\C++Projects\C++\RandomSentence\Nouns.h|12|error: 'string' in namespace 'std' does not name a type|

C:\Documents and Settings\Me\My Documents\C++Projects\C++\RandomSentence\Nouns.h|13|error: 'string' in namespace 'std' does not name a type|

C:\Documents and Settings\Me\My Documents\C++Projects\C++\RandomSentence\Nouns.cpp|9|error: no 'std::string Nouns::nounGenerator()' member function declared in class 'Nouns'|

||=== Build finished: 4 errors, 0 warnings ===|

Here is my header file:

class Nouns
{
    public:
        Nouns();
        std::string noun;
    protected:
    private:
        int rnp; // random noun picker
        std::string dog, cat, rat, coat, toilet, lizard, mime, clown, barbie, pig, lamp, chair, hanger, pancake, biscut, ferret, blanket, tree, door, radio;
        std::string nounGenerator()
};

And this is my cpp file:

#include "Nouns.h"
#include <iostream>

Nouns::Nouns()
{

}

std::string Nouns::nounGenerator(){
    RollRandom rollRandObj;

    rnp = rollRandObj.randNum;

    switch(rnp){
    case 1:
        noun = "dog";
        break;
    case 2:
        noun = "cat";
        break;
    case 3:
        noun = "rat";
        break;
    case 4:
        noun = "coat";
        break;
    case 5:
        noun = "toilet";
        break;
    case 6:
        noun = "lizard";
        break;
    case 7:
        noun = "mime";
        break;
    case 8:
        noun = "clown";
        break;
    case 9:
        noun = "barbie";
        break;
    case 10:
        noun = "pig";
        break;
    case 11:
        noun = "lamp";
        break;
    case 12:
        noun = "chair";
        break;
    case 13:
        noun = "hanger";
        break;
    case 14:
        noun = "pancake";
        break;
    case 15:
        noun = "biscut";
        break;
    case 16:
        noun = "ferret";
        break;
    case 17:
        noun = "blanket";
        break;
    case 18:
        noun = "tree";
        break;
    case 19:
        noun = "door";
        break;
    case 20:
        noun = "radio";
        break;
    }

    return noun;
}

This question is related to c++ string namespaces std

The answer is


You need to

#include <string>

<iostream> declares cout, cin, not string.


Nouns.h doesn't include <string>, but it needs to. You need to add

#include <string>

at the top of that file, otherwise the compiler doesn't know what std::string is when it is encountered for the first time.


You need to add:

#include <string>

In your header file.


Examples related to c++

Method Call Chaining; returning a pointer vs a reference? How can I tell if an algorithm is efficient? Difference between opening a file in binary vs text How can compare-and-swap be used for a wait-free mutual exclusion for any shared data structure? Install Qt on Ubuntu #include errors detected in vscode Cannot open include file: 'stdio.h' - Visual Studio Community 2017 - C++ Error How to fix the error "Windows SDK version 8.1" was not found? Visual Studio 2017 errors on standard headers How do I check if a Key is pressed on C++

Examples related to string

How to split a string in two and store it in a field String method cannot be found in a main class method Kotlin - How to correctly concatenate a String Replacing a character from a certain index Remove quotes from String in Python Detect whether a Python string is a number or a letter How does String substring work in Swift How does String.Index work in Swift swift 3.0 Data to String? How to parse JSON string in Typescript

Examples related to namespaces

Class 'App\Http\Controllers\DB' not found and I also cannot use a new Model How do I get an object's unqualified (short) class name? socket.error:[errno 99] cannot assign requested address and namespace in python What is the use of "using namespace std"? Visibility of global variables in imported modules Using :: in C++ 'namespace' but is used like a 'type' type object 'datetime.datetime' has no attribute 'datetime' Why am I getting error CS0246: The type or namespace name could not be found? string in namespace std does not name a type

Examples related to std

Converting std::__cxx11::string to std::string What is the use of "using namespace std"? How to get error message when ifstream open fails How to declare std::unique_ptr and what is the use of it? declaring a priority_queue in c++ with a custom comparator cc1plus: error: unrecognized command line option "-std=c++11" with g++ How to convert std::chrono::time_point to calendar datetime string with fractional seconds? string in namespace std does not name a type cout is not a member of std printf with std::string?