Dev C++ simple threading program - c++

I created a simple program to learn how to use threading. This is the code i created
#include <iostream>
#include <stdlib.h>
#include <thread>
using namespace std;
void count_asc();
void count_desc();
int main() {
thread first(count_asc);
thread second(count_desc);
first.join();
second.join();
system("pause");
return 0;
}
void count_asc(){
int ctr;
for(ctr=1;ctr<=10;ctr++){
cout<<"First thread: "<<ctr<<endl;
}
}
void count_desc(){
int ctr;
for(ctr=10;ctr>=1;ctr--){
cout<<"Second thread: "<<ctr<<endl;
}
}
I am using Dev C++ 5.5.3. I have read other questions about this but me being a beginner in programming cannot really understand advanced instructions. When this code is compiled the following error is produced
main.cpp: In function 'int main()':
main.cpp:11:2: error: 'thread' was not declared in this scope
main.cpp:11:9: error: expected ';' before 'first'
main.cpp:12:9: error: expected ';' before 'second'
main.cpp:14:2: error: 'first' was not declared in this scope
main.cpp:15:2: error: 'second' was not declared in this scope
I have already included -std=c++11 in the c++ compiler additional command-line options in the project option of the Dev C++ but i can't still remove the errors. Can you please check what i am doing wrong? also as much as possible i dont want to start using other libraries as i am having a hard time building them(ex. boost)

The problem is most likely due to lack of support for C++11's std::thread in the build of GCC 4.7.1 included with TDM-GCC. Have a look at this question, for details. Your code compiles fine with GCC 4.8.1 (although it still has runtime errors):
http://ideone.com/oUhvi3
I would therefore suggest that to resolve your problem, you try updating to a more recent version of the compiler. According to this link and this link doing so should be a simple matter of installing the most recent version of the compiler into the folder where it currently resides, or alternatively, installing it in a new folder and updating the settings in Dev C++ to point to the new compiler.
However, since you are new to C++ (and programming in general), and therefore have no particular attachment to Dev C++, I would recommend that you instead switch to a more modern and widely used IDE. MS Visual Studio is a good bet for Windows, but there are plenty of open source and cross platform IDEs available for C++. Using an IDE which is popular is recommended for beginners, as you are much more likely to find sources of help and support online, and more likely to get answers on sites such as Stackoverflow, when you get stuck. There are tons of Stackoverflow questions relating to IDEs. Examples (from a quick Google search):
What is the good cross platform C++ IDE?
Best C++ IDE or Editor for Windows
https://stackoverflow.com/questions/535369/what-is-the-best-free-windows-c-ide-compiler

Related

error: '_Out_' has not been declared when including <Kinect.h>

I'm using CLion and I need to get some data from the Kinect, but when it comes to #include <Kinect.h> I get the following error:
In file included from C:/PROGRA~1/MICROS~2/Kinect/V2D305~1.0_1/inc/Kinect.h:547,
from C:\Users\fredd\CLionProjects\3D_avatar\main.cpp:19:
C:/PROGRA~1/MICROS~2/Kinect/V2D305~1.0_1/inc/Kinect.INPC.h:109:13: error: '_Out_' has not been declared
_Out_ WAITABLE_HANDLE *waitableHandle) = 0;
^~~~~
And an endless list of similar errors. I think I miss some kind of library that defines this data format, but I really don't know (if that's the problem) which one. I've searched all over the internet and googled for about 3 hours without any result.
How can I solve the error?
Also, is there a better way to make Kinect works with CLion?
I've managed to understand what was going on: MinGW couldn't compile SAL annotations, so I had to switch compiler to MSVC, bundled with Visual Studio. Also, I had to link MatLab with my project since there's no library (except for libfreenect2, which has driven me mad) that can handle a Kinect v2.

How to use C++ Expects operator?

I'm starting a project using C++, which I haven't used before outside of a handful of school projects - nowhere near the scope of what I'm tackling now.
My goal is to try my best to follow the C++ Core Guidelines as I work to avoid errors, improve performance, and most importantly: improve maintainability of my code.
I've been running into literally hundreds of issues ranging from my g++ / Clang++ versions not being right to standard libraries not being found to g++ using the wrong version of C++ for compilation to very basic functions not behaving as expected - and I haven't even started to look into autotools, so I expect many more headaches to follow.
This question is specific to one part of the C++ Core Guidelines, though. Interfaces 6: Prefer Expects() for expressing preconditions
I tried writing the following simple code:
#include <iostream>
using namespace std;
int square(int x) {
Expects(x > 0);
return x * x;
}
int main() {
cout << square(3) << endl;
return 0;
}
This threw an error in g++:
$> g++ -std=c++17 main.cpp
main.cpp: In function ‘int square(int)’:
main.cpp:7:2: error: ‘Expects’ was not declared in this scope
Expects(x > 0);
^~~~~~~
-> [1]
I tried using Clang, as well, but it has an entirely different (and unrelated) problem:
$> clang++ -x c++ main.cpp
main.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
^~~~~~~~~~
1 error generated.
-> [1]
I haven't figured out how to fix that one yet, so I'm not bothering with it.
Expects is part of the GSL library. You have to use some GSL library implementation, which you can find on Github:
https://github.com/martinmoene/gsl-lite
https://github.com/Microsoft/GSL
These are the ones I have off the top of my head.
The CPP Guidelines likely allude to the "contracts" proposal which provides the same checks via attributes. It was scheduled for C++20, but later removed for lack of consensus on its scope. See p1823r0 and a standard committee member's Reddit thread on the rationale leading to the removal.
Apart from GSL, Excepts exists also in C++20 not in C++17 with a little different syntax
https://en.cppreference.com/w/cpp/language/attributes/contract

C++ Threads not working, "error: 'thread' is not a member of 'std'"

I'm going to keep it fast and simple.
Here's the code I've written:
#include <iostream>
#include <thread>
void myFunc()
{
std::cout << "Hello!" << std::endl;
}
int main()
{
std::thread myThread(myFunc);
myThread.join();
return 0;
}
This is the command I've used to compile the program:
g++ threads.cc -o threads.exe -std=c++11
This is the error I'm getting:
threads.cc: In function 'int main()':
threads.cc:11:6: error: 'thread' is not a member of 'std'
std::thread myThread(myFunc);
^
threads.cc:13:6: error: 'myThread' was not declared in this scope
myThread.join();
^
I'm running Windows 10 and as you can see I'm trying to compile the program with GCC.
Can anyone help me with this? I've tried a few different ways to do multi-threading in C++, but none have worked.
The day after tomorrow I'll attend to a programming competition at my school and to make things go faster (the programs may only take up to 5 seconds to process data, which can be a lot) I'm thinking multi-threading could really help. You might think I should know this if I learn programming at school, but I haven't started that course yet and I'm just interested in the competition because it seems fun.
EDIT:
I have now installed the packages "mingw32-libpthreadgc" and "mingw32-libpthreadgce" (both the dev and dll classes). I also installed the "mingw32-pthreads-w32" (dev, doc and lic classes).
I have tried a few different includes <thread>, <pthread>, <pthread.h>. I have also tried adding the flag "-pthread" and "-lpthread". Also, I don't think we'll be allowed to use those flags anyways.
Nothing has worked so far.
I would be awesome if anyone could give a concrete example of which package / packages must be installed, which file / files to include and a short example code (in case mine won't work). We're not allowed to use any libraries except for the standard ones, but running a linux vm is probably fine, so any linux-only example works too.
I haven't found an example like that anywhere yet, so that would be awesome! Thanks!
(Yes, I did restart my computer)
When you install mingw sepect posix threads instead of default

How to make sure code (c++) written in Xcode can compile on other platforms?

I am a beginner was trying to do some C++ programming on Xcode. It works fine, but when I try to compile the same c++ file on my windows pc using VS, there were some errors. After I look at my code closely, there are really some stupid mistakes that I have made which caused the errors, but Xcode seemed to have ignored them...
My question is that is there any setting that I need to change to prevent Xcode from being so smart?
For example, the following code can actually compile in xcode:
#include <iostream>
using namespace std;
int main() {
if (true or false){
cout << "How is this possible? \n";
}
return 0;
}
There are also other cases where the code is actually wrong, but it can compile just fine is Xcode which is the annoying part and I want to disable that.
As far as I can see there is nothing wrong with your code.
The ISO C++ standard does not specify which standard headers are included by other standard headers. So, it is entirely possible that the version of iostream used by Xcode directly or indirectly includes ciso646. Whereas Visual Studio's version of iostream does not include ciso646. There are many similar cases with other headers. You just need to read the error messages and realize that your error (when you move your file to a different platform) is due to a missing header file.
It would be nice if writing portable code meant writing code in accordance with the C++ standard specification, but unfortunately that's not the case. Although there are various compiler options on various implementations which can help bring different implementations closer together, in general you will just have to bring the code into the target environment and actually test it there.
So ultimately writing portable code means you'll have to learn some subset of C++ that is accepted by all the implementations you want to target.
or is an 'alternative token' in C++, and VS is incorrect to reject it. There's no option in Xcode to disable support for alternative tokens. However VS has non-standard support for or as a macro using the header <ciso646>, and Xcode does have a header <ciso646> which does nothing (as the standard specifies). So you can write code which uses or and which works in both Xcode and VS by including this header.
#include <iostream>
#include <ciso646> // does nothing in Xcode, allows `or` in VS
using namespace std;
int main() {
if (true or false){
cout << "How is this possible? \n";
}
return 0;
}
Unfortunately VS can't support all of the alternative tokens through macros and so Xcode will still support some that VS doesn't.
There are also other cases where the code is actually wrong, but it can compile just fine is Xcode which is the annoying part and I want to disable that.
If you give specific examples then I can provide additional advice on how to write portable code.
Rather than changing your Xcode settings, I suggest cross-checking your code using another development environment.
If you're looking for something cheap and full-proof. Download a VirtualBox Windows VM, and run download Dev C++ (bloodhshed)
VS does not support or: you need to use || instead.
You can include some special files but it doesn't inject or sufficiently well into the language for it to work in all instances.
If you want to suppress use of or (and your compiler supports no better way)
#define it to something that emits a compiler error, for example
#define or OR
This at least means that the nature of the compilation errors will be identical on Xcode and VC.

Compile in c++14

So in my CSE course we are given a header file to use right now for our programs that we're writing.
Unfortunately I can't get terminal to compile using that header, it gives quite a few errors (compiling with just 'g++'). Also, when I'm at my university and I'm using PuTTY I get the same errors while using this header. However, I don't get the errors when I compile with 'g++ -std=c++14'.
I've tried compiling with this command on terminal on my mac, but it says it doesn't recognize the c++14 part.
dhcp-10-202-147-243:hw1pr1 Admin$ g++ -std=c++14 hw1pr1.cpp
error: invalid value 'c++14' in '-std=c++14'
Any help on how I could get this to work would be greatly appreciated. Hopefully this all made some sort of sense.
Here's the error I get when I compile with the header file I'm talking about in terminal with just g++.
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/ext/hash_map:212:5: warning:
Use of the header <ext/hash_map> is deprecated. Migrate to <unordered_map>
[-W#warnings]
# warning Use of the header <ext/hash_map> is deprecated. Migrate to ...
^
In file included from read_first_name.cpp:1:
./std_lib_facilities_4.h:43:20: error: no matching function for call to object
of type 'hash<char *>'
return hash<char*>()(s.c_str());
^~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/ext/__hash:39:12: note:
candidate function not viable: 1st argument ('const value_type *'
(aka 'const char *')) would lose const qualifier
size_t operator()(char *__c) const _NOEXCEPT
^
In file included from read_first_name.cpp:1:
./std_lib_facilities_4.h:112:8: warning: comparison of unsigned expression < 0
is always false [-Wtautological-compare]
if (i<0||size()<=i) throw Range_error(i);
~^~
./std_lib_facilities_4.h:118:8: warning: comparison of unsigned expression < 0
is always false [-Wtautological-compare]
if (i<0||size()<=i) throw Range_error(i);
~^~
3 warnings and 1 error generated.
This error doesn't happen and the program will compile fully when I use PuTTY and 'g++ std=c++14'
There's lots of change between C++ standards, so what is valid in one revision need not be in another.
g++ defaults to -std=gnu++98 for C++, which is the decades old C++98-standard enhanced with GNU extensions (most of which are conformant).
Choose the proper revision: -std=c++1y -pedantic is a very close approximation to C++14.
What changes introduced in C++14 can potentially break a program written in C++11?
Looking at what you say you're having to use and the name format of that .cpp file, I think I'm in the same class. A year later, looks like, but here's my solution for archive's sake:
The std_lib_facilities.h header comes with the Bjarne Stroustrup textbook, "Programming: Principles and Practices Using C++". For those unaware, Bjarne Stroustrup invented C++ (he has a pretty good idea what he's talking about). Incidentally, the book is a fantastic way to learn C++, if one takes the time to actually read it. The std_lib_facilities.h header is just a convenient header file for beginners in C++, containing links to all the major standard libraries used in the textbook, as well as some helper functions that help account for potential mistakes or errors, or are just convenient for learning (such as an error() function that handles simple exception throwing for the student, or adding an "out of bounds" check for vectors). It's ultimately just a way to allow students to hop right into code without having to learn specifics about the header.
Stroustrup keeps updated with C++ and thus includes several libraries that require the c++11 standard. The CSCE department wants its students (at least in this early class) to connect to the department's Unix system and compile from there, in order to avoid confusion with downloading and updating compilers.
I happened to already have had a couple C++ classes beforehand, and thus already had g++ set up on my Ubuntu laptop. I avoided including the std_lib_facilities for as long as possible since I was getting the same error as Topic Creator Joe, where g++ didn't recognize the "c++11" part (manually including the required libraries worked fine until we had to use a class from the textbook that used one of the header's helper functions) . Eventually, I found a help topic online that advised me simply to update my g++ compiler to 4.7 or higher, since 4.6 and lower doesn't have support for C++11 (or, of course, C++14). It was oddly rather involved compared to updates one might be used to on Mac or Windows, and I doubt the exact process would apply, but that is (was?) likely the problem: it's just an older version of g++, and it needs an update to compile C++11 and later. I recommend searching for ways to update g++/gcc for Mac.
Should, y'know, anyone else with this problem stumble upon this and not have their problem solved yet.