变量赋值
常用的变量赋值都是用“=”去赋值的
1 int i = 2;
但是如果把一个浮点数赋值给i的话,就会造成精度损失,在C++中最好使用初始化列表的方式“{}”给变量赋值,这样可以保证不会发生某些可能导致信息丢失的类型转换
1 #include <iostream> 2 using namespace std; 3 4 int main() { 5 int i {2.3}; 6 return 0; 7 }
比如这样声明,编译器就会报错
<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的方法
1 #include <iostream> 2 using namespace std; 3 4 int main() { 5 int v[] = {0, 1, 2, 3, 4}; 6 7 for (auto x : v) { 8 cout << x << " "; 9 } 10 cout << endl; 11 12 for (auto i = 0; i < sizeof(v) / sizeof(int); ++i) { 13 cout << v[i] << " "; 14 } 15 cout << endl; 16 17 for (auto x : {0, 1, 2, 3, 4}) { 18 cout << x << " "; 19 } 20 cout << endl; 21 22 for (auto x : v) { 23 x += 1; 24 } 25 for (auto x : v) { 26 cout << x << " "; 27 } 28 cout << endl; 29 30 /* 对于不带引用的情况,可以理解为对于v的每个元素将其从头到尾依次放入x并打印 */ 31 for (auto & x : v) { 32 x += 1; 33 } 34 for (auto x : v) { 35 cout << x << " "; 36 } 37 cout << endl; 38 39 return 0; 40 }
运行结果如下:
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,再下面介绍的类中我们可以看到一些优化的部分,可以看到类的操作会更加简单,使用者根本就无需关注里面的实现。
先来看结构实现的代码吧
1 struct Vector 2 { 3 int sz; 4 double * elem; 5 }; 6 7 void vector_init(Vector & v, int s) 8 { 9 v.elem = new double[s]; 10 v.sz = s; 11 } 12 13 double read_and_sum(int s) 14 { 15 Vector v; 16 vector_init(v, s); 17 18 for (int i = 0; i != s; ++i) { 19 cin >> v.elem[i]; 20 } 21 22 double sum = 0; 23 for (int i = 0; i != s; ++i) { 24 sum += v.elem[i]; 25 } 26 27 return sum; 28 } 29 30 int main() 31 { 32 cout << read_and_sum(5) << endl; 33 return 0; 34 }
运行结果:
类
我们先简单的介绍一下类,在后面会不断的完善,这里我们就仅仅简单的引入构造函数、初始化列表、运算符重载的概念而已。
具体比上面结构的优点,我们还是看代码吧:
1 #include <iostream> 2 #include <new> 3 4 using namespace std; 5 6 class Vector 7 { 8 public: 9 /* 10 C++11这样用 11 低版本的话就是 Vector(int s) : elem(new double[s]), sz(s) {} 12 */ 13 Vector(int s) : elem{new double[s]}, sz{s} 14 { 15 16 } 17 18 double & operator[](int i) 19 { 20 return elem[i]; 21 } 22 23 int size() 24 { 25 return sz; 26 } 27 28 private: 29 double * elem; 30 int sz; 31 }; 32 33 double read_and_sum(int s) 34 { 35 Vector v(s); 36 37 for (int i = 0; i != v.size(); ++i) { 38 cin >> v[i]; 39 } 40 41 double sum = 0; 42 for (int i = 0; i != v.size(); ++i) { 43 sum += v[i]; 44 } 45 46 return sum; 47 } 48 49 int main() 50 { 51 cout << read_and_sum(5) << endl; 52 return 0; 53 }
运行结果:
枚举书上的枚举前面加了class
1 #include <iostream> 2 #include <new> 3 4 using namespace std; 5 6 enum class Traffic_light 7 { 8 green, 9 yellow, 10 red 11 }; 12 13 14 Traffic_light & operator++(Traffic_light & t) 15 { 16 switch(t) 17 { 18 case Traffic_light::green: 19 return t = Traffic_light::yellow; 20 case Traffic_light::yellow: 21 return t = Traffic_light::red; 22 case Traffic_light::red: 23 return t = Traffic_light::green; 24 } 25 } 26 27 int main() 28 { 29 Traffic_light src = Traffic_light::green; 30 31 for (int i = 0; i < 3; ++i) { 32 Traffic_light next = ++src; 33 cout << next << endl; 34 } 35 36 return 0; 37 }
在编译的时候出错了:
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最重要的用途是为泛型编程中作为形参的类型设置断言。
1 #include <iostream> 2 #include <cassert> 3 4 using namespace std; 5 6 int main() 7 { 8 const int i = 5; 9 10 static_assert(i < 4, "5 is larger then 4"); 11 12 return 0; 13 14 }
编译结果:
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来判断编译的平台,达到尽早检查的目的
1 #include <iostream> 2 #include <cassert> 3 4 using namespace std; 5 6 int main() 7 { 8 static_assert(sizeof(void *) == 4, "64-bit code generation is not supported."); 9 10 const int i = 5; 11 12 static_assert(i < 4, "5 is larger then 4"); 13 14 return 0; 15 16 }
编译结果:
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");
^~~~~~~~~~~~~