In the C++ program below, I include the string.h file and I successfully instantiate the C++ string class in it and call one of its member functions: size().
#include <iostream>
#include <string.h>
using namespace std;
int main( )
{
string s = "Hello";
cout << "String: " << s << endl;
cout << "Size of string: " << s.size() << endl;
cin.get();
return 0;
}
The output is:
String: Hello
Size of string: 5
I am using Dev-C++ 4.9.9.2
My question: doesn't the string.h file just provide the functions for manipulating C strings? It doesn't include the definition of the C++ string class right? So, how is it that I am able to access the C++ string class without using #include <string>? My understanding is that the string.h file is the C strings library file and <string> includes the C++ string library file. Is this not right?
Thanks!
This is because std::string is defined though one of the files included in the <iostream> header. The streams provide support for input and output of strings, so they need to include a string header in order to define the corresponding >> and << operations.
Related
This question already has answers here:
String Undeclared In C++
(5 answers)
Closed 1 year ago.
I am new to C++ and am currently working on a project that wants me to turn a Roman Numeral into a Hindu-Arabic number (our normal number system). I'm currently writing a class RomanNumeralType which needs to store a roman numeral type string. When I try to run the code below I get error code C3646: unknown override specifier and also error codes C4430 and C2061 which both address string (in bold) not being a valid identifier. It was my impression that #include fixed this however I am unsure why it is not working. I currently do not have a function that actually converts the numeral to a number but I want to fix this problem first. How do I get the string data type to work in my class?
Here is a minimally reproduced example of my code
//main
#include <iostream>
#include <iomanip>
#include "Header2.h"
#include <string>
using namespace std;
int main() {
string ans;
cout << "Please enter a string of Roman Numerals (M,D,C,L,X,V,I): " << endl;
cin >> ans;
}
RomanNumeralType r1(ans);
cout << "Your original Roman Numeral was: " << r1.getNumeral() << endl;
cout << "Your Roman Numeral as a integer is: " << r1.getNumber(r1.getNumeral()) << endl;
return 0;
}
//Header2.h
#ifndef HEADER2
#define HEADER2
#include <string>
class RomanNumeralType {
public:
**string** romanNumeral;
RomanNumeralType(**string** x)
{romanNumeral = x;}
**string** getNumeral()
{return romanNumeral;}
}
When you do #include <string> think of it as though you are pasting in the contents of a file shared across all developers that has all the definitions for string. That class lives within the std namespace, so when you want to use a string, you have to tell the compiler that string is in std. The preferred method is to use the fully qualified name, so every time you use string, you would replace it with std::string.
The other option is to do what you did in main.cpp, using namespace std;This tells the compiler that the namespace to look in is in std. This is generally considered bad practice for actual programs, but it is OK if it is a small, one off program like you seem to be writing.
If you do the first option, you will have to change everything in the std namespace such as cout, but it is clearer that you didn't write your own.
I am starting learning C++ a little while ago.
Came to know about header files and preprocessor statements.
I know that std::cin and std::cout are the objects/Function is declared in standard library IOSTREAM.
But when taking input as a string and to read the whole line. We have to getline() function or at least the instructor is using it in the video.
Now I have checked on the internet and most of the sites are showing that getline() is defined under STRING file/Library. But the thing is my program is working perfectly fine even without including the string file. So what I am missing.? or doing something wrong. and if you can please also explain how getline function is working. and also please what's the actual difference between using namespace std, and using std::
Thank you
#include <iostream>
int main()
{
std::string str;
std::cout << "Please enter your name: \n";
getline(std::cin, str);
std::cout << "Hello, " << str
<< " welcome to GfG !\n";
return 0;
}
You have to include the header <string> to use the function std::getline. It is implementation defined whether the header <iostream> includes the header <string>.
In this call using unqualified name getline
getline(std::cin, str);
the compiler applies the argument dependent lookup ADL (the first argument std::cin is defined in the namespace std::) and finds the corresponding name std::getline in the namespace std::.
My code is as follows.
#include "test.h"
#include "string"
#include "iostream"
using namespace std::string::find;
test::test(){
string str ("ffs test ffs");
string str2 ("test");
if (str.find(str2) != std::string::npos) {
std::cout << "found" << "\n";
} else {
std::cout << "not found" << "\n";
}
}
the issue I'm having is this, when trying to define a string in the C++ file qt states "unknown type name 'string'". Also on line 4 my 'import' highlights string as if it doesn't exist, despite it being an option the editor suggests to me while I'm typing it. What am I doing wrong here? Everything I find is to try and fix issues passing stuff to QStrings and nothing related to my issue as far as I can tell. I've tried both types of importing #include <thing> and #include "thing" on all the imports it doesn't seem to make a difference.
Use std::string instead of string.
#include "test.h"
#include <string>
#include <iostream>
test::test(){
std::string str ("ffs test ffs");
std::string str2 ("test");
if (str.find(str2) != std::string::npos) {
std::cout << "found" << "\n";
} else {
str::cout << "not found" << "\n";
}
}
Don't use using namespace (of course in your case, it wasn't a namespace, so that's another error), use <> for system headers.
After inclusion of the appropriate headers iostream, string etc, you can write:
using std::string;
This will bring in only string from the namespace std into your program.
And you can do this if you want to avoid typing std::string everywhere. You can do this for stream objects like cout, cin as well.
using std::cout;
using std::cin;
Use Scope operator :: in Your Code and Access manually to std class
std::string
it will help you !
In my code below errors occur and the program will not run, I am required to make a Constructor that must open the file with the given filename. If the filename does not exist then it Prints an error message and terminates the program.
Below is the code that I have done so far in C++:
#include "ReadWords.h"
#include <iostream>
#include <cstdlib>
using namespace std;
ReadWords::ReadWords(const char filename[])
{
wordfile.open(filename);
if (!wordfile)
{
cout << "cannot make " << filename << endl;
exit(1);
}
}
void ReadWords::close()
{
wordfile.close();
}
Why dont you try including fstream to the top of your file and see if that works
I suppose wordfile is of type std::fstream. If your ReadWords.h #includes <fstream>, it should work (compiles and works as expected).
By the way, it's a bad practice to use using namespace std;.
Also, since you use C++, take a look at std::string. It's safer than using plain char* or char[].
Here is my file.h :
#define MAXCOMPONENTS 20
#include <string>
#include <string.h>
#include <iostream>
class file{
public:
file(char const * filename);
virtual ~file();
void Takeinfocomponents();
void Takeshape();
void Getvalue(int i);
char *Getcomponents();
char *Getcolor();
protected:
private:
char const * filename;
String shape;
int value[MAXCOMPONENTS];
char components[MAXCOMPONENTS];
char color[MAXCOMPONENTS];
};
And my file.cpp :
#include <fstream>
#include <iostream>
#include <string.h>
#include <string>
#include "file.h"
using namespace std;
file::file(char const* filename)
{
cout << "constructor/fichier:" << filename << endl;
ifstream fichier(filename,ios::in);
if(fichier){
this->filename=filename;
fichier.close();
Takeshape();
Takeinfocomponents();
}else{
cout << "File name invalid." << endl;
}
}
file::~file()
{
}
char* file::Getcolor(){
return this->color;
}
char* file::Getcomponents(){
return this->components;
}
void file::Getvalue(int i){
cout << this->value[i] << endl;
}
void file::Takeinfocomponents(){ // pic up name of components, his number and his color
cout << "Takeinfocomponents/fichier:" << filename << endl;
ifstream fichier(this->filename,ios::in);
ifstream stop(this->filename,ios::in);
string line;
int i=0;
getline(fichier,line);
getline(stop,line);
getline(stop,line);
while(line!="/" && i!=99){ // take all informations while the stop signal isn't read
getline(stop,line);
fichier >> this->components[i] >> this->value[i] >> this->color[i];
cout << this->components[i] << this->value[i] << this->color[i] << endl;
i++;
}
fichier.close();
}
void file::Takeshape(){ // pic up the shape in .txt
cout << "Takeshape" << endl;
fstream fichier(this->filename,ios::in);
string shape;
fichier >> shape;
this->shape=shape;
fichier.close();
}
This is a part of a larger programm who make graphic from informations ( from the .txt ), this part is use to pic up informations from the .txt.
The problem come from the declaration of the :
String shape;
He told me that string is not a name type. I've tried with a small "s" :
string shape;
But this ain't working.
I've the impression that i miss a very small things that could unlock my problem.
Thx for help.
Notabene : I'm french and my english is not this good, please answer like i was a little child ahah !
You have to explicitly state the namespace:
std::string shape;
You shouldn't pollute the namespace in the headers, so using namespace std is not an option here.
See also the question about namespace pollution. If you just need strings, prefer to use
using std::string;
in the cpp file.
C++ uses the concept of a namespace. A namespace is used to group types, variables, etc. together in a meaningful way, regardless of the number of header files those types or variables are spread across.
In this example, the string type is inside the std namespace. std is short for Standard Template Library, and it is the namespace that most of C++'s library classes, etc. are stored in.
The correct way of accessing type inside a namespace is namespace::type, so the correct way of accessing the string type inside the std namespace is std::string. You can also write using namespace std to access the types in std without having to write std:: each time, but doing this in a global scope is a bad idea, because it pollutes the global namespace.
In the code you posted, string shape; appears before using namespace std, as the #include "file.h" appears before it. Therefore, it won't take effect.
To be able to use the string class and create string objects, you need to include...
#include <string>
... at the top of your header files.
You do not need...
#include <string.h>
The string class, like all STL classes, is part of the std namespace. If you do not want to write std:: before every class name, you can simply state...
using namespace std;
... at the top of your header files so that instead of...
std::string shape;
... you can simply use...
string shape;