Unable to Resolve Identifier constexpr - c++

I'm teaching myself C++ with the textbook 'Programming -- Principles and Practice Using C++ (Second Edition)' and have come across a problem while trying to run an example problem. The line I'm supposed to input is
constexpr int max = 17;
but I am getting an error: "Unable to resolve identifier constexpr", but I have no idea why because I have the necessary header file (specific to the textbook, downloaded from www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h ) All the other programs I've tried have worked just fine...
Other useful info:
-Using Netbeans (C++ Version)
-Using Windows 8
-Using Cygwin
-Not an expert.

constexpr is a new feature of C++11. You need a C++11 capable compiler (like g++4.8) and have to enable the C++11 extensions when you compile a program:
g++ -std=c++11 main.cpp -o test

Related

How to specify c++ 14/17 with bazel.rc (tool specific to using GCC /G++, project not open to other tool chains)

Within my bazel.rc file for the C++ Bazel build options, I have specified:
'build --cxxopt="-std=c++1y"'
When I try to change to build --cxxopt="-std=c++14", bazel Throws error of :
''build --cxxopt="-std=c++14"' is not an option'
, with a cursor below 4 in c++14. If I change the 4 to y (c++1y), it compiles with no problem. I tried adding 'std:make_unique' in my code (c++14 addition) , and it clearly does not compile (make _unique is not part of std) which is assume is complaining that c++14 is not set as the standard, hence no make_unique for me.
What is the latest supported version of C++ in bazel? More specifically, how do I enable C++14 / C++17 (and even C++2x) for Bazel build, it those are supported? Thanks!
NOTE: this is not the same as How to set C++ standard version when build with Bazel??. (I am not asking about cpp 11 and I am asking about a very specific toolchain -- GCC/g++) I am not using any of the tools suggested by that thread. I am using GCC / g++ and am restricted in not being able to use the tools suggested by the answer in the previous question, I've RTFMd and googled. Thanks in advance.

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

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.

Dev C++ simple threading program

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

How can I use sqlite3.c in a c++ project?

I am attempting to use sqlite3 in a C++ project in Eclipse and have found a great deal of advice online on using the API, but unfortunately am falling at an earlier hurdle. I guess this is due to my lack of experience with C/C++ and CDT.
I've simply copied sqlite3.c and sqlite3.h into the project's source folder and have a test method which is as follows:
int main() {
sqlite3* db;
sqlite3** dbpointer = &db;
const char* dbname = "test.db";
sqlite3_open(dbname, dbpointer);
return 0;
}
However, the sqlite3.c file shows up in Eclipse with numerous errors. For example, the following section is annotated with 'Field 'IN_DECLARE_VTAB' could not be resolved'.
#ifdef SQLITE_OMIT_VIRTUALTABLE
#define IN_DECLARE_VTAB 0
#else
#define IN_DECLARE_VTAB (pParse->declareVtab)
#endif
When I try to compile I get a series of errors like:
gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/sqlite3.d" -MT"src/sqlite3.d" -o "src/sqlite3.o" "../src/sqlite3.c"
../src/sqlite3.c:30997: error: initializer element is not constant
../src/sqlite3.c:30997: error: (near initialization for `aSyscall[0].pCurrent')
../src/sqlite3.c:30997: error: initializer element is not constant
../src/sqlite3.c:30997: error: (near initialization for `aSyscall[0]')
../src/sqlite3.c:31009: error: initializer element is not constant
../src/sqlite3.c:31009: error: (near initialization for `aSyscall[1]')
../src/sqlite3.c:31017: error: initializer element is not constant
../src/sqlite3.c:31017: error: (near initialization for `aSyscall[2]')
I did find a similar question here, but it doesn't appear to have been resolved there either.
I suspect this is a set-up issue with Eclipse, so if anyone could give me any advice or directions to useful tutorials I'd really appreciate it. And if I'd be better off posting this to a dedicated sqlite forum just let me know.
SQLite is written in C, and there are a number of differences between C and C++. Not huge numbers, but they're definitely not the same and neither is a superset of the other. Because you are using a single Eclipse project, you've probably ended up trying to compile C code with a C++ compiler, and are therefore coming unstuck on these small differences.
You are advised to build sqlite3.c into a separate library (it can be a static library or a dynamic one; your call) as a C project, and then make your C++ project just use that C project as a dependency. Or you can build it once and just have it as an external dependency; that'll work too. (To be fair, it's an external dependency; you shouldn't really embed it wholesale into your code anyway as that will make tracking bugfixes harder. Keeping it separate — at least for build, even if not for distribution — will make your life much easier.)
Have you try in this way? (with double pointer):
int main() {
sqlite3* db;
const char* dbname = "test.db";
sqlite3_open(dbname, &db);
return 0;
}
I suppose you're working on linux.
Another approach is to execute a script:
int main() {
system("connectDB.sh");
/* connectDB.sh should be chmod +x */
}
Your file connectDB will be:
#!/bin/bash
sqlite3 test.db "select * from test.table"