'String' does not name type

While compiling i got this error,
 
C:\Users\Derv\Desktop\Census\QuestsAndAnswers.cpp|25|error: 'string' does not name a type| 


I tried fixing it by adding #include <string>, using namespace std
and by even using std::string but for some reason, it still gives the error.

Here's the code ^^

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "QuestsAndAnswers.h"
#include <iostream>
#include <string>


QuestsAndAnswers::QuestsAndAnswers()
{
    std::string FName = "NULL";
}

    std::string FName;

void QuestsAndAnswers::setFName( std::string fname){

    FName = fname;
}
string QuestsAndAnswers::getFName(){ //error occurs here

    return FName;
}

//.h file

#ifndef QUESTSANDANSWERS_H
#define QUESTSANDANSWERS_H
#include <string>


class QuestsAndAnswers
{
    public:
        QuestsAndAnswers();
        void setFName( std::string fname);
        std::string getFName();
};

#endif // QUESTSANDANSWERS_H



Can anyone tell me what the problem is?
First, you don't have a member named FName, so you can't return that. You'll need to add that to your class.

Second, I'd be consistent with using std namespace. I tend to avoid it and prefix all std stuff with std::

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Class declaration
class QuestsAndAnswers
{
    std::string f_name;      
public:
    QuestsAndAnswers();
    void setFName( std::string new_name );
    std::string getFName() const;
};

// Then your functions would be...
void QuestsAndAnswers::setFName( std::string new_name )
{
   f_name = new_name;
}

std::string QuestsAndAnswers::getFName() const
{
   return f_name;
}
Thanks for answering, but what do you mean by i don't have a member named FName? Doesn't this line declare one?

std::string FName;
It looks like you have that line in global namespace.

1
2
3
4
5
6
7
8
class QuestsAndAnswers
{
    std::string FName; //Now this is a member
    public:
        QuestsAndAnswers();
        void setFName( std::string fname);
        std::string getFName();
};


But yeah, you have some really odd mixture of namespace operators. Just stick to std:: for everything. It'll make everything less confusing.
Last edited on
No, you need to declare inside your class, like in my example above. In that example, the f_name variable belongs to the QuestsAndAnswers class.

Your code declares a variable but it doesn't belong to the class. I guess its sort of global to that cpp. Anyway, it's not the way you want to be doing it. :-)

Edit: Appreciate the ninja.
Last edited on
Ahh.. i understand now! I shoud declare all variables inside the .h file to prevent Global Variables right? :)
You should declare variables inside your class. If they are just inside the header file, then they will still be in global namespace and accessible to anything that includes that header file.
Hm.. if i do something like this
1
2
3
4
5
6
7
8
9
10
11
 #ifndef EXAMPLE_H
#define EXPAMPLE_H
#include <string>


class Example
{
    public: std::string var
};

#endif //EXAMPLE_H 


would that be considered a global variable as it's in the header file for class Example?
I believe I already linked you to the C++ tutorial, otherwise here you go:
http://www.cplusplus.com/doc/tutorial/classes/

would that be considered a global variable as it's in the header file for class Example?

No, var would be member data of the class Example, and each Example object would have its own copy of var.

If you want something that is similar to a global variable but part of the class, you can define static member data.

1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef EXAMPLE_H
#define EXAMPLE_H // mistake in original

#include <string>

class Example
{
    public:

        static std::string var; // missing semicolon in original
};

#endif // EXAMPLE_H  


1
2
3
4
5
// Example.cpp

#include <string>

std::string Example::var; // static member data must be defined externally 

http://www.parashift.com/c++-faq/link-errs-static-data-mems.html

Now all Example objects will share the same var, and you can also access it like this:

1
2
3
4
5
6
7
8
9
// ...

int main()
{
    Example::var = "Hello, World!";

    Example e;
    std::cout << e.var << std::endl;
}
Oh.. thanks :)
Topic archived. No new replies allowed.