Can't use strings within classes

Maybe this is just the compiler, but this is killing me right now. I simply can't use strings within the classes that I write. I place the class within a header file (and have the function prototypes and whatnot) and then implement the functions in a .cpp file. When I try compiling it, the output makes as if it doesn't know what the heck 'string' is. Here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
class Student 
{
public:
	Student(int cAge);
	int getAge();
	void setAge(int cAge);
	string getName();

private:
	int age;
	string name;
};


And the implementation file code is this:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include "Student.h"
Student::Student(int cAge) {
	age = cAge;
}
int Student::getAge() {
	return age;
}
void Student::setAge(int cAge) {
	age = cAge;
}
string Student::getName() {
	return name;
}


I seriously don't know why this is happening and so some help/insight would be greatly appreciated!
Strings are part of the "#include <iostream>" libary. You must import that and then declare it like this "std::string child; etc" Might be a little bit different but it is std::string.

Hope that helps
Thanks for answering! But I don't think you can do "#include" statements in header files (that is, .h files). Even in my programming book, it gives an example class called "Student" and it merely declares the "name" member like I have it above.
You can include them in header files. I'm doing that at the moment. Example i have a header that I'm including stuff in as i need to tell the function decleration in the header about the pointers im using. If you can't get it working let me know and i'll write a quick program to check to see if i can get it working ;)
...Upon redoing it (and putting in "using namespace std") it seemed to work! Thank you!
Well main thing is that you got it working - only from experience of programs I've made for who ever it might be. Companies and what not don't like seeing "using namespace std;" Unless this is just for you or for school or something it probably wont matter much. But just etting you know. Hope that helps anyway.

;)
Ok, string is part of <string>, not <iostream>.

And including header files from other header files is common, accepted, recommended, and virtually required practice in C++ programming.

And lastly, never, ever, put a line like

 
using namespace xxx;  // xxx == whatever namespace 


in a header file.

All references to "string" in header files should be prefaced with std::, ie,

1
2
3
4
5
6
#include <string>

class Student {
  // ...
  std::string name;
};


See jsmith the only thing I don't understand with what you said is with any compiler i've used - If I use "#include <string> or <string.h>. Both give me an error telling me pretty much their is no such thing. So i've always just pulled it out with the iostream. I don't doubt your wrong or anything as i've seen it used before. Just lost why its never worked for me :(
Last edited on
Topic archived. No new replies allowed.