C++程序设计(第4版)读书笔记_C++概览:基础知识
变量赋值
常用的变量赋值都是用“=”去赋值的
int i = ;
但是如果把一个浮点数赋值给i的话,就会造成精度损失,在C++中最好使用初始化列表的方式“{}”给变量赋值,这样可以保证不会发生某些可能导致信息丢失的类型转换
#include <iostream>
using namespace std; int main() {
int i {2.3};
return ;
}
比如这样声明,编译器就会报错
#include <iostream>
using namespace std; int main() {
int v[] = {, , , , }; for (auto x : v) {
cout << x << "\t";
}
cout << endl; for (auto i = ; i < sizeof(v) / sizeof(int); ++i) {
cout << v[i] << "\t";
}
cout << endl; for (auto x : {, , , , }) {
cout << x << "\t";
}
cout << endl; for (auto x : v) {
x += ;
}
for (auto x : v) {
cout << x << "\t";
}
cout << endl; /* 对于不带引用的情况,可以理解为对于v的每个元素将其从头到尾依次放入x并打印 */
for (auto & x : v) {
x += ;
}
for (auto x : v) {
cout << x << "\t";
}
cout << endl; return ;
}
运行结果如下:
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
1 2 3 4 5
update 2017-11-02:范围for循环对指针无效,因为我们不知道数组的大小了
结构
在结构里面我们先完成一个最简单的vector,再下面介绍的类中我们可以看到一些优化的部分,可以看到类的操作会更加简单,使用者根本就无需关注里面的实现。
先来看结构实现的代码吧
struct Vector
{
int sz;
double * elem;
}; void vector_init(Vector & v, int s)
{
v.elem = new double[s];
v.sz = s;
} double read_and_sum(int s)
{
Vector v;
vector_init(v, s); for (int i = ; i != s; ++i) {
cin >> v.elem[i];
} double sum = ;
for (int i = ; i != s; ++i) {
sum += v.elem[i];
} return sum;
} int main()
{
cout << read_and_sum() << endl;
return ;
}
运行结果:
类
#include <iostream>
#include <new> using namespace std; class Vector
{
public:
/*
C++11这样用
低版本的话就是 Vector(int s) : elem(new double[s]), sz(s) {}
*/
Vector(int s) : elem{new double[s]}, sz{s}
{ } double & operator[](int i)
{
return elem[i];
} int size()
{
return sz;
} private:
double * elem;
int sz;
}; double read_and_sum(int s)
{
Vector v(s); for (int i = ; i != v.size(); ++i) {
cin >> v[i];
} double sum = ;
for (int i = ; i != v.size(); ++i) {
sum += v[i];
} return sum;
} int main()
{
cout << read_and_sum() << endl;
return ;
}
运行结果:
枚举
#include <iostream>
#include <new> using namespace std; enum class Traffic_light
{
green,
yellow,
red
}; Traffic_light & operator++(Traffic_light & t)
{
switch(t)
{
case Traffic_light::green:
return t = Traffic_light::yellow;
case Traffic_light::yellow:
return t = Traffic_light::red;
case Traffic_light::red:
return t = Traffic_light::green;
}
} int main()
{
Traffic_light src = Traffic_light::green; for (int i = ; i < ; ++i) {
Traffic_light next = ++src;
cout << next << endl;
} return ;
}
在编译的时候出错了:
main.cpp: In function 'int main()':
main.cpp:33:8: error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'Traffic_light')
cout << next << endl;
~~~~~^~~~~~~
In file included from /usr/local/include/c++/7.2.0/iostream:39:0,
from main.cpp:1:
/usr/local/include/c++/7.2.0/ostream:108:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ostream_type& (*)(std::basic_ostream<_CharT, _Traits>::__ostream_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(__ostream_type& (*__pf)(__ostream_type&))
^~~~~~~~
/usr/local/include/c++/7.2.0/ostream:108:7: note: no known conversion for argument 1 from 'Traffic_light' to 'std::basic_ostream<char>::__ostream_type& (*)(std::basic_ostream<char>::__ostream_type&) {aka std::basic_ostream<char>& (*)(std::basic_ostream<char>&)}'
/usr/local/include/c++/7.2.0/ostream:117:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ios_type& (*)(std::basic_ostream<_CharT, _Traits>::__ios_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>; std::basic_ostream<_CharT, _Traits>::__ios_type = std::basic_ios<char>]
operator<<(__ios_type& (*__pf)(__ios_type&))
^~~~~~~~
/usr/local/include/c++/7.2.0/ostream:117:7: note: no known conversion for argument 1 from 'Traffic_light' to 'std::basic_ostream<char>::__ios_type& (*)(std::basic_ostream<char>::__ios_type&) {aka std::basic_ios<char>& (*)(std::basic_ios<char>&)}'
/usr/local/include/c++/7.2.0/ostream:127:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(ios_base& (*__pf) (ios_base&))
^~~~~~~~
/usr/local/include/c++/7.2.0/ostream:127:7: note: no known conversion for argument 1 from 'Traffic_light' to 'std::ios_base& (*)(std::ios_base&)'
/usr/local/include/c++/7.2.0/ostream:166:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(long __n)
^~~~~~~~
/usr/local/include/c++/7.2.0/ostream:166:7: note: no known conversion for argument 1 from 'Traffic_light' to 'long int'
/usr/local/include/c++/7.2.0/ostream:170:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(unsigned long __n)
^~~~~~~~
/usr/local/include/c++/7.2.0/ostream:170:7: note: no known conversion for argument 1 from 'Traffic_light' to 'long unsigned int'
/usr/local/include/c++/7.2.0/ostream:174:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(bool __n)
^~~~~~~~
/usr/local/include/c++/7.2.0/ostream:174:7: note: no known conversion for argument 1 from 'Traffic_light' to 'bool'
In file included from /usr/local/include/c++/7.2.0/ostream:693:0,
from /usr/local/include/c++/7.2.0/iostream:39,
from main.cpp:1:
/usr/local/include/c++/7.2.0/bits/ostream.tcc:91:5: note: candidate: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char; _Traits = std::char_traits<char>]
basic_ostream<_CharT, _Traits>::
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/local/include/c++/7.2.0/bits/ostream.tcc:91:5: note: no known conversion for argument 1 from 'Traffic_light' to 'short int'
In file included from /usr/local/include/c++/7.2.0/iostream:39:0,
from main.cpp:1:
/usr/local/include/c++/7.2.0/ostream:181:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(unsigned short __n)
^~~~~~~~
/usr/local/include/c++/7.2.0/ostream:181:7: note: no known conversion for argument 1 from 'Traffic_light' to 'short unsigned int'
In file included from /usr/local/include/c++/7.2.0/ostream:693:0,
from /usr/local/include/c++/7.2.0/iostream:39,
from main.cpp:1:
/usr/local/include/c++/7.2.0/bits/ostream.tcc:105:5: note: candidate: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char; _Traits = std::char_traits<char>]
basic_ostream<_CharT, _Traits>::
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/local/include/c++/7.2.0/bits/ostream.tcc:105:5: note: no known conversion for argument 1 from 'Traffic_light' to 'int'
In file included from /usr/local/include/c++/7.2.0/iostream:39:0,
from main.cpp:1:
/usr/local/include/c++/7.2.0/ostream:192:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(unsigned int __n)
^~~~~~~~
/usr/local/include/c++/7.2.0/ostream:192:7: note: no known conversion for argument 1 from 'Traffic_light' to 'unsigned int'
/usr/local/include/c++/7.2.0/ostream:201:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(long long __n)
^~~~~~~~
/usr/local/include/c++/7.2.0/ostream:201:7: note: no known conversion for argument 1 from 'Traffic_light' to 'long long int'
/usr/local/include/c++/7.2.0/ostream:205:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(unsigned long long __n)
^~~~~~~~
/usr/local/include/c++/7.2.0/ostream:205:7: note: no known conversion for argument 1 from 'Traffic_light' to 'long long unsigned int'
/usr/local/include/c++/7.2.0/ostream:220:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(double __f)
^~~~~~~~
/usr/local/include/c++/7.2.0/ostream:220:7: note: no known conversion for argument 1 from 'Traffic_light' to 'double'
/usr/local/include/c++/7.2.0/ostream:224:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(float __f)
^~~~~~~~
/usr/local/include/c++/7.2.0/ostream:224:7: note: no known conversion for argument 1 from 'Traffic_light' to 'float'
/usr/local/include/c++/7.2.0/ostream:232:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(long double __f)
^~~~~~~~
/usr/local/include/c++/7.2.0/ostream:232:7: note: no known conversion for argument 1 from 'Traffic_light' to 'long double'
/usr/local/include/c++/7.2.0/ostream:245:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(const void* __p)
^~~~~~~~
/usr/local/include/c++/7.2.0/ostream:245:7: note: no known conversion for argument 1 from 'Traffic_light' to 'const void*'
In file included from /usr/local/include/c++/7.2.0/ostream:693:0,
from /usr/local/include/c++/7.2.0/iostream:39,
from main.cpp:1:
/usr/local/include/c++/7.2.0/bits/ostream.tcc:119:5: note: candidate: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__streambuf_type*) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>]
basic_ostream<_CharT, _Traits>::
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/local/include/c++/7.2.0/bits/ostream.tcc:119:5: note: no known conversion for argument 1 from 'Traffic_light' to 'std::basic_ostream<char>::__streambuf_type* {aka std::basic_streambuf<char>*}'
In file included from /usr/local/include/c++/7.2.0/string:52:0,
from /usr/local/include/c++/7.2.0/bits/locale_classes.h:40,
from /usr/local/include/c++/7.2.0/bits/ios_base.h:41,
from /usr/local/include/c++/7.2.0/ios:42,
from /usr/local/include/c++/7.2.0/ostream:38,
from /usr/local/include/c++/7.2.0/iostream:39,
from main.cpp:1:
/usr/local/include/c++/7.2.0/bits/basic_string.h:6082:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)
operator<<(basic_ostream<_CharT, _Traits>& __os,
^~~~~~~~
/usr/local/include/c++/7.2.0/bits/basic_string.h:6082:5: note: template argument deduction/substitution failed:
main.cpp:33:11: note: mismatched types 'const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>' and 'Traffic_light'
cout << next << endl;
^~~~
In file included from /usr/local/include/c++/7.2.0/bits/ios_base.h:46:0,
from /usr/local/include/c++/7.2.0/ios:42,
from /usr/local/include/c++/7.2.0/ostream:38,
from /usr/local/include/c++/7.2.0/iostream:39,
from main.cpp:1:
/usr/local/include/c++/7.2.0/system_error:217:5: note: candidate: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::error_code&)
operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __e)
^~~~~~~~
/usr/local/include/c++/7.2.0/system_error:217:5: note: template argument deduction/substitution failed:
main.cpp:33:11: note: cannot convert 'next' (type 'Traffic_light') to type 'const std::error_code&'
cout << next << endl;
^~~~
In file included from /usr/local/include/c++/7.2.0/iostream:39:0,
from main.cpp:1:
/usr/local/include/c++/7.2.0/ostream:497:5: note: candidate: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, _CharT)
operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
^~~~~~~~
/usr/local/include/c++/7.2.0/ostream:497:5: note: template argument deduction/substitution failed:
main.cpp:33:11: note: deduced conflicting types for parameter '_CharT' ('char' and 'Traffic_light')
cout << next << endl;
^~~~
In file included from /usr/local/include/c++/7.2.0/iostream:39:0,
from main.cpp:1:
/usr/local/include/c++/7.2.0/ostream:502:5: note: candidate: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, char)
operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
^~~~~~~~
/usr/local/include/c++/7.2.0/ostream:502:5: note: template argument deduction/substitution failed:
main.cpp:33:11: note: cannot convert 'next' (type 'Traffic_light') to type 'char'
cout << next << endl;
^~~~
In file included from /usr/local/include/c++/7.2.0/iostream:39:0,
from main.cpp:1:
/usr/local/include/c++/7.2.0/ostream:508:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, char)
operator<<(basic_ostream<char, _Traits>& __out, char __c)
^~~~~~~~
/usr/local/include/c++/7.2.0/ostream:508:5: note: template argument deduction/substitution failed:
main.cpp:33:11: note: cannot convert 'next' (type 'Traffic_light') to type 'char'
cout << next << endl;
^~~~
In file included from /usr/local/include/c++/7.2.0/iostream:39:0,
from main.cpp:1:
/usr/local/include/c++/7.2.0/ostream:514:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, signed char)
operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
^~~~~~~~
/usr/local/include/c++/7.2.0/ostream:514:5: note: template argument deduction/substitution failed:
main.cpp:33:11: note: cannot convert 'next' (type 'Traffic_light') to type 'signed char'
cout << next << endl;
^~~~
In file included from /usr/local/include/c++/7.2.0/iostream:39:0,
from main.cpp:1:
/usr/local/include/c++/7.2.0/ostream:519:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, unsigned char)
operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
^~~~~~~~
/usr/local/include/c++/7.2.0/ostream:519:5: note: template argument deduction/substitution failed:
main.cpp:33:11: note: cannot convert 'next' (type 'Traffic_light') to type 'unsigned char'
cout << next << endl;
^~~~
In file included from /usr/local/include/c++/7.2.0/iostream:39:0,
from main.cpp:1:
/usr/local/include/c++/7.2.0/ostream:539:5: note: candidate: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const _CharT*)
operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
^~~~~~~~
/usr/local/include/c++/7.2.0/ostream:539:5: note: template argument deduction/substitution failed:
main.cpp:33:11: note: mismatched types 'const _CharT*' and 'Traffic_light'
cout << next << endl;
^~~~
In file included from /usr/local/include/c++/7.2.0/ostream:693:0,
from /usr/local/include/c++/7.2.0/iostream:39,
from main.cpp:1:
/usr/local/include/c++/7.2.0/bits/ostream.tcc:321:5: note: candidate: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const char*)
operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s)
^~~~~~~~
/usr/local/include/c++/7.2.0/bits/ostream.tcc:321:5: note: template argument deduction/substitution failed:
main.cpp:33:11: note: cannot convert 'next' (type 'Traffic_light') to type 'const char*'
cout << next << endl;
^~~~
In file included from /usr/local/include/c++/7.2.0/iostream:39:0,
from main.cpp:1:
/usr/local/include/c++/7.2.0/ostream:556:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const char*)
operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
^~~~~~~~
/usr/local/include/c++/7.2.0/ostream:556:5: note: template argument deduction/substitution failed:
main.cpp:33:11: note: cannot convert 'next' (type 'Traffic_light') to type 'const char*'
cout << next << endl;
^~~~
In file included from /usr/local/include/c++/7.2.0/iostream:39:0,
from main.cpp:1:
/usr/local/include/c++/7.2.0/ostream:569:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const signed char*)
operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s)
^~~~~~~~
/usr/local/include/c++/7.2.0/ostream:569:5: note: template argument deduction/substitution failed:
main.cpp:33:11: note: cannot convert 'next' (type 'Traffic_light') to type 'const signed char*'
cout << next << endl;
^~~~
In file included from /usr/local/include/c++/7.2.0/iostream:39:0,
from main.cpp:1:
/usr/local/include/c++/7.2.0/ostream:574:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const unsigned char*)
operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s)
^~~~~~~~
/usr/local/include/c++/7.2.0/ostream:574:5: note: template argument deduction/substitution failed:
main.cpp:33:11: note: cannot convert 'next' (type 'Traffic_light') to type 'const unsigned char*'
cout << next << endl;
^~~~
In file included from /usr/local/include/c++/7.2.0/iostream:39:0,
from main.cpp:1:
/usr/local/include/c++/7.2.0/ostream:682:5: note: candidate: template<class _Ostream, class _Tp> typename std::enable_if<std::__and_<std::__not_<std::is_lvalue_reference<_Tp> >, std::__is_convertible_to_basic_ostream<_Ostream>, std::__is_insertable<typename std::__is_convertible_to_basic_ostream<_Tp>::__ostream_type, const _Tp&, void> >::value, typename std::__is_convertible_to_basic_ostream<_Tp>::__ostream_type>::type std::operator<<(_Ostream&&, const _Tp&)
operator<<(_Ostream&& __os, const _Tp& __x)
^~~~~~~~
/usr/local/include/c++/7.2.0/ostream:682:5: note: template argument deduction/substitution failed:
/usr/local/include/c++/7.2.0/ostream: In substitution of 'template<class _Ostream, class _Tp> typename std::enable_if<std::__and_<std::__not_<std::is_lvalue_reference<_Tp> >, std::__is_convertible_to_basic_ostream<_Ostream>, std::__is_insertable<typename std::__is_convertible_to_basic_ostream<_Tp>::__ostream_type, const _Tp&, void> >::value, typename std::__is_convertible_to_basic_ostream<_Tp>::__ostream_type>::type std::operator<<(_Ostream&&, const _Tp&) [with _Ostream = std::basic_ostream<char>&; _Tp = Traffic_light]':
main.cpp:33:11: required from here
/usr/local/include/c++/7.2.0/ostream:682:5: error: no type named 'type' in 'struct std::enable_if<false, std::basic_ostream<char>&>'
main.cpp: In function 'Traffic_light& operator++(Traffic_light&)':
main.cpp:25:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
#include <iostream>
#include <cassert> using namespace std; int main()
{
const int i = ; static_assert(i < , "5 is larger then 4"); return ; }
编译结果:
main.cpp: In function 'int main()':
main.cpp:10:9: error: static assertion failed: 5 is larger then 4
static_assert(i < 4, "5 is larger then 4");
^~~~~~~~~~~~~
如果上述代码中的i不为常量,那么编译就直接报错了
main.cpp: In function 'int main()':
main.cpp:10:5: error: non-constant condition for static assertion
static_assert(i < 4, "5 is larger then 4");
^~~~~~~~~~~~~
main.cpp:10:5: error: the value of 'i' is not usable in a constant expression
main.cpp:8:9: note: 'int i' is not const
int i = 5;
^
#include <iostream>
#include <cassert> using namespace std; int main()
{
static_assert(sizeof(void *) == , "64-bit code generation is not supported."); const int i = ; static_assert(i < , "5 is larger then 4"); return ; }
编译结果:
main.cpp: In function 'int main()':
main.cpp:8:5: error: static assertion failed: 64-bit code generation is not supported.
static_assert(sizeof(void *) == 4, "64-bit code generation is not supported.");
^~~~~~~~~~~~~
main.cpp:12:5: error: static assertion failed: 5 is larger then 4
static_assert(i < 4, "5 is larger then 4");
^~~~~~~~~~~~~
C++程序设计(第4版)读书笔记_C++概览:基础知识的更多相关文章
- JavaScript高级程序设计第三版-读书笔记(1-3章)
这是我第一次用markdown,也是我第一次在网上记录我自己的学习过程. 第一章 JavaScript主要由以下三个不同的部分构成 ECMAScript 提供核心语言功能 DOM 提供访问 ...
- 9Andrew.S.Tanenbaum计算机网络第三版读书笔记-总体概览
- 《Java并发编程实战》读书笔记-第一部分 基础知识小结
并发技巧清单 可变状态是至关重要的 所有的并发问题都可以归结为如何协调对并发状态的访问.可变状态越少,就越容易确保线程安全性. 尽量将域声明为final类型,除非需要它们是可变的. 不可变对象一定是线 ...
- Java 8实战之读书笔记二:基础知识
好记性不如烂笔头,整理一些个人觉得比较重要的东西. 一.基础知识 第1章 为什么要关心Java 8 Java 8提供了一个新的API(称为"流", Stream),它支持许多处理数 ...
- Linux设备驱动程序 第三版 读书笔记(一)
Linux设备驱动程序 第三版 读书笔记(一) Bob Zhang 2017.08.25 编写基本的Hello World模块 #include <linux/init.h> #inclu ...
- Primer C++第五版 读书笔记(一)
Primer C++第五版 读书笔记(一) (如有侵权请通知本人,将第一时间删文) 1.1-2.2 章节 关于C++变量初始化: 初始化不是赋值,初始化的含义是创建变量时赋予其一个初始值,而赋值的含义 ...
- 单元测试之道Java版——读书笔记
单元测试知道Java版读书笔记 首先我们必须要知道我们所写的代码,它的功能是什么,如果我们不了解代码的行为,那么也就无从测试. 我们测试的目的,是为了我们整个程序架构的稳定,代码其实就是欧文要实现功能 ...
- 《Go并发编程实战》读书笔记-语法概览
<Go并发编程实战>读书笔记-语法概览 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客我们会快速浏览一下Go的语法,内容涉及基本构成要素(比如标识符,关键字,子 ...
- 读书笔记(06) - 语法基础 - JavaScript高级程序设计
写在开头 本篇是小红书笔记的第六篇,也许你会奇怪第六篇笔记才写语法基础,笔者是不是穿越了. 答案当然是没有,笔者在此分享自己的阅读心得,不少人翻书都是从头开始,结果永远就只在前几章. 对此,笔者换了随 ...
随机推荐
- [Codeforces 26E] MultiThreading
Brief Intro: 给你n个数,每个数有2*CNT[i]个,让你构造一个序列 使得最终的Y值为W(其余见题面) Solution: 就是一道纯构造的题目: 先把特殊情况特殊处理,接下来考虑一般情 ...
- [Contest20180316]Game
这题有一个结论:如果他是最强的(⑨),那么线段树最优,如果他是最弱的,那么链状树最优 严格证明可能挺困难,感性理解就是公平赛制让强的人容易赢,极度不公平的赛制能让弱的人有机会反杀 所以我们只改他的能力 ...
- 【扩展欧几里得】poj2115 C Looooops
题意大概是让你求(A+Cx) mod 2^k = B的最小非负整数解. 若(B-A) mod gcd(C,2^k) = 0,就有解,否则无解. 式子可以化成Cx + 2^k*y = B - A,可以用 ...
- Scala实战高手****第12课:Scala函数式编程进阶(匿名函数、高阶函数、函数类型推断、Currying)与Spark源码鉴赏
/** * 函数式编程进阶: * 1.函数和变量一样作为Scala语言的一等公民,函数可以直接赋值给变量 * 2.函数更常用的方式是匿名函数,定义的时候只需要说明输入参数的类型和函数体即可,不需要名称 ...
- Java生成扫描可以生成手机号名片的二维码
(1)需求:用户通过扫描pc端网站后台管理系统的二维码获取对接人的相关信息,主要是是手机号信息,达到点击可以直接打电话或者将对接人的信息直接保存到通讯录 注:源码来源:https://blog.csd ...
- 【重装系统】老毛桃U盘工具V2013超级装机版-安装原版Win7/Win8
老毛桃U盘工具V2013超级装机版-程序下载和运行 老毛桃U盘工具V2013超级装机版-安装原版XP的方法 老毛桃U盘工具V2013超级装机版-安装原版Win7/Win8
- Shiro+SpringMVC 实现更安全的登录(加密匹配&登录失败超次数锁定帐号)
原文:http://blog.csdn.net/wlwlwlwl015/article/details/48518003 前言 初学shiro,shiro提供了一系列安全相关的解决方案,根据官方的介绍 ...
- 【jQuery】:not选择器的说明和:checked选择器的使用
1.:not选择器的说明使用 先给出一下例子: $(".form1 :not(input[name='category'])") 这个 能实现 获取到from1表单中除了input ...
- SQLSERVER中汉字提取首字母的拼音函数的实现
--创建一个汉字提取首字母的函数--还存在一点小小的问题(符号?)create function hs(@a varchar(1000)='')returns varchar(1000)asbegin ...
- HTMLTestRunner美化
https://www.cnblogs.com/findyou/p/6925733.html 参考这个,美化的不错,进入了汉化,及加入了一些样式,