This code works for me:
#include <string>
#include <iostream>
int main()
{
std::string s;
s = "hello world";
cout << s;
return 0;
}
But this one doesn't:
#include <string>
int main()
{
string s;
s = "hello world";
return 0;
}
Is the include of <iostream> needed as well as the <string> one?
I'm using Eclipse CDT IDE.
Iostream is not needed to use string. You are missing using namespace std (or alternatively using the std:: prefix) in the second example, that's why it's not working.
Related
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
char str[30];
gets(str);
}
when i use gets () function compiler gives me the following error
error: 'gets' was not declared in this scope
i was using G++ with geany ide
please make the solution simple cuz iam a beginner.
gets was deprecated in C++11 and removed from C++14. If you are using GCC6.0 or newer then by default it uses C++14 and won't be available. Instead of using
main()
{
char str[30];
gets(str);
}
use
int main()
{
std::string str;
std::getline(cin, str);
}
gets is an unsafe function and is not supported by the C Standard any more.
Instead use fgets.
For example
#include <iostream>
#include <cstdio>
#include <cstring>
int main()
{
char str[30];
std::fgets(str, sizeof( str ), stdin );
str[ std::strcspn( str, "\n" ) ] = '\0';
//...
}
You can use scanf() to input string.
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
char str[30];
scanf("%s", str);
}
Whenever I use std::string to declare a string variable in c++ it prevents the program from outputting anything. for example:
#include <cstdlib>
#include <iostream>
#include <string>
int main() {
std::cout << "Hello";
// std::string s;
return 0;
}
This will output Hello to the command-line as it should do.
#include <cstdlib>
#include <iostream>
#include <string>
int main() {
std::cout << "Hello";
std::string s;
return 0;
}
This will not output anything (and no errors) since i'm declaring a variable using std::string
I'm using the minGW compiler on a Windows 10 64bit machine
I ran this code on a different IDE and it was successful. For some reason I get the above error message on Xcode. I assume I'm missing a header of some kind, but I'm not sure which one.
#include <iostream>
#include <limits>
#include <string>
#include <vector>
int main() {
vector<string> listRestaurants; // error: Implicit instantiation of undefined template
return 0;
}
Xcode 10.2.1 was showing me the error
Implicit instantiation of undefined template 'std::__1::vector<std::__1::basic_string<char>, std::__1::allocator<std::__1::basic_string<char> > >'.
#include <iostream>
#include <vector>
int main(int argc, const char * argv[]) {
std::vector<std::string> listRestaurants;
....
return 0;
}
Fixed my issue.
If adding std:: is not the issue for you, then check if you have #include <vector>. That fixed the issue for me.
Didn't realize that #include <vector> is required. I thought it was part of standard library template; I ran this code in VSCODE IDE and it worked fine for me
#include <iostream>
#include <vector>
using namespace std;
int main()
{
uint_least8_t i; // trying to be conscious of the size of the int
vector<int> vect;
for(i = 0; i < 5; ++i)
{
vect.push_back(i);
}
for(auto i : vect)
{
cout << i << endl;
}
return 0;
}
From the comments:
The std namespace houses both of those templates. Change vector to std::vector, and string to std::string. – WhozCraig
the vector and string were placed in the namespace std
using namespace std;
I am trying to reverse a string (c++, compiling with g++).
Isn't string considered a container for the algorithm functions?
This is the code:
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string str = "hello";
str.reverse(str.begin(), str.rbegin());
return 0;
}
Thanks
The std::string class template does not have a member function called reverse. There is a std::reverse function located in the <algorithm> header. You probably want to use it in a following manner:
#include <string>
#include <algorithm>
int main() {
std::string str = "hello";
std::reverse(str.begin(), str.end());
}
Note the use of str.end() in place of your str.rbegin(). You can also define a new string and use the string constructor overload that accepts reverse iterators:
#include <string>
int main() {
std::string str = "hello";
std::string reversestr(str.rbegin(), str.rend());
}
std::string has no method reverse. But std::reverse exists:
#include <string>
#include <algorithm>
#include <iostream>
int main()
{
std::string str = "hello";
std::reverse(str.begin(), str.end());
std::cout << str << "\n"; // prints "olleh"
}
I am a C++ noob, fiddling with the following problem for some hours now. Hopefully, someone can enlighten me.
I had a cpp file with content like so:
test.cpp file content
#include <iostream>
#include <exception>
#include <stdlib.h>
#include <string.h>
using std::cin; using std::endl;
using std::string;
string foobar(string bar) {
return "foo" + bar;
}
int main(int argc, char* argv[])
{
string bar = "bar";
System::convCout << "Foobar: " << foobar(bar) << endl;
}
This one compiles and runs well. Now I'd like to put foobar into an external library:
mylib.h file content
string foobar(string bar);
mylib.cpp file content
#include <string.h>
using std::cin; using std::endl;
using std::string;
string foobar(string bar) {
return "foo" + bar;
}
test.cpp file content
#include <iostream>
#include <exception>
#include <stdlib.h>
#include "mylib.h"
int main(int argc, char* argv[])
{
string bar = "bar";
System::convCout << "Foobar: " << foobar(bar) << endl;
}
I adjusted my Makefile, so that test.cpp compiles and links mylib, but I always encounter the error:
test.cpp::8 undefined reference to `foobar(std::string)
How do I have to handle string arguments? My attempts seems to be completely wrong here.
Regards
Felix
The C++ standard library type std::string is in the header string. To use it, you must include <string>, not <string.h>. Your mylib.h should look something like
#ifndef MYLIB_H
#define MYLIB_H
#include <string>
std::string foobar(std::string bar);
#endif
and your mylib.cpp should include it:
#include "mylib.h"
std::string foobar(std::string bar) {
return "foo" + bar;
}
Note that it may be unnecessary to pass bar by value. Looking at your code, a const reference might do.