An error showed up saying "time" is not a member of "std" for the sentence:
std::srand(std::time(0));
<ctime> and <cstdlib> already included. And the compiler is TDM-GCC MinGW.
I met this error several times and I still can't figure out the reasons.
This is because time(2) is a C standard library function, not a C++ standard library function.
#include <iostream>
#include <ctime>
int main(int argc, char *argv[])
{
auto t = time(nullptr);
std::srand(t);
std::cout << t << "\n";
return 0;
}
Related
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'm using C++ in Microsoft Visual Studio 12. I want to pass command line arguments. I have tried listing them in the MSVS's Project/Properties/Debugging/Command Arguments field and I've also tried using the CLIArgsMadeEasy add on but it never works. argc is always 1 where, of course, argv[0] is the app path.
Example: given a program of fred.exe that I would like to launch with three args : a,b,c
i.e. the equivalent of a cmd window line of
fred.exe a b c
I specify the args in the provided edit boxes exactly as:
a b c
using either method described above (MSVS standard or CLIArgsMadeEasy) but when I run they aren't passed.
The code is:
#include <iostream> // for standard I/O
#include <string> // for strings
#include <iomanip> // for controlling float print precision
#include <sstream> // string to number conversion
#include <math.h>
using namespace std;
int main(int argc, char *argv[])
{
...
I have tried this program in my visual studio and it works:
#include <iostream> // for standard I/O
#include <string> // for strings
#include <iomanip> // for controlling float print precision
#include <sstream> // string to number conversion
#include <math.h>
using namespace std;
int main(int argc, char *argv[])
{
for(int i = 1; i < argc; i++)
{
cout << i << ":" << argv[i] << endl;
}
return 0;
}
In xcode 4.3 I have set the project to use c++11: I changed the voice c++ language dialect to c++11, and c++ standard library to "libc++ ( LLVM c++ standard library with c++11 support)".
Then I tried to compile and execute this simple code:
#include <iostream>
using namespace std;
int main (int argc, char** argv)
{
char buffer[100];
cin.getline(buffer,100);
cout << buffer << endl;
return 0;
}
The problem is that it asks two times the input.For example I type "hello" and the stream remains opened, waiting for another string.If I type another string then it prints out "hello".
If I don't use c++11 this problem doesn't occur.
Does anyone know how to solve this problem? I want to take in input maximum 100 characters without using std::string.
This is a bug in libc++. My apologies. It is fixed on Mountain Lion. You can work around it by using getline(istream&, string&) instead:
#include <iostream>
#include <string>
using namespace std;
int main (int argc, char** argv)
{
std::string buffer;
getline(cin, buffer);
cout << buffer << endl;
return 0;
}
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.
#include <cstdlib>
using namespace std;
/*
*
*/
int main(int argc, char** argv)
{
cout << "COME AT ME BRO!\n"
return 0;
}
It says cout is unable to resolve identifier
The C++ code assistance is setup properly, I'm just not sure whatelse it could possibly be.
You did not include <iostream> and thus the identifier std::cout is never declared or defined in your program.
You're including the wrong header file. It should be :
#include <iostream>