I am using this command line to compile my program.
clang++ -std=c++17 -O3 main.cpp -o main
I have started the compiler 20 minutes ago, and it is just hanging. I terminate the compiler, and try to compile it again and it is still hanging. If I use the exact same command line, but without the -O3 the compiler completes instantly, but with the -O3 it is hanging.
The code that it is compiling is relatively simple, without any errors. What is going on?
#include <ctime> // for time()
#include <cstdlib> // for srand(), rand(), size_t, EXIT_SUCCESS
#include <iostream>
#include <string>
#include <vector>
using std::cout;
using std::endl;
using std::string;
using std::vector;
int main()
{
vector<string> messages;
messages.push_back(string("“Blessed are those who are persecuted because of righteousness, for theirs is the kingdom of heaven.”"));
messages.push_back(string("“Let the little children come to me, and do not hinder them, for the kingdom of heaven belongs to such as these.”"));
/* Literally 10000 more quotes from the Bible. */
srand(time(NULL));
cout << messages[ rand() % messages.size() ] << endl;
return EXIT_SUCCESS;
}
What is going on?
If you want to keep all the strings in the program (instead of reading them from a file) I would replace the std::vector<std::string> with a const std::vector<std::string_view> or maybe even a const std::vector<const char*> and initialize it with all the strings:
#include <ctime> // for time()
#include <cstdlib> // for srand(), rand(), size_t, EXIT_SUCCESS
#include <iostream>
#include <string_view>
#include <vector>
int main() {
const std::vector<std::string_view> messages{
"“Blessed are those who are persecuted because of righteousness, for theirs is the kingdom of heaven.”",
"“Let the little children come to me, and do not hinder them, for the kingdom of heaven belongs to such as these.”",
/* Literally 10000 more quotes from the Bible. */
};
srand(time(NULL));
std::cout << messages[ rand() % messages.size() ] << '\n';
}
I wasn't patient enough to wait for the compiler to finish compiling your original code. The above compiled in ~1 second.
Note: There's a <random> header which gives you access to much better pseudo random number generation than rand(). You should look into using that instead. The end of your program would look like something like this using that:
std::mt19937 prng(std::random_device{}());
std::uniform_int_distribution<std::size_t> dist(0, messages.size() - 1);
std::cout << messages[ dist(prng) ] << '\n';
Related
Even though I've included the < ranges > header in my code the compiler doesn't register it and causes errors when I try to run the code.
#include <iostream>
#include <iomanip>
#include <numeric>
#include <algorithm>
#include <ranges>
using namespace std;
int main() {
for (int i : views::iota(1, 11)) //this is the function supposedly included in the <ranges> library
cout << i << endl;
return 0;
};
When I runstd::cout << __cplusplus I get the output 202002 which means that in theory VS Code and the compiler are successfully reading that it's C++20. My compiler version is Apple clang version 13.1.6. I'm not sure how to change the language standard, but since I'm getting the C++20 output then it should have the right standard?
I've tried online compilers and the code runs just fine. Any idea on something else I can try?
Please check code given below. The random number it generates for each execution is the increment of the previous generated number in the previous execution.
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
srand(time(NULL));
cout<<"\n Random Number : "<<rand()<<endl;
cin.get();
return 1;
}
Please executeit for 5-6 times and you will see that the random numbers are increasing for each execution and they are very close to each other.
Note : Please use CodeBlocks or Visual studio to check it, not the online compilers.
Actually I found a way to solve my problem but still it might not be an answer to my question.
Anyway the problem is not about srand() or rand() functions but it is about the function time(NULL). Since I am trying to run this code on Windows, instead of using time(NULL) as a parameter for srand(), I used GetTickCount() and now it generates random numbers properly for each execution.
#include <iostream>
#include <cstdlib>
#include <windows.h>
using namespace std;
int main()
{
srand(GetTickCount());
cout<<"\n Random Number : "<<rand();
cout<<"\n";
cin.get();
return 1;
}
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.
I'm trying to run the following code, but eclipse is giving the errors "type thread cannot be resolved", "thread was not declared in this scope", "expected ; before t1".
#include <iostream>
#include "Tetris.h"
#include <Windows.h> //Sleep
#include <conio.h> //getch()
#include <thread>
#define SLEEP_TIME 1000
using namespace std;
void call_from_thread() {
cout << "hello!" << endl;
}
int main(){
/* some code... */
thread t1(call_from_thread);
/* some more code... */
return 0;
}
So far i've tried the following:
I've also tried the -thread flag as I've seen someone suggesting in an answer here.
I've been looking on google and stackoverflow for hours without being able to solve the problem. "thread" still cannot be resolved and therefore I can't compile and generate a binary.
Any help would be very welcome
thanks in advance
This is really driving me crazy:
#include <iostream>
#include <vector>
#include <string.h>
#include <thread>
using namespace std;
void test() {
vector<string> myvector;
string a("Teststring");
myvector.push_back(a);
cout << myvector.begin()->length() << endl;
}
int main() {
thread(test).join();
return 0;
}
The code compiles fine with the -std=c++11 flag to the compiler and the -pthread flag to the linker.
BUT: Eclipse does either know the std::thread or the myvector.begin()->length(), even if the code runs fine eclipse warns me "Method 'length' could not be resolved".
I tried every possible solution in here: Eclipse CDT C++11/C++0x support without any success. This took me so many hours now, what am I doing wrong?!
Is there anybody getting a project setup without problems with this code?
EDIT: Other code example - same problem:
#include <iostream>
#include <vector>
#include <thread>
using namespace std;
class TestClass {
public:
void test() {
cout << "test" << endl;
}
};
void test() {
vector<TestClass> testClassVector;
TestClass x;
testClassVector.push_back(x);
testClassVector.begin()->test();
}
int main() {
thread(test).join();
return 0;
}
Compiles and runs correct, but returns in eclipse: Method 'test' could not be resolved
EDIT:
working versions:
((TestClass)*(testClassVector.begin())).test();
TestClass foo2 = *(testClassVector.begin());
foo2.test();
still not working:
testClassVector.begin()->test();
The last compiles and works like the two above, but eclipse still claims:
Method 'test' could not be resolved
Maybe I'm wrong, but I think your problem don't come from Eclypse. Juste, begin() on a vector return a std::vector<T>::iterator first, this is not a pointer and there is no method length, but you can ask for the vector size with myvector.size(); if this is what you want.
The problem could come from your #include <string.h> that is not the same as #include <string>, string.h is for string operation like strcmp, strstr, etc... juste string will define the std::string object.
I don't have Eclipse set up but the problem appears to be around std::string. Does the problem go away if you remove the threading from the example? (I also changed to #include <string> instead of string.h)
#include <iostream>
#include <vector>
#include <string>
#include <thread>
using namespace std;
#if 0
void test() {
vector<string> myvector;
string a("Teststring");
myvector.push_back(a);
cout << myvector.begin()->length() << endl;
}
#endif
int main() {
//thread(test).join();
vector<string> myvector;
string a("Teststring");
myvector.push_back(a);
cout << myvector.begin()->length() << endl;
return 0;
}
That should hopefully print out 10.
Update from comment:
Does this generate the Eclipse warning?
auto tmp = *(myvector.begin());
std::cout << tmp.length() << std::endl;
What about this?
std::string foo("abc123");
std::cout << foo.length() << std::endl;
I guess one more too:
std::string foo2 = *(myvector.begin());
std::cout << foo2.length() << std::endl;
The solution found:
I downloaded eclipse kepler Kepler
Created a new project and tried to compile this source code (like above):
#include <iostream>
#include <vector>
#include <thread>
using namespace std;
class TestClass {
public:
void test() {
cout << "test" << endl;
}
};
void test() {
vector<TestClass> testClassVector;
TestClass x;
testClassVector.push_back(x);
testClassVector.begin()->test();
}
int main() {
thread(test).join();
return 0;
}
On the first run eclipse told me, thread belongs to the new c++11 standard and I have to add -std=c++11 to the compiler flags. To use thread I also added -pthread to the linker flags. With this steps the code could be compiled, but eclipse marks the thread still as unknown. To fix this I proceeded the following step:
Under C/C++ Build (at project settings), find the Preprocessor Include Path and go to the Providers Tab. Deselect all except CDT GCC Builtin Compiler Settings. Then untag Share settings entries … . Add the option -std=c++11 to the text box called Command to get compiler specs.
Found here.
Now - unbelievable but true - it works, even without any errors marked by eclipse. The solution is using the (beta) version of eclipse, wich seems to handle this in a better way.
Thanks for all your help!