string in namespace std does not name a type

C++StringNamespacesStd

C++ Problem Overview


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]: https://stackoverflow.com/questions/5527665/weird-string-does-not-name-a-type-

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;
}

C++ Solutions


Solution 1 - C++

You need to

#include <string>

<iostream> declares cout, cin, not string.

Solution 2 - C++

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.

Solution 3 - C++

You need to add:

#include <string>

In your header file.

Solution 4 - C++

You need to add

#include <string>

Here you are trying to access string noun:: but made no namespace named string noun. You are trying to access a private file.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1581100View Question on Stackoverflow
Solution 1 - C++Luchian GrigoreView Answer on Stackoverflow
Solution 2 - C++Ernest Friedman-HillView Answer on Stackoverflow
Solution 3 - C++Pablo Santa CruzView Answer on Stackoverflow
Solution 4 - C++nidhi kumariView Answer on Stackoverflow