I am having trouble using string variables inside of a class in c++. I have tried using include but that doesn't appear to have any noticeable effect.
Here are the contents of the header file the class is declared in:
#include <string.h>
#ifndef STUDENT_H
#define STUDENT_H
class Student
{
private:
int iStudentAge;
int iStudentBirthDay;
int iStudentBirthMonth;
int iStudentBirthYear;
string sStudentName;
string sStudentCourse;
public:
Student();
Student(int iValue1, int iValue2, int iValue3, int iValue4, string sValue5, string sValue6);
void setAge(int iNewValue);
int getAge();
void setBirthDay(int iNewValue);
int getBirthDay();
void setBirthMonth(int iNewValue);
int getBirthMonth();
void setBirthYear(int iNewValue);
int getBirthYear();
void setName(string iNewValue);
string getName();
void setCourse(string iNewValue);
string getCourse();
};
#endif // !STUDENT_H
The specific errors I am having are all on the lines that involve strings, except for the include line.
<string.h> is the C header file for string handling null-terminated byte strings. std::string is defined in the header file <string>.
So first change your include to the correct file, then you need to use the std:: scope prefix for all strings.
For C++ you should use the according library #include <string> not #include <string.h> witch is a C library.
For variable declaration use scope
std::string sStudentName;
or
using namespace std;
before the class declaration for string to be recognized as a type.
The first option is recommended, to know why see Why is “using namespace std;” considered bad practice?
As a compromise between littering "std::" all over the file, and using the potentially dangerous "using namespace std", you can write:
using std::string;
Related
I need to implement a class for one of my assignment and one of the function in the class that has string as datatype doesn't work
my definition code is :
#include <string>
class expression {
public:
expression();
void promptUser();
int getNum1();
int getNum2();
int calculate();
st::string str;
string numToString(int num);
string opToString();
private:
int num1;
int num2;
char op;
};
And in my implementation file
when I try to definite numTostring
string expression::numToString(int num) {
string digit;
...
It says that the declaration is incompatible with the header file(my class definition)
I have no idea why because both the function heading are the same.
the header file of expression.cpp( the implementation file) are :
#include "expression1.h"
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
Your class uses the unqualified name string, but there is no string data type defined in any enclosing scopes. There's a std::string data type defined in namespace std. That's looks to be the type that you need:
std::string str;
std::string numToString(int num);
std::string opToString();
You can keep from having to type out std:: everywhere by specifying a using statement:
using std::string;
But you might not want to do that inside a header file, so stick with fully qualifying the type.
If you want to use , you need to refer to it with std::
For example, your expression class declares:
st::string str;
string numToString(int num);
string opToString();
Which should be:
std::string str; // you typed st:: instead of std::
std::string numToString(int num); // lack of std::
std::string opToString(); // lack of std::
If you dont use 2 files (cpp + h) to define and declare your class then you can add line
using namespace std;
just after your includes. This way you wont have to type std:: each time you try to refer to string and similar types. However, using this is often called a bad "beginner" practice.
If you do use cpp+h then just add std:: before every string type and add using namespace std; to your cpp file.
If you want to know more then read:
1. http://www.cplusplus.com/doc/tutorial/namespaces/
2. Why is "using namespace std" considered bad practice?
3. How do you properly use namespaces in C++?
You also need to move
#include "stdafx.h"
up so it is the first header included. The compiler ignores everything that comes before that magic line.
I am working on a project that asks the user to input a string, and then through get and set functions simply displays the string. However I am having issues actually having the user input the string and then pass those to the get and set functions. Here is my code:
This is my Main.cpp :
#include "stdafx.h"
#include <iostream>
#include "Laptop.h"
#include<string>
using namespace std;
int main()
{
Laptop Brand;
string i;
cout << "Enter your brand of laptop : ";
cin >> i;
Brand.setbrand (i);
return 0;
}
This is my Laptop.cpp :
#include "stdafx.h"
#include <iostream>
#include "Laptop.h"
#include <string>
using namespace std;
void Laptop::setbrand(string brand)
{
itsbrand = brand;
}
string Laptop::getbrand()
{
return itsbrand;
}
and this is my laptop.h :
#include<string>
class Laptop
{
private :
string itsbrand;
public :
void setbrand(string brand);
string getbrand();
};
In my laptop.cpp I have errors with setbrand and getbrand. They say that getbrand and setbrand are incompatible . I am pretty sure it has to do with I am passing a string through the parameters. Any ideas?
You have missed to include the correct namespace in the laptop.h file therefore the compiler cannot find any declared string class in the current (global) namespace. Just put, in the beginning of the file, using std::string;.
On a side note I'd avoid generic
using namespace std;
because it fights the purpose of having namespaces in the first place. It's usually better to specify exactly what kind of class you are using. Therefore:
using std::string;
is better.
The good fix here is to use std::string instead of string in the header file:
class Laptop
{
private :
std::string itsbrand;
public :
void setbrand(std::string brand);
std::string getbrand();
};
unlike the other files you do not have using namespace std. I would actually suggest just using std::string everywhere. It is safer and will save you from worse problems later on.
I am receiving the error: identifier "string" undefined.
However, I am including string.h and in my main file, everything is working fine.
CODE:
#pragma once
#include <iostream>
#include <time.h>
#include <string.h>
class difficulty
{
private:
int lives;
string level;
public:
difficulty(void);
~difficulty(void);
void setLives(int newLives);
int getLives();
void setLevel(string newLevel);
string getLevel();
};
Can someone please explain to me why this is occurring?
<string.h> is the old C header. C++ provides <string>, and then it should be referred to as std::string.
You want to do #include <string> instead of string.h and then the type string lives in the std namespace, so you will need to use std::string to refer to it.
You forgot the namespace you're referring to. Add
using namespace std;
to avoid std::string all the time.
Because string is defined in the namespace std. Replace string with std::string, or add
using std::string;
below your include lines.
It probably works in main.cpp because some other header has this using line in it (or something similar).
Perhaps you wanted to #include<string>, not <string.h>. std::string also needs a namespace qualification, or an explicit using directive.
You must use std namespace. If this code in main.cpp you should write
using namespace std;
If this declaration is in header, then you shouldn't include namespace and just write
std::string level;
#include <string> would be the correct c++ include, also you need to specify the namespace with std::string or more generally with using namespace std;
game.cpp
#include <iostream>
#include <string>
#include <sstream>
#include "game.h"
#include "board.h"
#include "piece.h"
using namespace std;
game.h
#ifndef GAME_H
#define GAME_H
#include <string>
class Game
{
private:
string white;
string black;
string title;
public:
Game(istream&, ostream&);
void display(colour, short);
};
#endif
The error is:
game.h:8 error: 'string' does not name a type
game.h:9 error: 'string' does not name a type
Your using declaration is in game.cpp, not game.h where you actually declare string variables. You intended to put using namespace std; into the header, above the lines that use string, which would let those lines find the string type defined in the std namespace.
As others have pointed out, this is not good practice in headers -- everyone who includes that header will also involuntarily hit the using line and import std into their namespace; the right solution is to change those lines to use std::string instead
string does not name a type. The class in the string header is called std::string.
Please do not put using namespace std in a header file, it pollutes the global namespace for all users of that header. See also "Why is 'using namespace std;' considered a bad practice in C++?"
Your class should look like this:
#include <string>
class Game
{
private:
std::string white;
std::string black;
std::string title;
public:
Game(std::istream&, std::ostream&);
void display(colour, short);
};
Just use the std:: qualifier in front of string in your header files.
In fact, you should use it for istream and ostream also - and then you will need #include <iostream> at the top of your header file to make it more self contained.
Try a using namespace std; at the top of game.h or use the fully-qualified std::string instead of string.
The namespace in game.cpp is after the header is included.
You can overcome this error in two simple ways
First way
using namespace std;
include <string>
// then you can use string class the normal way
Second way
// after including the class string in your cpp file as follows
include <string>
/*Now when you are using a string class you have to put **std::** before you write
string as follows*/
std::string name; // a string declaration
I know this is quite a ridiculous question but this is quite confusing and irritating, as something that should work simply is not. I'm using Code Blocks with the GCC compiler and I am trying to simply create a string variable in my class
#ifndef ALIEN_LANGUAGE
#define ALIEN_LANGUAGE
#include <string>
class Language
{
public:
private:
string str;
};
#endif
Strange enough, my compiler halts me with an error saying this:
C:\Documents and Settings\...|11|error: `string' does not name a type|
||=== Build finished: 1 errors, 0 warnings ===|
For some reason, it is unable to find the class "string" which for some reason, my main.cpp is able to detect "#include " while my language class is not able for some reason.
This is the main I wrote quickly just to see it main itself is able to see the string file:
//main.cpp
#include <iostream>
#include <string>
#include "alien_language.h"
using namespace std;
int main()
{
string str;
return 0;
}
Does anyone know what's going on?
using namespace std;
That's what's going on.
You don't have std:: prefixing the string in your class. Everything in the standard library is in the namespace std.
It is generally regarded as bad practice to use using namespace std;, by the way. For more information on why and what to do instead, check out this question: Using std Namespace.
The string class is defined in the std namespace. You should chenge the class to this:
class Language
{
public:
private:
std::string str;
};
It is also possible, but not recommended to add this to the top of the header file:
using namespace std;
string is in namespace std, and you need to qualify it fully inside your header file:
#include <string>
class Language
{
public:
private:
std::string str;
};
Do not use using namespace std; or similar in header files.
You should refer to it as std::string;
It looks to me like you're missing the all-important (with a hint of sarcasm) using namespace std; line. Either add that in before your class, or explicitely use std::string str. I'd recommend against adding the using namespace std; line in a header file, as it would pollute the mainspace for any file that includes it.
The string class in standard C++ is in std namespace. Write something like
using std::string; in your header or fully qualify it as std::string in your header.
Beware that using namespace std; in header is a bad practice (read here).