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
Related
I am trying to show in an edit box a program execution time. I have found some examples at Stackoverflow like the code here below.
using namespace std;
using namespace date;
ostringstream out;
auto start = chrono::system_clock::now();
//some program execution
auto finish = chrono::system_clock::now();
out << finish - start;
string s = out.str();
cout << s << '\n';
I have installed the library #include <date/date.h> via vcpkg. But the problem is: as soon as I do #include <date/date.h>, and run code with Local Windows Debugger a numerous mistake is happening as indicated in the snap shot below.
I mean, simple including of <date/date.h> library leads to errors.
How can I avoid this issue?
Many thanks in advance!
UPD
#include "pch.h"
#include "framework.h"
#include "MFCApplication2.h"
#include "MFCApplication2Dlg.h"
#include "afxdialogex.h"
#include <iostream>
#include <date/date.h>
void CMFCApplication2Dlg::OnBnClickedButton1()
{
//I have cleared the code inside, but errors yet appear
}
I've started debbuging on some app, which hangs up in a loop based on readdir call.
Step by step I've cut everything but problem code, this is it:
So, in basic, it shows name of first entry and nothing more. It even does not exits, just waiting for something.
Also, I've found, that if don't lin it against libpocofoundation, it works.
But I have to do it because it used in the original app.
I'm a little bit confused, I don't use Poco in this example in any way, but it some way hangs it.
Please help me, I'm in panic :D
#include <iostream>
#include <sys/types.h>
#include <dirent.h>
#include <cstring>
#include <string>
#include <fcntl.h>
using namespace std;
int main(int argc, char *argv[])
{
const char TMP_DIR[] = "/opt";
DIR *dir = opendir(TMP_DIR);
std::cerr
<< readdir(dir)->d_name
<< readdir(dir)->d_name
<< std::endl;
return 0;
}
So... I don't know why it was happening. So I just dropped libpoco.
I have been trying to debug this problem for a while and quite honestly, I just can't see what I'm doing wrong.
Why is there a syntax error?
#include <iostream>;
#include <time.h>;
#include <stdio.h>;
#include <stdlib.h>;
using namespace std;
class Problem3 {
public:
bool isPrime(long double num) {
srand(time(NULL));
return 0;
}
};
The error I'm getting is,
"Function 'srand' could not be resolved."
I'm well aware now that I don't need the semi-colons after 'include' statements
I'm using Eclipse CDT along with MinGW as my compiler
How I resolved the problem:
It had to do with the MinGW compiler I was using. Switching over to Visual Studio solved the problem.
; at the end of the #include directives are the problem in your code. #include directives don't need (wrong to place indeed) semicolons at the end unlike C++ statements.
[Warning] extra tokens at end of #include directive [enabled by default]
It seems any character after > in the directive causes this error/warning.
#include<iostream>a //error
Change to this:
#include <iostream>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
class Problem3 {
public:
bool isPrime(long double num) {
srand(time(NULL));
return 0;
}
};
int main(){
cout<<"Hello Main";
}
EDIT:
Regarding the linker issue:
One suggestion is C++ expects types to be explicitly casted between types (more than C). So, use a cast to convert time_t which is returned by the time to unsigned int which is the input parameter type of srand. (And of course this might not be the problem with linker error)
Instead of using stdlib.h, try using <cstdlib>, try if it helps. Because it uses namespace.
Apart from that, I have seen this snippet here. Use that pattern if it helps.
#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
srand(time(0)); //use current time as seed for random generator
int random_variable = rand();
cout << "Random value on [0 " << RAND_MAX << "]: "
<< random_variable << '\n';
}
there is already question in SO check if that helps Eclipse Method could not be resolved in a simple program C++
Never use time() to initialize srand()..
EDIT:
Now it seems many people got this kind of problem. I found a question How do I fix Eclipse CDT Error “Function 'isdigit' could not be resolved. He is facing the same problem. The asker suggested a work around to this in his question edit.
Quoted from that question:
I now believe this to be a Code Analysis problem. A better solution is
to edit the Code Analysis options to make "Function could not be
resolved" be a warning instead of an error. That way you can see the
warnings in Problems view, but continue to work. If the function is
REALLY missing, the compiler will tell you! I also have a new theory,
that the problem is with the Code Analyzer following symlinks, because
all of the "missing" functions are in symlinked include files. Would
love any input on this theory.
Hope that points to solve the problem.
; should not be there after #include.
#include <iostream>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include files shoule not end with ;
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!
I'm using eclipse and I'm building a simple program, but I get an error saying function sleep could not be resolved
#include <time.h>
#include <stdio.h>
#include <conio.h>
#include <iostream>
using namespace std;
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
printf("ciao");
sleep(20);
return 0;
}
I don't know if I need other libraries or something else.
MinGW should be installed properly, so I have no idea
The sleep() function is defined by POSIX, not by the C++ standard.
If you're on a Unix-like system, you need
#include <unistd.h>
If you're not, then the sleep() function might not even be available.
Oh, and mixing cout << ... and printf() is probably not a good idea, and you don't need the #include <conio.h>.
If you are using MinGW as stated, then you may need to include windows.h. The sleep implementation I think uses the Win API Sleep().
For example:
#include <windows.h>
#include <iostream>
int main() {
std::cout << "!!!Hello World!!!" << std::endl;
Sleep(20000);
std::cout << "Text Will Appear After 2 Sec.." << std::endl;
return 0;
}
When developing portable code that must run on unix and windows, I've always defined a sleep() macro for windows that calls the windows Sleep() that looks like this:
#define sleep(a) Sleep(a * 1000)
It's simple enough to do.