62

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;
}
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
  • 2
    Why use a switch when an array would work beautifully? – TheZ Aug 07 '12 at 20:46
  • 2
    @TheZ: Why not `#include` the things you want to use? I suspect these questions have the same answer... – not all wrong Aug 07 '12 at 20:48
  • switch is what I'm ised to using. Personal preference. And to me it looks neater. –  Aug 07 '12 at 20:53
  • @NekkoRivera Well, now you have the chance every programmer gets once in a while: make more efficient/extensible code, or stick to your old ways :) – TheZ Aug 07 '12 at 20:54
  • 1
    Once I get the program working (there are other errors in other parts of the code that I can probably fix myself) I will try to change the switch statement into an array. It'll probably take up less room and make the program easier to work with. –  Aug 07 '12 at 21:00

5 Answers5

105

You need to

#include <string>

<iostream> declares cout, cin, not string.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • 2
    `` probably indirectly declares `string` (in most versions, anyway) but note that he's including it *after* `Nouns.h` is already processed. Although this wouldn't be the best solution, reversing the order of the two includes would probably fix the problem as well. Of course, this would come up again everywhere else `Nouns.h` was included, so it's better to include `` in that file. – Ernest Friedman-Hill Aug 07 '12 at 20:48
  • 1
    @ErnestFriedman-Hill I've never seen iostream including string... And including string in nouns.h is the correct approach here, not outside. – Luchian Grigore Aug 07 '12 at 20:49
  • Oops... I sware I spent atleast 30 minutes staring at the code trying to figure out what was wrong. Simple things are easily overlooked. Thank you... –  Aug 07 '12 at 20:51
  • @LuchianGrigore: If I recall, MSVC's `iostream` includes enough of string for it to work in some but not all contexts, leading to much confusion. – Mooing Duck Aug 07 '12 at 20:54
  • @LuchianGrigore -- note that I didn't say *includes*, but *declares*. For example, on my system (gcc 4.0), iostream includes ostream which includes ios which includes iosfwd which includes bits/stringfwd.h which forward-declares std::string. – Ernest Friedman-Hill Aug 07 '12 at 20:57
  • And for sure, including `` in `Nouns.h` is the right fix. – Ernest Friedman-Hill Aug 07 '12 at 20:58
  • @ErnestFriedman-Hill that's enough for anything including iostream to compile, because ios_base & string are used as parameters to `<<` and `>>`. But not enough to use it anywhere meaningful (like a member of a class, where a full definition is required). But yeah, I missed that you said declares, not includes. ;) – Luchian Grigore Aug 07 '12 at 21:00
9

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.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
4

You need to add:

#include <string>

In your header file.

Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
0

Notice

#include <string.h>

is not

#include <string>
alboforlizo
  • 170
  • 1
  • 6
-2

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.

Obsidian
  • 3,719
  • 8
  • 17
  • 30