pdeitel / cplusplus20forprogrammers Goto Github PK
View Code? Open in Web Editor NEWCode examples for our book _C++ 20 for Programmers_ (Pearson, 2020)
Code examples for our book _C++ 20 for Programmers_ (Pearson, 2020)
Hi,
In C++ 20 Fundamentals: Running in a Jupyter Notebook following command is executed inside examples dir.
docker build --rm -t xeuscling .
This assumes there is a DockerFile on the examples directory. But currently there is none.
Please add the DockerFile or instruct how to create this in order to get this work.
Thanks.
Hi there,
In my opinion, VS C++ does not fully qualify the iterator's type name due to I verify the below statement true.
std::cout << std::is_base_of_v<std::random_access_iterator_tag, std::array<int, 5>::iterator::iterator_category>; // 1(True)
I couldn't find any workaround yet.
Any idea or suggestion will be welcome.
Regards.
-----snip-----
Build started at 9:54 AM...
1>------ Build started: Project: cpp20_test, Configuration: Debug x64 ------
1>Source.cpp
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(15,24): error C2923: 'std::is_base_of_v': 'std::_Array_const_iterator<_Ty,5>::iterator_category' is not a valid template type argument for parameter '_Derived'
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(15,24): error C2923: with
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(15,24): error C2923: [
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(15,24): error C2923: _Ty=int
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(15,24): error C2923: ]
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(16,19):
1>see declaration of 'std::_Array_const_iterator<_Ty,5>::iterator_category'
1> with
1> [
1> _Ty=int
1> ]
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(15,24):
1>the template instantiation context (the oldest one first) is
1> C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(39,19):
1> see reference to function template instantiation 'auto customDistance<std::_Array_const_iterator<_Ty,5>>(Iterator,Iterator)' being compiled
1> with
1> [
1> _Ty=int,
1> Iterator=std::_Array_const_iterator<int,5>
1> ]
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(16,19): error C2275: 'std::_Array_const_iterator<_Ty,5>::iterator_category': expected an expression instead of a type
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(16,19): error C2275: with
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(16,19): error C2275: [
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(16,19): error C2275: _Ty=int
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(16,19): error C2275: ]
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(16,37): error C2059: syntax error: ')'
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(16,39): error C2143: syntax error: missing ';' before '{'
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(13,51): error C2181: illegal else without matching if
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(15,24): error C2923: 'std::is_base_of_v': 'std::_List_const_iterator<std::_List_val<std::_List_simple_types<_Ty>>>::iterator_category' is not a valid template type argument for parameter '_Derived'
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(15,24): error C2923: with
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(15,24): error C2923: [
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(15,24): error C2923: _Ty=int
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(15,24): error C2923: ]
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(16,19):
1>see declaration of 'std::_List_const_iterator<std::_List_val<std::_List_simple_types<_Ty>>>::iterator_category'
1> with
1> [
1> _Ty=int
1> ]
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(15,24):
1>the template instantiation context (the oldest one first) is
1> C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(41,19):
1> see reference to function template instantiation 'auto customDistance<std::_List_const_iterator<std::_List_val<std::_List_simple_types<_Ty>>>>(Iterator,Iterator)' being compiled
1> with
1> [
1> _Ty=int,
1> Iterator=std::_List_const_iterator<std::_List_val<std::_List_simple_types>>
1> ]
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(16,19): error C2275: 'std::_List_const_iterator<std::_List_val<std::_List_simple_types<_Ty>>>::iterator_category': expected an expression instead of a type
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(16,19): error C2275: with
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(16,19): error C2275: [
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(16,19): error C2275: _Ty=int
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(16,19): error C2275: ]
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(18,20): error C2676: binary '-': 'Iterator' does not define this operator or a conversion to a type acceptable to the predefined operator
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(18,20): error C2676: with
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(18,20): error C2676: [
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(18,20): error C2676: Iterator=std::_List_const_iterator<std::_List_val<std::_List_simple_types>>
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(18,20): error C2676: ]
1>C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.38.33130\include\xutility(1775,5):
1>could be 'unknown-type std::operator -(const std::reverse_iterator<_BidIt> &,const std::reverse_iterator<_BidIt2> &) noexcept()'
1> C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(18,16):
1> 'unknown-type std::operator -(const std::reverse_iterator<_BidIt> &,const std::reverse_iterator<_BidIt2> &) noexcept()': could not deduce template argument for 'const std::reverse_iterator<_BidIt> &' from 'Iterator'
1> with
1> [
1> Iterator=std::_List_const_iterator<std::_List_val<std::_List_simple_types>>
1> ]
1>C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.38.33130\include\xutility(4326,30):
1>or 'unknown-type std::operator -(const std::move_iterator<_Iter> &,const std::move_iterator<_Iter2> &) noexcept()'
1> C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(18,16):
1> 'unknown-type std::operator -(const std::move_iterator<_Iter> &,const std::move_iterator<_Iter2> &) noexcept()': could not deduce template argument for 'const std::move_iterator<_Iter> &' from 'Iterator'
1> with
1> [
1> Iterator=std::_List_const_iterator<std::_List_val<std::_List_simple_types>>
1> ]
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(20,5): error C2059: syntax error: 'else'
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(26,9): error C2059: syntax error: 'for'
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(13,51): error C2143: syntax error: missing ')' before ';'
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(26,39): error C2143: syntax error: missing ';' before '!='
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(13,51): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(26,47): error C2059: syntax error: '++'
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(26,53): error C2059: syntax error: ')'
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(28,9): error C2059: syntax error: '}'
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(31,5): error C2059: syntax error: '}'
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(41,62): error C2143: syntax error: missing ';' before '}'
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(41,62): error C2059: syntax error: '}'
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(42,15): error C2143: syntax error: missing ';' before '<<'
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(42,15): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(42,10): error C2371: 'std::cout': redefinition; different basic types
1>C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.38.33130\include\iostream(39,75):
1>see declaration of 'std::cout'
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(43,1): error C2059: syntax error: '}'
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(43,1): error C2143: syntax error: missing ';' before '}'
1>C:\Users\jmche\source\repos\Project1\cpp20_test\Source.cpp(60,1): error C1903: unable to recover from previous error(s); stopping compilation
1>Done building project "cpp20_test.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 69 up-to-date, 0 skipped ==========
========== Build completed at 9:54 AM and took 08.896 seconds ==========
friend void setX(Count& c, int value); // line 9
I think should be
friend void modifyX(Count& c, int value); // line 9
Thanks for the amazing book, has been an excellent learning resource so far.
Congratulations with the book!
I did not read it, I am looking for one for teaching, hence I am trying to compile blindly. Got stuck with an error:
examples/ch04/fig04_11.cpp:5:10: fatal error: zip_file.hpp: No such file or directory
5 | #include "zip_file.hpp"
zip_file.hpp
is not in the repo, where can we get it?
I hastily came up with CMake script to compile it (something like this would be nice to have in the repo):
cmake_minimum_required(VERSION 3.15)
project(CXX20ForProgrammers LANGUAGES CXX)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # for clang-tidy
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_EXTENSIONS OFF)
add_subdirectory("examples/libraries/fmt")
add_subdirectory("examples/libraries/GSL")
file(GLOB sources "${CMAKE_CURRENT_SOURCE_DIR}/examples/ch*/*.cpp")
foreach(source ${sources})
file(STRINGS ${source} hasmain REGEX ".+\ main\(.+\).+")
if (hasmain)
get_filename_component(name "${source}" NAME_WLE)
if (NOT ${name} STREQUAL "fig04_07_with_error")
add_executable(${name} ${source})
file(STRINGS ${source} usesfmt REGEX ".+fmt/format.+")
if (usesfmt)
target_include_directories(${name} PRIVATE "examples/libraries/fmt/include")
target_link_libraries(${name} PRIVATE fmt)
endif()
file(STRINGS ${source} usesgsl REGEX ".+gsl/gsl.+")
if (usesgsl)
target_include_directories(${name} PRIVATE "examples/libraries/GSL/include")
endif()
endif()
endif(hasmain)
endforeach()
Then used the usual cmake
dance:
cmake -S . -B build -G Ninja
cmake --build build
In the folder "CPlusPlus20ForProgrammers/examples/ch05/"
"fig15_20.cpp" should be "fig05_20.cpp"
Thx.
I'm reviewing this textbook for use in my C++ class (previously I used C++ How to Program 10th ed). If I fork this repo to my own public repo, do you consider that to be "reposting", which requires written permission?
line 20: randomInt(engine)
expects 2 values only 1 was provided?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.