变量赋值

常用的变量赋值都是用“=”去赋值的

 int i = ;

但是如果把一个浮点数赋值给i的话,就会造成精度损失,在C++中最好使用初始化列表的方式“{}”给变量赋值,这样可以保证不会发生某些可能导致信息丢失的类型转换

 #include <iostream>
using namespace std; int main() {
int i {2.3};
return ;
}

比如这样声明,编译器就会报错

<source>: In function 'int main()':
5 : <source>:5:12: error: narrowing conversion of '2.2999999999999998e+0' from 'double' to 'int' inside { } [-Wnarrowing]
int i {2.3};
^
Compiler exited with result code 1
 
auto
通过一个for循环来学习使用auto的方法
 #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 ;
}

运行结果:

枚举

书上的枚举前面加了class
 #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]
 }
 ^
应该是没有重载标准输出导致的,这个后续再完善吧,如果把class去掉,就可以正常运行了。囧……
 
静态断言 static_assert
static_assert能用于任何可以表达为常量表达式的东西,static_assert(A, S)的作用是当A不为true时把S作为一条编译器错误信息输出,这是C++11的特性。
static_assert最重要的用途是为泛型编程中作为形参的类型设置断言。
 #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;
         ^
另外可以用static_assert来判断编译的平台,达到尽早检查的目的
 #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++概览:基础知识的更多相关文章

  1. JavaScript高级程序设计第三版-读书笔记(1-3章)

    这是我第一次用markdown,也是我第一次在网上记录我自己的学习过程. 第一章 JavaScript主要由以下三个不同的部分构成 ECMAScript   提供核心语言功能 DOM     提供访问 ...

  2. 9Andrew.S.Tanenbaum计算机网络第三版读书笔记-总体概览

  3. 《Java并发编程实战》读书笔记-第一部分 基础知识小结

    并发技巧清单 可变状态是至关重要的 所有的并发问题都可以归结为如何协调对并发状态的访问.可变状态越少,就越容易确保线程安全性. 尽量将域声明为final类型,除非需要它们是可变的. 不可变对象一定是线 ...

  4. Java 8实战之读书笔记二:基础知识

    好记性不如烂笔头,整理一些个人觉得比较重要的东西. 一.基础知识 第1章 为什么要关心Java 8 Java 8提供了一个新的API(称为"流", Stream),它支持许多处理数 ...

  5. Linux设备驱动程序 第三版 读书笔记(一)

    Linux设备驱动程序 第三版 读书笔记(一) Bob Zhang 2017.08.25 编写基本的Hello World模块 #include <linux/init.h> #inclu ...

  6. Primer C++第五版 读书笔记(一)

    Primer C++第五版 读书笔记(一) (如有侵权请通知本人,将第一时间删文) 1.1-2.2 章节 关于C++变量初始化: 初始化不是赋值,初始化的含义是创建变量时赋予其一个初始值,而赋值的含义 ...

  7. 单元测试之道Java版——读书笔记

    单元测试知道Java版读书笔记 首先我们必须要知道我们所写的代码,它的功能是什么,如果我们不了解代码的行为,那么也就无从测试. 我们测试的目的,是为了我们整个程序架构的稳定,代码其实就是欧文要实现功能 ...

  8. 《Go并发编程实战》读书笔记-语法概览

    <Go并发编程实战>读书笔记-语法概览 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客我们会快速浏览一下Go的语法,内容涉及基本构成要素(比如标识符,关键字,子 ...

  9. 读书笔记(06) - 语法基础 - JavaScript高级程序设计

    写在开头 本篇是小红书笔记的第六篇,也许你会奇怪第六篇笔记才写语法基础,笔者是不是穿越了. 答案当然是没有,笔者在此分享自己的阅读心得,不少人翻书都是从头开始,结果永远就只在前几章. 对此,笔者换了随 ...

随机推荐

  1. 【分块】bzoj3295 [Cqoi2011]动态逆序对

    考虑每次删除pos位置一个数x后,所造成的的影响就是,逆序对的个数少了在1~pos-1中大于x的数的个数加上pos+1~n中小于x的数的个数. 那么我们需要的操作就只有查询区间内比某数大(小)的个数. ...

  2. es 数据 导出 到 MySQL

    暂时没有找到直接 导出到 mysql 数据库的工具 或者项目 目前实现思路: 使用 elasticdump  工具 实现 从 es 数据 导出到 json 文件 ,然后 使用 脚本程序 操作 改 js ...

  3. python基础-函数之装饰器、迭代器与生成器

    1. 函数嵌套 1.1 函数嵌套调用 函数的嵌套调用:在调用一个函数的过程中,又调用了其他函数 def bar(): print("from in the bar.") def f ...

  4. js发送验证码(倒计时)

    需求:在用户点击获取验证码后60s内不能再次获取 代码 //倒计时60秒 var countdown=60; //显示获取验证码倒计时的文本标签 var _generate_code =$(" ...

  5. Android 中AlarmManager升级4.2

  6. Linux之ps查找进程用kill终止命令

    http://www.cnblogs.com/peida/archive/2012/12/20/2825837.html http://blog.csdn.net/andy572633/article ...

  7. 【docker】查看docker镜像的版本号TAG,从远程仓库拉取自己想要版本的镜像

    要想查看镜像的版本好TAG,需要在docker hub查看 地址如下:https://hub.docker.com/r/library/ 进入之后,在页面左上角搜索框搜索, 例如搜索redis 搜索完 ...

  8. powerbuilder webbrowser 内嵌浏览器 select下拉框无法使用

        Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MA ...

  9. HashMap深度解析(一)

    HashMap可以说是Java中最常用的集合类框架之一,是Java语言中非常典型的数据结构,我们总会在不经意间用到它,很大程度上方便了我们日常 开发.在很多Java的笔试题中也会被问到,最常见的,“H ...

  10. Android ViewStub的使用方法

    大家写项目的时候肯定会有一些东西提前写好,可是不到一定条件是不想让它显示出来的.我们可能的做法就是让它View.GONE 或View.INVISIBLE等到一定条件了在代码里面这设置View.VISI ...