Click to See Complete Forum and Search --> : Private data member problems


jrw10293
November 20th, 2007, 06:30 PM
I was working on a class for the first time the other day and could not get anything but built in data types to work as private data members. I tried creating a test program with the bare minimum code and still could not get it to work. my error is:

'string' does not name a type

I'm sure there is something simple that I'm missing, but what is it?

here's what i tried:
//---------------------------------------
specification file "Problem.h"
//---------------------------------------
class SimpleClass
{

public:
SimpleClass();
private:
string test; //this line is where i get the mentioned error

};

//-----------------------------------------
implementation file "Problem.cpp"
//-----------------------------------------
#include <cstdlib>
#include <iostream>
#include <string>
#include "Problem.h"

using namespace std;

SimpleClass::SimpleClass()
{
test="bob";
cout<<test;
}

//---------------------------------------
main file "main.cpp"
//---------------------------------------

#include <cstdlib>
#include <iostream>
#include <string>
#include "Problem.h" //compiler highlights this line

using namespace std;

int main()
{
SimpleClass ();

system("PAUSE");
return 0;
}

Paul McKenzie
November 20th, 2007, 07:01 PM
1) Please use code tags when posting code.

2) The header file is missing the <string> header.

#include <string>

class SimpleClass
{
public:
SimpleClass();

private:
std::string test; //this line is where i get the mentioned error
};

If you introduce a type in your header such as std::string, then you include the file that defines that type.

Regards,

Paul McKenzie