error: 'string' does not name a type

B

bd

What would cause this error?

Here is the code:

#include <string>
class name
{
public:
// default constructor
void NAME()
{
FirstName = "John";
MiddleName = "H.";
LastName = "Doe";
}

// non-default constructor
string NAME(string FN, string MN, string LN)
{
FirstName=FN;
MiddleName=MN;
LastName=LN;
}

// returns First Name, Middle Name, and Last Name in order
void getFirstLast()
{

}

// prints the first, middle, and last name
void print()
{

}

private:
string FN, MN, LN;

}
 
J

Joel Yliluoma

What would cause this error?

Here is the code:
...
FirstName = "John";
MiddleName = "H.";
LastName = "Doe";
...

These three variables are never declared in your code.
Also, the class declaration should end in a semicolon.
 
S

Sergiy Michka

What would cause this error?

Here is the code:

#include <string>

using namespace std; // class string is in this namespace, you should
use it or use sd::string instead of string
class name
{
public:
// default constructor
void NAME()
{
FirstName = "John";
MiddleName = "H.";
LastName = "Doe";
}

// non-default constructor
string NAME(string FN, string MN, string LN)
{
FirstName=FN;
MiddleName=MN;
LastName=LN;
}

// returns First Name, Middle Name, and Last Name in order
void getFirstLast()
{

}

// prints the first, middle, and last name
void print()
{

}

private:
string FN, MN, LN;
FN, MN, LN are not class members of your class. You should declare
these variables instead:
string FirstName, MiddleName, LastName;
 
J

Jim Langston

Sergiy Michka said:
using namespace std; // class string is in this namespace, you should
use it or use sd::string instead of string

Please, Sergiy, do not suggest people add using namespace std; without some
type of clarification
It is understood some people do this, but if you must use using, using
std::string; would be much better.
typing std::string instead of string is what I, and a lot of people, do.

[snip code]
 
B

bd

I see lots of people using std::string, but for now everything I am
taught in school says to just use

using namespace std;
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

I see lots of people using std::string, but for now everything I am
taught in school says to just use

using namespace std;

Please quote the text you are replying to.

There are a few problems with "using namespace", one being that you
should never use it in a .h-file (or any other file that gets
#included). Another advantage with prefixing is that it is always clear
which class/function/whatever we are talking about, when you work on big
projects there is a good chance that you define something that has the
same name as something in the standard library.

There is also a third alternative, which is kind of in between those
two, and that is to specify only those things you are using:

#include <string>

using std::string;

int main()
{
string str;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top