question about class member

hi everyone
i've got a question about the variable declaration in the class
why i cannot declare a variable of type string member inside the class definition?
my compiler always show error message stating that there is no such type 'string'

is there any other ways to declare a variable of type string inside the class??

thanks
can you post your code?

did you forget to include String library?
Possible reasons:
1. you didn't include <string>
2 you didn't specify std namespace

Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <string>
#include <iostream>

class Foo
{
public:
    Foo(const std::string& name = "foo") : name_(name){}
    void printName() const {std::cout << name_ << '\n';}
private:
    std::string name_;
};

int main()
{
    Foo obj;
    obj.printName();
    return 0;
}
I always wonder why Foo is used as an example about 75% of the time, but hey it works well for some reason.
....in the .h file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class test
{
    string x;
    public:
    void print();
    test();
};

void test::print()
{
    std::cout << x << std::endl;
}

test::test()
{
    x = "hi";
}


in the .cpp file
1
2
3
4
5
6
7
8
#include<iostream>
#include"test.h"

int main()
{
    test t;
    t.print();
}


sorry for the late reply, recently busy with my test, now finished already
now back to the problem
the compiler i used is code block
and the problem is that everytime i compile the program, my compiler ends up with giving errors that states no such type as 'string'
is there something wrong with the program??
yeah you didn't include the library of string

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <string>//<-- here's your problem, you forgot to include it

class test
{
    string x;
    public:
    void print();
    test();
};

void test::print()
{
    std::cout << x << std::endl;
}

test::test()
{
    x = "hi";
}
i've tried to add the #include<string> into the .h file, but it still cannot compile
why is that happen??
Have you seen my post?
yeah include this also
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>

//then you can also use using
using namespace std;

class test
{
    string x;
    public:
    void print();
    test();
};

void test::print()
{
    cout << x << endl;
}

test::test()
{
    x = "hi";
}
sorry denis
i didn't knew that string need to be writen as std::string
just now i've tried to include using namespace std; in the header file
now it can compile correctly

why string needs to include the namespace std ??
do you really think that your advice to insert using namespace std; to a header file is a good advice?
why using namespace std; should not be included in the header file??
i'm not really understand the use of namespace std
Please learn C++, and it's up to you to include 'using' in header file or not.
But please, trust me it bad idea. If you want to know why please use Google with query
why using namespace std; should not be included in the header file??
ok i go check it out
thanks for advising me denis
no its not a good advice to use using in .h file. XD

my comment to loveless is, if you have short .h file and short main why not make it in one file only?

actually i am going to solve my assignment using object-oriented programming
so i am trying to write a short header file so that i can understand more about it

then i came to the problem with the string variable
but thanks to denis and olredixsis, finally i know where is the mistake i've made
that is i use string instead of std::string
Topic archived. No new replies allowed.