I have a basic program that compares two strings :
#include <string>
#include <iostream>
using namespace std;
int main (int argc, char *argv[]) {
if(strcmp (argv[0],"./test") != 0) {
cout << "not equal" << endl;
} else {
cout << "equal" << endl;
}
return 0;
}
it compiles with gcc but not with clang :
> clang -o test test_clang.cpp
test_clang.cpp:7:6: error: use of undeclared identifier 'strcmp'
if(strcmp (argv[0],"./test") != 0) {
^
1 error generated.
Why doesn't it compile with clang ?
EDIT: People are getting harsh on stack overflow, up to the point that I am hesitating to post a question. The question above has a simple answer, fine, but is it normal to down-vote questions (twice in the first minute!) because they have a simple, yet non obvious, answer ?
Use
#include <string.h>
or
#include <cstring>
instead of
#include <string>
The string header is for the std::string from C++. string.h is for C zero terminated char* strings. cstring is like string.h but for C++.
The reason it worked with gcc is probably different warning/error level settings. It is possible to compile the code without #including the header and having the declaration of strcmp. The compiler will not be able to do type checking but the symbol still gets resolved by the linker.
You can also avoid using strcmp completely and write
#include <string>
#include <iostream>
int main (int argc, char *argv[]) {
std::string command = argv[0];
if( command != "./test" ) {
std::cout << "not equal" << endl;
} else {
std::cout << "equal" << endl;
}
return 0;
}
Using a std::string on one side of the comparison will cause the "./test" string to be converted into a std::string as well and the comparison will be done by the == operator of the std::string class.
You're not including the correct header file
#include <cstring>
You need to #include <cstring> (or possibly #include <string.h>.)
Many compilers include extra standard headers when you include another. The Standard allows this; it's your responsibility to use the headers that guarantee declarations for what you use, not just headers that happen to have the declarations for your compiler.
You have to include <cstring>. <string> is the header for C++ strings.
Related
I am trying to take a string and parse it into an int. I have read the many answers out there, and it seems that using stoi is the most up-to-date way. It appears to me that stoi uses std, but I am getting Function 'stoi' could not be resolved despitre using namespace std;
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include<stdlib.h>
using namespace std;
int main(int argc, char* argv[]) {
string line = "";
string five = "5";
int number = stoi(five); //Error here with stoi
return 0;
}
Any ideas what is causing this?
Update:
I am using Eclipse. My flags are: -c -fmessage-length=0 -std=c++11
If you are using GCC or MINGW, then this is the answer:
std::stoi doesn't exist in g++ 4.6.1 on MinGW
This is a result of a non-standard declaration of vswprintf on
Windows. The GNU Standard Library defines
_GLIBCXX_HAVE_BROKEN_VSWPRINTF on this platform, which in turn disables the conversion functions you're attempting to use. You can
read more about this issue and macro here:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37522.
If you're willing to modify the header files distributed with MinGW,
you may be able to work around this by removing the
!defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF) macro on line 2754 of
.../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h, and adding
it back around lines 2905 to 2965 (the lines that reference
std::vswprintf). You won't be able to use the std::to_wstring
functions, but many of the other conversion functions should be
available.
Please always provide platform and compiler information.
Toggle on C++11 support in your compiler flags. -std=c++11 for a recent gcc. For Eclipse, please refer to the corresponding question in the FAQ and this answer explains how to get rid of the remaining Eclipse warning.
If you are amenable to parsing an int another way, how about using an STL algorithm and a C++11 lambda expression?
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "12345";
int num = 0;
for_each(str.begin(), str.end(), [&num](char c){ num = 10 * num + (c - '0'); });
cout << str << " = " << num << endl;
}
So I recently discovered the use of map and vectors, however, I'm having trouble of trying to figure a way to loop through a vector containing strings.
Here's what I've tried:
#include <string>
#include <vector>
#include <stdio>
using namespace std;
void main() {
vector<string> data={"Hello World!","Goodbye World!"};
for (vector<string>::iterator t=data.begin(); t!=data.end(); ++t) {
cout<<*t<<endl;
}
}
and when I try to compile it, I get this error:
cd C:\Users\Jason\Desktop\EXB\Win32
wmake -f C:\Users\Jason\Desktop\EXB\Win32\exbint.mk -h -e
wpp386 ..\Source\exbint.cpp -i="C:\WATCOM/h;C:\WATCOM/h/nt" -w4 -e25 -zq -od -d2 -6r -bt=nt -fo=.obj -mf -xs -xr
..\Source\exbint.cpp(59): Error! E157: col(21) left expression must be integral
..\Source\exbint.cpp(59): Note! N717: col(21) left operand type is 'std::ostream watcall (lvalue)'
..\Source\exbint.cpp(59): Note! N718: col(21) right operand type is 'std::basic_string<char,std::char_traits<char>,std::allocator<char>> (lvalue)'
Error(E42): Last command making (C:\Users\Jason\Desktop\EXB\Win32\exbint.obj) returned a bad status
Error(E02): Make execution terminated
Execution complete
I tried the same method using map and it worked. The only difference was I changed the cout line to:
cout<<t->first<<" => "<<t->last<<endl;
Add iostream header file and change stdio to cstdio.
#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
using namespace std;
int main()
{
vector<string> data={"Hello World!","Goodbye World!"};
for (vector<string>::iterator t=data.begin(); t!=data.end(); ++t)
{
cout<<*t<<endl;
}
return 0;
}
#include <iostream>
#include <vector>
#include <string>
int main()
{
std::vector<std::string> data = {"Hello World!", "Goodbye World!"};
for (std::vector<std::string>::iterator t = data.begin(); t != data.end(); t++) {
std::cout << *t << std::endl;
}
return 0;
}
Or with C++11 (or higher):
#include <iostream>
#include <vector>
#include <string>
typedef std::vector<std::string> STRVEC;
int main()
{
STRVEC data = {"Hello World!", "Goodbye World!"};
for (auto &s: data) {
std::cout << s << std::endl;
}
return 0;
}
From the Open Watcom V2 Fork-Wiki on the C++ Library Status page:
<string>
Mostly complete. Although there are no I/O operators, all other member functions and string operations are available.
A workaround (besides implementing the << operator) would be asking the string instances for the C string:
for (vector<string>::iterator t = data.begin(); t != data.end(); ++t) {
cout << t->c_str() << endl;
}
This of course only works as long as the strings don't contain zero byte values.
When I compile your code, I get:
40234801.cpp:3:17: fatal error: stdio: No such file or directory
#include <stdio>
^
You clearly have a header called "stdio" in your include path that you haven't shown us.
If you change that line to the standard #include <iostream>, then the only reported error is that you wrote void main() instead of int main(). Fix that, and it will build and run.
In passing, note also that using namespace should be avoided.
I found a solution to my own issue. Instead of using a c_str, I used std::string and switched to using the G++ compiler instead of Open Watcom
Instead of having:
char *someString="Blah blah blah";
I instead replaced it with:
string someString="Blah blah blah";
This way is much more efficient and easier.
When I include the required library, the "#include.." line doesn't show any warning. But when I use the functions in that library, I find the Vim shows that "..use of undeclared function...". It seems that the library is not correctly included. So I want to know how to figure out this problem?
The screenshots for this question are attached as follows:
Try including it as follows:
#include <stdlib.h> //use <> instead of ""
Also, the "printf" function comes from the "cstdio" library so try implementing that library as well,
#include <stdio.h>
UPDATED
The easiest way to fix that problem is;
Include the stdio.h library
#include <stdio.h>
Then, instead of typing;
printf('s');
you do,
printf("s");
Now, if you really want to print a character 's', then use,
printf("%c", 's'); // Tells the printf function that 's' is a character
The final code would look like;
#include <stdio.h>
int main(int argc, char** argv) {
printf("s");
printf("%c", 's');
return 0;
}
Now, your comment was that "cout" does not work. In order for "cout" to work you need to include the iostream library:
#include <iostream>
Then, you can use "cout" in your code;
std::cout << 's';
std::cout << "s";
Or you can include "namespace std" and the "iostream" library to avoid using std:: before "cout"
include <iostream>
using namespace std;
Thereafter, use cout without std::
cout << 's';
cout << "s";
The final code would be;
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
cout << 's';
cout << "s";
return 0;
}
If you want to learn more about what is in the iostream library and how to use it I recommend using this site:
http://www.cplusplus.com/reference/iostream/
Also, for the stdio.h,
http://www.cplusplus.com/reference/cstdio/
I was trying to play around with Strings in a Hangman program that I'm writing and couldn't get them to work so tried working with them on a simpler basis and I'm still having no luck.
As far as I've read online in the references and what other people have said this code should work:
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
int main (int argc, char** argv){
string word = {"Hello"};
int length = strlen(word);
}
But I get this compiler error:
'string' was not declared in this scope
and consequently, 'word' is also not declared in scope.
Can anyone see what I'm doing wrong? I'm using the g++ compiler on Ubuntu if that makes a difference, no idea which version though.
You are confusing C and C++.
You included only C libraries, whereas std::string comes from the C++ header string. You'd have to write:
#include <string>
to use it. However, you'd then have to make other changes, such as not using strlen.
You should learn from your C++ book, not random posts on the internet (#lolirony)
C version
#include <string.h>
int main(void)
{
const char* word = "Hello";
const size_t length = strlen(word); // `size_t` is more appropriate than `int`
return 0;
}
C-like C++ version
#include <cstring>
using namespace std;
int main()
{
const char* word = "Hello";
const size_t length = strlen(word);
}
Idiomatic C++ version (recommended)
#include <string>
int main()
{
const std::string word = "Hello";
const std::size_t length = word.size();
}
'string' was not declared in this scope
You need to include the header <string> and refer to it as std::string. Also, strlen does not understand std::string or any user defined types, but you can use the size() method instead:
#include <string>
int main()
{
std::string word = "Hello";
size_t length = word.size();
}
<cstring> is the header for C++ support of C-style null-terminated strings. You should include <string>.
You haven't included the C++ string header in your project.
#include <string>
The libraries that you've included are all plain-C headers.
Additionally, strlen() doesn't work with a c++ string; you should use word.size() instead.
string is a specialization of standard class std::basic_string . It is declared in header <string>
So if you want "to play around with standard class std::string:" you need to include directive
#include <string>
Header <cstring> is not the same as header <string> and contains declarations of standard C functions such as strlen.
However there is no any sense to apply function strlen to an object of type std::string The compiler in this case will issue an error.
I advice you to play with the following code that to see the difference
#include <iostream>
#include <string>
#include <cstring>
int main (int argc, char** argv)
{
std::string word = "Hello";
std::string::size_type length = word.length();
std::cout << "Object word of type std::string has value "
<< word << " with length of " << length
<< std::endl;
std::cout << "The size of the object itself is " << sizeof( word ) << std::endl;
char another_word[] = "Hello";
size_t another_length = std::strlen( another_word );
std::cout << "Object another_word of type char [6] has value "
<< another_word << " with length of " << another_length
<< std::endl;
std::cout << "The size of the object itself is " << sizeof( another_word ) << std::endl;
}
I am trying to take a string and parse it into an int. I have read the many answers out there, and it seems that using stoi is the most up-to-date way. It appears to me that stoi uses std, but I am getting Function 'stoi' could not be resolved despitre using namespace std;
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include<stdlib.h>
using namespace std;
int main(int argc, char* argv[]) {
string line = "";
string five = "5";
int number = stoi(five); //Error here with stoi
return 0;
}
Any ideas what is causing this?
Update:
I am using Eclipse. My flags are: -c -fmessage-length=0 -std=c++11
If you are using GCC or MINGW, then this is the answer:
std::stoi doesn't exist in g++ 4.6.1 on MinGW
This is a result of a non-standard declaration of vswprintf on
Windows. The GNU Standard Library defines
_GLIBCXX_HAVE_BROKEN_VSWPRINTF on this platform, which in turn disables the conversion functions you're attempting to use. You can
read more about this issue and macro here:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37522.
If you're willing to modify the header files distributed with MinGW,
you may be able to work around this by removing the
!defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF) macro on line 2754 of
.../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h, and adding
it back around lines 2905 to 2965 (the lines that reference
std::vswprintf). You won't be able to use the std::to_wstring
functions, but many of the other conversion functions should be
available.
Please always provide platform and compiler information.
Toggle on C++11 support in your compiler flags. -std=c++11 for a recent gcc. For Eclipse, please refer to the corresponding question in the FAQ and this answer explains how to get rid of the remaining Eclipse warning.
If you are amenable to parsing an int another way, how about using an STL algorithm and a C++11 lambda expression?
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "12345";
int num = 0;
for_each(str.begin(), str.end(), [&num](char c){ num = 10 * num + (c - '0'); });
cout << str << " = " << num << endl;
}