40_不要重载&&、||

不要重载&&、|| 因为 用户无法实现 && ||的短路特性。

&& 短路特性: A && B 如果A为假 B将不会执行

|| 短路特性: A || B 如果A为真 B将不会执行

#include <iostream>

using namespace std;

class Complex{
public:
    Complex(int flag){
        this->flag = flag;
    }
    Complex& operator+=(Complex& complex){
        this->flag = this->flag + complex.flag;
        return *this;
    }
    bool operator&&(Complex& complex){
        return this->flag && complex.flag;
    }
public:
    int flag;
};
int main(){

    Complex complex1(0);  //flag 0
    Complex complex2(1);  //flag 1

    //原来情况,应该从左往右运算,左边为假,则退出运算,结果为假
    //这边却是,先运算(complex1+complex2),导致,complex1的flag变为complex1+complex2的值, complex1.a = 1
    // 1 && 1
    //complex1.operator&&(complex1.operator+=(complex2))
    if (complex1 && (complex1 += complex2)){
        //complex1.operator+=(complex2)
        cout << "真!" << endl;
    }
    else{
        cout << "假!" << endl;
    }

    return EXIT_SUCCESS;
}

运行结果:

&&的短路特性 期望的结果是假,但是程序的结果为真

12-18
a.cpp: In function ‘int main()’: a.cpp:7:16: error: no match for ‘operator>>’ (operand types are ‘std::basic_istream<char>::__istream_type’ {aka ‘std::basic_istream<char>’} and ‘<unresolved overloaded function type>’) 7 | cin>>number>>endl; | ~~~~~~~~~~~^~~~~~ In file included from /usr/include/c++/11/sstream:38, from /usr/include/c++/11/complex:45, from /usr/include/c++/11/ccomplex:39, from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:54, from a.cpp:1: /usr/include/c++/11/istream:120:7: note: candidate: ‘std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__istream_type& (*)(std::basic_istream<_CharT, _Traits>::__istream_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]’ 120 | operator>>(__istream_type& (*__pf)(__istream_type&)) | ^~~~~~~~ /usr/include/c++/11/istream:120:36: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘std::basic_istream<char>::__istream_type& (*)(std::basic_istream<char>::__istream_type&)’ {aka ‘std::basic_istream<char>& (*)(std::basic_istream<char>&)’} 120 | operator>>(__istream_type& (*__pf)(__istream_type&)) | ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~ /usr/include/c++/11/istream:124:7: note: candidate: ‘std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__ios_type& (*)(std::basic_istream<_CharT, _Traits>::__ios_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>; std::basic_istream<_CharT, _Traits>::__ios_type = std::basic_ios<char>]’ 124 | operator>>(__ios_type& (*__pf)(__ios_type&)) | ^~~~~~~~ /usr/include/c++/11/istream:124:32: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘std::basic_istream<char>::__ios_type& (*)(std::basic_istream<char>::__ios_type&)’ {aka ‘std::basic_ios<char>& (*)(std::basic_ios<char>&)’} 124 | operator>>(__ios_type& (*__pf)(__ios_type&)) | ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~ /usr/include/c++/11/istream:131:7: note: candidate: ‘std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::ios_base& (*)(std::ios_base&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]’ 131 | operator>>(ios_base& (*__pf)(ios_base&)) | ^~~~~~~~ /usr/include/c++/11/istream:131:30: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘std::ios_base& (*)(std::ios_base&)’ 131 | operator>>(ios_base& (*__pf)(ios_base&)) | ~~~~~~~~~~~~^~~~~~~~~~~~~~~~ /usr/include/c++/11/istream:168:7: note: candidate: ‘std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(bool&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]’ 168 | operator>>(bool& __n) | ^~~~~~~~ /usr/include/c++/11/istream:168:24: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘bool&’ 168 | operator>>(bool& __n) | ~~~~~~^~~ /usr/include/c++/11/istream:172:7: note: candidate: ‘std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short int&) [with _CharT = char; _Traits = std::char_traits<char>]’ 172 | operator>>(short& __n); | ^~~~~~~~ /usr/include/c++/11/istream:172:25: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘short int&’ 172 | operator>>(short& __n); | ~~~~~~~^~~ /usr/include/c++/11/istream:175:7: note: candidate: ‘std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(short unsigned int&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]’ 175 | operator>>(unsigned short& __n) | ^~~~~~~~ /usr/include/c++/11/istream:175:34: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘short unsigned int&’ 175 | operator>>(unsigned short& __n) | ~~~~~~~~~~~~~~~~^~~ /usr/include/c++/11/istream:179:7: note: candidate: ‘std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(int&) [with _CharT = char; _Traits = std::char_traits<char>]’ 179 | operator>>(int& __n); | ^~~~~~~~ /usr/include/c++/11/istream:179:23: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘int&’ 179 | operator>>(int& __n); | ~~~~~^~~ /usr/include/c++/11/istream:182:7: note: candidate: ‘std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(unsigned int&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]’ 182 | operator>>(unsigned int& __n) | ^~~~~~~~ /usr/include/c++/11/istream:182:32: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘unsigned int&’ 182 | operator>>(unsigned int& __n) | ~~~~~~~~~~~~~~^~~ /usr/include/c++/11/istream:186:7: note: candidate: ‘std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long int&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]’ 186 | operator>>(long& __n) | ^~~~~~~~ /usr/include/c++/11/istream:186:24: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘long int&’ 186 | operator>>(long& __n) | ~~~~~~^~~ /usr/include/c++/11/istream:190:7: note: candidate: ‘std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long unsigned int&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]’ 190 | operator>>(unsigned long& __n) | ^~~~~~~~ /usr/include/c++/11/istream:190:33: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘long unsigned int&’ 190 | operator>>(unsigned long& __n) | ~~~~~~~~~~~~~~~^~~ /usr/include/c++/11/istream:195:7: note: candidate: ‘std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long long int&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]’ 195 | operator>>(long long& __n) | ^~~~~~~~ /usr/include/c++/11/istream:195:29: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘long long int&’ 195 | operator>>(long long& __n) | ~~~~~~~~~~~^~~ /usr/include/c++/11/istream:199:7: note: candidate: ‘std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long long unsigned int&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]’ 199 | operator>>(unsigned long long& __n) | ^~~~~~~~ /usr/include/c++/11/istream:199:38: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘long long unsigned int&’ 199 | operator>>(unsigned long long& __n) | ~~~~~~~~~~~~~~~~~~~~^~~ /usr/include/c++/11/istream:214:7: note: candidate: ‘std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(float&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]’ 214 | operator>>(float& __f) | ^~~~~~~~ /usr/include/c++/11/istream:214:25: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘float&’ 214 | operator>>(float& __f) | ~~~~~~~^~~ /usr/include/c++/11/istream:218:7: note: candidate: ‘std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(double&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]’ 218 | operator>>(double& __f) | ^~~~~~~~ /usr/include/c++/11/istream:218:26: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘double&’ 218 | operator>>(double& __f) | ~~~~~~~~^~~ /usr/include/c++/11/istream:222:7: note: candidate: ‘std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long double&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]’ 222 | operator>>(long double& __f) | ^~~~~~~~ /usr/include/c++/11/istream:222:31: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘long double&’ 222 | operator>>(long double& __f) | ~~~~~~~~~~~~~^~~ /usr/include/c++/11/istream:235:7: note: candidate: ‘std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(void*&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]’ 235 | operator>>(void*& __p) | ^~~~~~~~ /usr/include/c++/11/istream:235:25: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘void*&’ 235 | operator>>(void*& __p) | ~~~~~~~^~~ /usr/include/c++/11/istream:259:7: note: candidate: ‘std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__streambuf_type*) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>]’ 259 | operator>>(__streambuf_type* __sb); | ^~~~~~~~ /usr/include/c++/11/istream:259:36: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘std::basic_istream<char>::__streambuf_type*’ {aka ‘std::basic_streambuf<char>*’} 259 | operator>>(__streambuf_type* __sb); | ~~~~~~~~~~~~~~~~~~^~~~ In file included from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:45, from a.cpp:1: /usr/include/c++/11/cstddef:132:5: note: candidate: ‘template<class _IntegerType> constexpr std::__byte_op_t<_IntegerType> std::operator>>(std::byte, _IntegerType)’ 132 | operator>>(byte __b, _IntegerType __shift) noexcept | ^~~~~~~~ /usr/include/c++/11/cstddef:132:5: note: template argument deduction/substitution failed: a.cpp:7:18: note: couldn’t deduce template parameter ‘_IntegerType’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/c++/11/string:56, from /usr/include/c++/11/bits/locale_classes.h:40, from /usr/include/c++/11/bits/ios_base.h:41, from /usr/include/c++/11/ios:42, from /usr/include/c++/11/istream:38, from /usr/include/c++/11/sstream:38, from /usr/include/c++/11/complex:45, from /usr/include/c++/11/ccomplex:39, from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:54, from a.cpp:1: /usr/include/c++/11/bits/basic_string.tcc:1485:5: note: candidate: ‘template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::__cxx11::basic_string<_CharT, _Traits, _Allocator>&)’ 1485 | operator>>(basic_istream<_CharT, _Traits>& __in, | ^~~~~~~~ /usr/include/c++/11/bits/basic_string.tcc:1485:5: note: template argument deduction/substitution failed: a.cpp:7:18: note: couldn’t deduce template parameter ‘_Alloc’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/c++/11/istream:1016, from /usr/include/c++/11/sstream:38, from /usr/include/c++/11/complex:45, from /usr/include/c++/11/ccomplex:39, from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:54, from a.cpp:1: /usr/include/c++/11/bits/istream.tcc:958:5: note: candidate: ‘std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT&) [with _CharT = char; _Traits = std::char_traits<char>]’ 958 | operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c) | ^~~~~~~~ /usr/include/c++/11/bits/istream.tcc:958:62: note: no known conversion for argument 2 from ‘<unresolved overloaded function type>’ to ‘char&’ 958 | operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c) | ~~~~~~~~^~~ In file included from /usr/include/c++/11/sstream:38, from /usr/include/c++/11/complex:45, from /usr/include/c++/11/ccomplex:39, from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:54, from a.cpp:1: /usr/include/c++/11/istream:756:5: note: candidate: ‘template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char&)’ 756 | operator>>(basic_istream<char, _Traits>& __in, unsigned char& __c) | ^~~~~~~~ /usr/include/c++/11/istream:756:5: note: template argument deduction/substitution failed: a.cpp:7:18: note: cannot convert ‘std::endl’ (type ‘<unresolved overloaded function type>’) to type ‘unsigned char&’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/c++/11/sstream:38, from /usr/include/c++/11/complex:45, from /usr/include/c++/11/ccomplex:39, from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:54, from a.cpp:1: /usr/include/c++/11/istream:761:5: note: candidate: ‘template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char&)’ 761 | operator>>(basic_istream<char, _Traits>& __in, signed char& __c) | ^~~~~~~~ /usr/include/c++/11/istream:761:5: note: template argument deduction/substitution failed: a.cpp:7:18: note: cannot convert ‘std::endl’ (type ‘<unresolved overloaded function type>’) to type ‘signed char&’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/c++/11/sstream:38, from /usr/include/c++/11/complex:45, from /usr/include/c++/11/ccomplex:39, from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:54, from a.cpp:1: /usr/include/c++/11/istream:859:5: note: candidate: ‘template<class _CharT, class _Traits, long unsigned int _Num> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT (&)[_Num])’ 859 | operator>>(basic_istream<_CharT, _Traits>& __in, _CharT (&__s)[_Num]) | ^~~~~~~~ /usr/include/c++/11/istream:859:5: note: template argument deduction/substitution failed: a.cpp:7:18: note: couldn’t deduce template parameter ‘_Num’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/c++/11/sstream:38, from /usr/include/c++/11/complex:45, from /usr/include/c++/11/ccomplex:39, from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:54, from a.cpp:1: /usr/include/c++/11/istream:868:5: note: candidate: ‘template<class _Traits, long unsigned int _Num> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char (&)[_Num])’ 868 | operator>>(basic_istream<char, _Traits>& __in, unsigned char (&__s)[_Num]) | ^~~~~~~~ /usr/include/c++/11/istream:868:5: note: template argument deduction/substitution failed: a.cpp:7:18: note: couldn’t deduce template parameter ‘_Num’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/c++/11/sstream:38, from /usr/include/c++/11/complex:45, from /usr/include/c++/11/ccomplex:39, from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:54, from a.cpp:1: /usr/include/c++/11/istream:873:5: note: candidate: ‘template<class _Traits, long unsigned int _Num> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char (&)[_Num])’ 873 | operator>>(basic_istream<char, _Traits>& __in, signed char (&__s)[_Num]) | ^~~~~~~~ /usr/include/c++/11/istream:873:5: note: template argument deduction/substitution failed: a.cpp:7:18: note: couldn’t deduce template parameter ‘_Num’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/c++/11/sstream:38, from /usr/include/c++/11/complex:45, from /usr/include/c++/11/ccomplex:39, from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:54, from a.cpp:1: /usr/include/c++/11/istream:1006:5: note: candidate: ‘template<class _Istream, class _Tp> _Istream&& std::operator>>(_Istream&&, _Tp&&)’ 1006 | operator>>(_Istream&& __is, _Tp&& __x) | ^~~~~~~~ /usr/include/c++/11/istream:1006:5: note: template argument deduction/substitution failed: a.cpp:7:18: note: couldn’t deduce template parameter ‘_Tp’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/c++/11/ccomplex:39, from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:54, from a.cpp:1: /usr/include/c++/11/complex:501:5: note: candidate: ‘template<class _Tp, class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::complex<_Tp>&)’ 501 | operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x) | ^~~~~~~~ /usr/include/c++/11/complex:501:5: note: template argument deduction/substitution failed: a.cpp:7:18: note: couldn’t deduce template parameter ‘_Tp’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:66, from a.cpp:1: /usr/include/c++/11/bitset:1472:5: note: candidate: ‘template<class _CharT, class _Traits, long unsigned int _Nb> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::bitset<_Nb>&)’ 1472 | operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x) | ^~~~~~~~ /usr/include/c++/11/bitset:1472:5: note: template argument deduction/substitution failed: a.cpp:7:18: note: couldn’t deduce template parameter ‘_Nb’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:72, from a.cpp:1: /usr/include/c++/11/iomanip:71:5: note: candidate: ‘template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::_Resetiosflags)’ 71 | operator>>(basic_istream<_CharT, _Traits>& __is, _Resetiosflags __f) | ^~~~~~~~ /usr/include/c++/11/iomanip:71:5: note: template argument deduction/substitution failed: a.cpp:7:18: note: cannot convert ‘std::endl’ (type ‘<unresolved overloaded function type>’) to type ‘std::_Resetiosflags’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:72, from a.cpp:1: /usr/include/c++/11/iomanip:101:5: note: candidate: ‘template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::_Setiosflags)’ 101 | operator>>(basic_istream<_CharT, _Traits>& __is, _Setiosflags __f) | ^~~~~~~~ /usr/include/c++/11/iomanip:101:5: note: template argument deduction/substitution failed: a.cpp:7:18: note: cannot convert ‘std::endl’ (type ‘<unresolved overloaded function type>’) to type ‘std::_Setiosflags’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:72, from a.cpp:1: /usr/include/c++/11/iomanip:132:5: note: candidate: ‘template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::_Setbase)’ 132 | operator>>(basic_istream<_CharT, _Traits>& __is, _Setbase __f) | ^~~~~~~~ /usr/include/c++/11/iomanip:132:5: note: template argument deduction/substitution failed: a.cpp:7:18: note: cannot convert ‘std::endl’ (type ‘<unresolved overloaded function type>’) to type ‘std::_Setbase’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:72, from a.cpp:1: /usr/include/c++/11/iomanip:170:5: note: candidate: ‘std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::_Setfill<_CharT>) [with _CharT = char; _Traits = std::char_traits<char>]’ 170 | operator>>(basic_istream<_CharT, _Traits>& __is, _Setfill<_CharT> __f) | ^~~~~~~~ /usr/include/c++/11/iomanip:170:71: note: no known conversion for argument 2 from ‘<unresolved overloaded function type>’ to ‘std::_Setfill<char>’ 170 | operator>>(basic_istream<_CharT, _Traits>& __is, _Setfill<_CharT> __f) | ~~~~~~~~~~~~~~~~~^~~ /usr/include/c++/11/iomanip:200:5: note: candidate: ‘template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::_Setprecision)’ 200 | operator>>(basic_istream<_CharT, _Traits>& __is, _Setprecision __f) | ^~~~~~~~ /usr/include/c++/11/iomanip:200:5: note: template argument deduction/substitution failed: a.cpp:7:18: note: cannot convert ‘std::endl’ (type ‘<unresolved overloaded function type>’) to type ‘std::_Setprecision’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:72, from a.cpp:1: /usr/include/c++/11/iomanip:230:5: note: candidate: ‘template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::_Setw)’ 230 | operator>>(basic_istream<_CharT, _Traits>& __is, _Setw __f) | ^~~~~~~~ /usr/include/c++/11/iomanip:230:5: note: template argument deduction/substitution failed: a.cpp:7:18: note: cannot convert ‘std::endl’ (type ‘<unresolved overloaded function type>’) to type ‘std::_Setw’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:72, from a.cpp:1: /usr/include/c++/11/iomanip:264:5: note: candidate: ‘template<class _CharT, class _Traits, class _MoneyT> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::_Get_money<_MoneyT>)’ 264 | operator>>(basic_istream<_CharT, _Traits>& __is, _Get_money<_MoneyT> __f) | ^~~~~~~~ /usr/include/c++/11/iomanip:264:5: note: template argument deduction/substitution failed: a.cpp:7:18: note: couldn’t deduce template parameter ‘_MoneyT’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:72, from a.cpp:1: /usr/include/c++/11/iomanip:418:5: note: candidate: ‘std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::_Get_time<_CharT>) [with _CharT = char; _Traits = std::char_traits<char>]’ 418 | operator>>(basic_istream<_CharT, _Traits>& __is, _Get_time<_CharT> __f) | ^~~~~~~~ /usr/include/c++/11/iomanip:418:72: note: no known conversion for argument 2 from ‘<unresolved overloaded function type>’ to ‘std::_Get_time<char>’ 418 | operator>>(basic_istream<_CharT, _Traits>& __is, _Get_time<_CharT> __f) | ~~~~~~~~~~~~~~~~~~^~~ In file included from /usr/include/c++/11/valarray:603, from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:95, from a.cpp:1: /usr/include/c++/11/bits/valarray_after.h:414:5: note: candidate: ‘template<class _Dom1, class _Dom2> std::_Expr<std::__detail::_BinClos<std::__shift_right, std::_Expr, std::_Expr, _Dom1, _Dom2>, typename std::__fun<std::__shift_right, typename _Dom1::value_type>::result_type> std::operator>>(const std::_Expr<_Dom1, typename _Dom1::value_type>&, const std::_Expr<_Dom2, typename _Dom2::value_type>&)’ 414 | _DEFINE_EXPR_BINARY_OPERATOR(>>, struct std::__shift_right) | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ /usr/include/c++/11/bits/valarray_after.h:414:5: note: template argument deduction/substitution failed: a.cpp:7:18: note: ‘std::basic_istream<char>::__istream_type’ {aka ‘std::basic_istream<char>’} is not derived from ‘const std::_Expr<_Dom1, typename _Dom1::value_type>’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/c++/11/valarray:603, from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:95, from a.cpp:1: /usr/include/c++/11/bits/valarray_after.h:414:5: note: candidate: ‘template<class _Dom> std::_Expr<std::__detail::_BinClos<std::__shift_right, std::_Expr, std::_Constant, _Dom, typename _Dom::value_type>, typename std::__fun<std::__shift_right, typename _Dom1::value_type>::result_type> std::operator>>(const std::_Expr<_Dom1, typename _Dom1::value_type>&, const typename _Dom::value_type&)’ 414 | _DEFINE_EXPR_BINARY_OPERATOR(>>, struct std::__shift_right) | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ /usr/include/c++/11/bits/valarray_after.h:414:5: note: template argument deduction/substitution failed: a.cpp:7:18: note: ‘std::basic_istream<char>::__istream_type’ {aka ‘std::basic_istream<char>’} is not derived from ‘const std::_Expr<_Dom1, typename _Dom1::value_type>’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/c++/11/valarray:603, from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:95, from a.cpp:1: /usr/include/c++/11/bits/valarray_after.h:414:5: note: candidate: ‘template<class _Dom> std::_Expr<std::__detail::_BinClos<std::__shift_right, std::_Constant, std::_Expr, typename _Dom::value_type, _Dom>, typename std::__fun<std::__shift_right, typename _Dom1::value_type>::result_type> std::operator>>(const typename _Dom::value_type&, const std::_Expr<_Dom1, typename _Dom1::value_type>&)’ 414 | _DEFINE_EXPR_BINARY_OPERATOR(>>, struct std::__shift_right) | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ /usr/include/c++/11/bits/valarray_after.h:414:5: note: template argument deduction/substitution failed: a.cpp:7:18: note: couldn’t deduce template parameter ‘_Dom’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/c++/11/valarray:603, from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:95, from a.cpp:1: /usr/include/c++/11/bits/valarray_after.h:414:5: note: candidate: ‘template<class _Dom> std::_Expr<std::__detail::_BinClos<std::__shift_right, std::_Expr, std::_ValArray, _Dom, typename _Dom::value_type>, typename std::__fun<std::__shift_right, typename _Dom1::value_type>::result_type> std::operator>>(const std::_Expr<_Dom1, typename _Dom1::value_type>&, const std::valarray<typename _Dom::value_type>&)’ 414 | _DEFINE_EXPR_BINARY_OPERATOR(>>, struct std::__shift_right) | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ /usr/include/c++/11/bits/valarray_after.h:414:5: note: template argument deduction/substitution failed: a.cpp:7:18: note: ‘std::basic_istream<char>::__istream_type’ {aka ‘std::basic_istream<char>’} is not derived from ‘const std::_Expr<_Dom1, typename _Dom1::value_type>’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/c++/11/valarray:603, from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:95, from a.cpp:1: /usr/include/c++/11/bits/valarray_after.h:414:5: note: candidate: ‘template<class _Dom> std::_Expr<std::__detail::_BinClos<std::__shift_right, std::_ValArray, std::_Expr, typename _Dom::value_type, _Dom>, typename std::__fun<std::__shift_right, typename _Dom1::value_type>::result_type> std::operator>>(const std::valarray<typename _Dom::value_type>&, const std::_Expr<_Dom1, typename _Dom1::value_type>&)’ 414 | _DEFINE_EXPR_BINARY_OPERATOR(>>, struct std::__shift_right) | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ /usr/include/c++/11/bits/valarray_after.h:414:5: note: template argument deduction/substitution failed: a.cpp:7:18: note: couldn’t deduce template parameter ‘_Dom’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:95, from a.cpp:1: /usr/include/c++/11/valarray:1195:1: note: candidate: ‘template<class _Tp> std::_Expr<std::__detail::_BinClos<std::__shift_right, std::_ValArray, std::_ValArray, _Tp, _Tp>, typename std::__fun<std::__shift_right, _Tp>::result_type> std::operator>>(const std::valarray<_Tp>&, const std::valarray<_Tp>&)’ 1195 | _DEFINE_BINARY_OPERATOR(>>, __shift_right) | ^~~~~~~~~~~~~~~~~~~~~~~ /usr/include/c++/11/valarray:1195:1: note: template argument deduction/substitution failed: a.cpp:7:18: note: ‘std::basic_istream<char>::__istream_type’ {aka ‘std::basic_istream<char>’} is not derived from ‘const std::valarray<_Tp>’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:95, from a.cpp:1: /usr/include/c++/11/valarray:1195:1: note: candidate: ‘template<class _Tp> std::_Expr<std::__detail::_BinClos<std::__shift_right, std::_ValArray, std::_Constant, _Tp, _Tp>, typename std::__fun<std::__shift_right, _Tp>::result_type> std::operator>>(const std::valarray<_Tp>&, const typename std::valarray<_Tp>::value_type&)’ 1195 | _DEFINE_BINARY_OPERATOR(>>, __shift_right) | ^~~~~~~~~~~~~~~~~~~~~~~ /usr/include/c++/11/valarray:1195:1: note: template argument deduction/substitution failed: a.cpp:7:18: note: ‘std::basic_istream<char>::__istream_type’ {aka ‘std::basic_istream<char>’} is not derived from ‘const std::valarray<_Tp>’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:95, from a.cpp:1: /usr/include/c++/11/valarray:1195:1: note: candidate: ‘template<class _Tp> std::_Expr<std::__detail::_BinClos<std::__shift_right, std::_Constant, std::_ValArray, _Tp, _Tp>, typename std::__fun<std::__shift_right, _Tp>::result_type> std::operator>>(const typename std::valarray<_Tp>::value_type&, const std::valarray<_Tp>&)’ 1195 | _DEFINE_BINARY_OPERATOR(>>, __shift_right) | ^~~~~~~~~~~~~~~~~~~~~~~ /usr/include/c++/11/valarray:1195:1: note: template argument deduction/substitution failed: a.cpp:7:18: note: couldn’t deduce template parameter ‘_Tp’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/c++/11/random:51, from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:108, from a.cpp:1: /usr/include/c++/11/bits/random.tcc:921:5: note: candidate: ‘template<class _IntType, class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::uniform_int_distribution<_IntType>&)’ 921 | operator>>(std::basic_istream<_CharT, _Traits>& __is, | ^~~~~~~~ /usr/include/c++/11/bits/random.tcc:921:5: note: template argument deduction/substitution failed: a.cpp:7:18: note: couldn’t deduce template parameter ‘_IntType’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/c++/11/random:51, from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:108, from a.cpp:1: /usr/include/c++/11/bits/random.tcc:982:5: note: candidate: ‘template<class _RealType, class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::uniform_real_distribution<_IntType>&)’ 982 | operator>>(std::basic_istream<_CharT, _Traits>& __is, | ^~~~~~~~ /usr/include/c++/11/bits/random.tcc:982:5: note: template argument deduction/substitution failed: a.cpp:7:18: note: couldn’t deduce template parameter ‘_RealType’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/c++/11/random:51, from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:108, from a.cpp:1: /usr/include/c++/11/bits/random.tcc:2167:5: note: candidate: ‘template<class _RealType, class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::cauchy_distribution<_RealType>&)’ 2167 | operator>>(std::basic_istream<_CharT, _Traits>& __is, | ^~~~~~~~ /usr/include/c++/11/bits/random.tcc:2167:5: note: template argument deduction/substitution failed: a.cpp:7:18: note: couldn’t deduce template parameter ‘_RealType’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/c++/11/random:49, from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:108, from a.cpp:1: /usr/include/c++/11/bits/random.h:3722:5: note: candidate: ‘template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::bernoulli_distribution&)’ 3722 | operator>>(std::basic_istream<_CharT, _Traits>& __is, | ^~~~~~~~ /usr/include/c++/11/bits/random.h:3722:5: note: template argument deduction/substitution failed: a.cpp:7:18: note: cannot convert ‘std::endl’ (type ‘<unresolved overloaded function type>’) to type ‘std::bernoulli_distribution&’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/c++/11/random:51, from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:108, from a.cpp:1: /usr/include/c++/11/bits/random.tcc:1124:5: note: candidate: ‘template<class _IntType, class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::geometric_distribution<_IntType>&)’ 1124 | operator>>(std::basic_istream<_CharT, _Traits>& __is, | ^~~~~~~~ /usr/include/c++/11/bits/random.tcc:1124:5: note: template argument deduction/substitution failed: a.cpp:7:18: note: couldn’t deduce template parameter ‘_IntType’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/c++/11/random:51, from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:108, from a.cpp:1: /usr/include/c++/11/bits/random.tcc:1775:5: note: candidate: ‘template<class _RealType, class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::exponential_distribution<_RealType>&)’ 1775 | operator>>(std::basic_istream<_CharT, _Traits>& __is, | ^~~~~~~~ /usr/include/c++/11/bits/random.tcc:1775:5: note: template argument deduction/substitution failed: a.cpp:7:18: note: couldn’t deduce template parameter ‘_RealType’ 7 | cin>>number>>endl; | ^~~~ In file included from /usr/include/c++/11/random:51, from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:108, from a.cpp:1: /usr/include/c++/11/bits/random.tcc:2561:5: note: candidate: ‘template<class _RealType, class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::weibull_distribution<_RealType>&)’ 2561 | operator>>(std::basic_istream<_CharT, _Traits>& __is, | ^~~~~~~~教我怎么看编译错误的原因
10-20
*** Using Compiler 'V5.06 update 5 (build 528)', folder: 'E:\stm32\ARM\ARMCC\Bin' Build target 'BreathingLightProMax' compiling gpio.c... compiling key.c... key.h(13): warning: #1-D: last line of file ends without a newline #endif ../Core/Inc/tim.h(45): warning: #47-D: incompatible redefinition of macro "FREQ_MIN" (declared at line 74 of "../Core/Inc/main.h") #define FREQ_MIN 100 //最小 ../Core/Inc/tim.h(48): warning: #47-D: incompatible redefinition of macro "DUTY_MIN" (declared at line 78 of "../Core/Inc/main.h") #define DUTY_MIN 0 ../Core/Inc/tim.h(49): warning: #47-D: incompatible redefinition of macro "DUTY_MAX" (declared at line 79 of "../Core/Inc/main.h") #define DUTY_MAX 100 key.c(105): warning: #186-D: pointless comparison of unsigned integer with zero if (current_duty < UTY_MIN) current_duty = DUTY_MIN; key.c: 5 warnings, 0 errors compiling main.c... ../Core/Inc/tim.h(45): warning: #47-D: incompatible redefinition of macro "FREQ_MIN" (declared at line 74 of "../Core/Inc/main.h") #define FREQ_MIN 100 //最小 ../Core/Inc/tim.h(48): warning: #47-D: incompatible redefinition of macro "DUTY_MIN" (declared at line 78 of "../Core/Inc/main.h") #define DUTY_MIN 0 ../Core/Inc/tim.h(49): warning: #47-D: incompatible redefinition of macro "DUTY_MAX" (declared at line 79 of "../Core/Inc/main.h") #define DUTY_MAX 100 ../Core/Src/main.c(81): warning: #223-D: function "Update_CCR" declared implicitly Update_CCR(); ../Core/Src/main.c(84): error: #159: declaration is incompatible with previous "Update_CCR" (declared at line 81) void Update_CCR(void) ../Core/Src/main.c(100): error: #65: expected a ";" static void BreathingLight(void){ ../Core/Src/main.c(121): warning: #12-D: parsing restarts here after previous syntax error uint16_t arr4 = _HAL_TIM_GET_AUTORELOAD(&htim4); ../Core/Src/main.c(122): error: #20: identifier "duty4" is undefined uint16_t ccr4 = duty4 * (arr4+1); ../Core/Src/main.c(122): error: #20: identifier "arr4" is undefined uint16_t ccr4 = duty4 * (arr4+1); ../Core/Src/main.c(230): warning: #223-D: function "Key_Scan_All" declared implicitly Key_Scan_All(); ../Core/Src/main.c(100): warning: #177-D: function "BreathingLight" was declared but never referenced static void BreathingLight(void){ ../Core/Src/main.c: 7 warnings, 4 errors compiling sdd1306.c... ..\Core\Src\sdd1306.c(281): warning: #223-D: function "fmodf" declared implicitly float phase = fmodf(elapsed * target_frequency, 1.0f); // 当前相位 0~1 ..\Core\Src\sdd1306.c(12): warning: #177-D: variable "wave_x" was declared but never referenced static uint8_t wave_x = 0; // 波形当前的X坐标 ..\Core\Src\sdd1306.c: 2 warnings, 0 errors compiling i2c.c... compiling stm32f1xx_hal_msp.c... compiling usart.c... ../Core/Src/usart.c(142): warning: #177-D: variable "step" was declared but never referenced float step = 100.0f / WAVE_POINTS; ../Core/Src/usart.c: 1 warning, 0 errors compiling tim.c... ../Core/Inc/tim.h(45): warning: #47-D: incompatible redefinition of macro "FREQ_MIN" (declared at line 74 of "../Core/Inc/main.h") #define FREQ_MIN 100 //最小 ../Core/Inc/tim.h(48): warning: #47-D: incompatible redefinition of macro "DUTY_MIN" (declared at line 78 of "../Core/Inc/main.h") #define DUTY_MIN 0 ../Core/Inc/tim.h(49): warning: #47-D: incompatible redefinition of macro "DUTY_MAX" (declared at line 79 of "../Core/Inc/main.h") #define DUTY_MAX 100 ../Core/Src/tim.c: 3 warnings, 0 errors compiling stm32f1xx_it.c... "BreathingLightProMax\BreathingLightProMax.axf" - 4 Error(s), 18 Warning(s). Target not created. Build Time Elapsed: 00:00:01/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file : main.c * @brief : Main program body ****************************************************************************** * @attention * * Copyright (c) 2025 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "main.h" #include "i2c.h" #include "tim.h" #include "usart.h" #include "gpio.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "math.h" #include "font6x8.h" #include "sdd1306.h" #include "key.h" #include "stdio.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ int a,b,c,d; float f=0.5;float f1=1;float f2=2;float z=0; float t=3.14/4; int n =1; uint16_t current_freq = 100; uint16_t current_duty = 50; /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); /* USER CODE BEGIN PFP */ /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ void Update_ARR(void) { uint32_t F_clk = HAL_RCC_GetPCLK2Freq(); uint32_t psc_val = htim1.Instance->PSC; uint32_t ARR_new = (F_clk / (current_freq * (psc_val + 1))) - 1; __HAL_TIM_DISABLE(&htim1); __HAL_TIM_SET_AUTORELOAD(&htim1, ARR_new); __HAL_TIM_ENABLE(&htim1); Update_CCR(); } void Update_CCR(void) { uint32_t arr_val = __HAL_TIM_GET_AUTORELOAD(&htim1); uint32_t CCR_new = (uint32_t)((float)(arr_val + 1) * (current_duty / 100.0f)) - 1; __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, CCR_new); } void Send_SquareWave_VOFA(void) { float t = HAL_GetTick() * 0.001f; float sine_val = 0.5f * sinf(2.0f * 3.1415926f * current_freq * t) + 0.5f; float pwm_val = (t - floorf(t * current_freq) / current_freq) < (current_duty / 100.0f / current_freq) ? 1.0f : 0.0f; printf("Sine:%.3f,PWM:%.3f\n", sine_val, pwm_val); static void BreathingLight(void){ float t1 = HAL_GetTick()*0.001; float duty1 = 0.5 * sin(f*3.14*t1+a*t/f)+0.5; uint16_t arr1 = __HAL_TIM_GET_AUTORELOAD(&htim1); uint16_t ccr1 = duty1 * (arr1+1); __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_1,ccr1); float t2 = HAL_GetTick()*0.001; float duty2 = 0.5 * sin(f*3.14*t2+b*t/f)+0.5; uint16_t arr2 = __HAL_TIM_GET_AUTORELOAD(&htim2); uint16_t ccr2 = duty2* (arr2+1); __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_1,ccr2); float t3 = HAL_GetTick()*0.001; float duty3 = 0.5 * sin(f*3.14*t3+c*t/f)+0.5; uint16_t arr3 = __HAL_TIM_GET_AUTORELOAD(&htim3); uint16_t ccr3 = duty3 * (arr3+1); __HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_1,ccr3); float t4 = HAL_GetTick()*0.001; float duty4 = 0.5 * sin(f*3.14*t4+d*t/f)+0.5; uint16_t arr4 = __HAL_TIM_GET_AUTORELOAD(&htim4); uint16_t ccr4 = duty4 * (arr4+1); __HAL_TIM_SET_COMPARE(&htim4,TIM_CHANNEL_3,ccr4); } //static void UpdateOLEDDisplay(void) //{ // uint32_t psc = htim1.Init.Prescaler; // uint32_t arr = htim1.Init.Period; // // float timer_clock_freq = (float)HAL_RCC_GetPCLK2Freq(); // float freq = timer_clock_freq / ((arr + 1) * (psc + 1)); // // // 3. 获取TIM1通道1的比较值(CCR) // uint32_t ccr = __HAL_TIM_GET_COMPARE(&htim1, TIM_CHANNEL_2); // // // 4. 计算占空比 // float duty_cycle = (float)ccr / (arr + 1) * 100.0f; // 转换为百分比 // char display_buf[32]; // // // 3. 将数值格式化为字符串 // sprintf(display_buf, "Freq: %.1f Hz", freq); // OLED_ShowString(0, 0, (uint8_t*)display_buf, 16); // // // sprintf(display_buf, "Duty: %.1f %%", duty_cycle); // OLED_ShowString(0, 8, (uint8_t*)display_buf, 16); // // // OLED_DrawSquareWave(0, 32, 16, freq, duty_cycle); // // OLED_UpdateScreen(); // 刷新屏幕显示 //} void UpdateOLEDDisplay(void) { char buf[32]; // 直接使用 current_freq 和 current_duty 显示 sprintf(buf, "Freq: %lu Hz", (unsigned long)current_freq); OLED_ShowString(0, 0, buf, 1); // 第一行显示频率 sprintf(buf, "Duty: %d %%", current_duty); OLED_ShowString(0, 8, buf, 1); // 第二行显示占空比 // 绘制方波图形(x_start=0, y_center=32, height=16) OLED_DrawSquareWave(0, 32, 16, (float)current_freq, (float)current_duty); OLED_UpdateScreen(); // 统一刷新屏幕 } int fputc(int ch, FILE *f) { HAL_UART_Transmit(&huart3, (uint8_t *)&ch, 1, 0xffff); return ch; } /* USER CODE END 0 */ /** * @brief The application entry point. * @retval int */ int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_TIM2_Init(); MX_TIM1_Init(); MX_TIM3_Init(); MX_TIM4_Init(); MX_I2C1_Init(); MX_USART3_UART_Init(); /* USER CODE BEGIN 2 */ HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_1); HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_1); HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_1); HAL_TIM_PWM_Start(&htim4,TIM_CHANNEL_3); HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_2); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ OLED_Init(); // 初始化OLED屏幕 OLED_Clear(); // 清屏,所有像素熄灭 __HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_2,5000); Update_ARR(); while (1) { Key_Scan_All(); static uint32_t last_vofa_send = 0; if (HAL_GetTick() - last_vofa_send >= 100) { Send_SquareWave_VOFA(); last_vofa_send = HAL_GetTick(); } // //1.阻塞式发送 // HAL_UART_Transmit(&huart3,(uint8_t *)"hello world",12,0XFFFF); // HAL_Delay(500); // //2.重定向 // printf("hello world"); // HAL_Delay(500); // //3.阻塞式接收 // uint8_t Buf[5]; // HAL_UART_Receive(&huart3,Buf,5,0XFFFF); // HAL_UART_Transmit(&huart3,Buf,5,0XFFFF); // HAL_Delay(500); /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ } /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB buses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { Error_Handler(); } } /* USER CODE BEGIN 4 */ /* USER CODE END 4 */ /** * @brief This function is executed in case of error occurrence. * @retval None */ void Error_Handler(void) { /* USER CODE BEGIN Error_Handler_Debug */ /* User can add his own implementation to report the HAL error return state */ __disable_irq(); while (1) { } /* USER CODE END Error_Handler_Debug */ } #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */ void assert_failed(uint8_t *file, uint32_t line) { /* USER CODE BEGIN 6 */ /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */ #include "key.h" #include "main.h" #include "tim.h" extern uint16_t current_freq; extern uint16_t current_duty; // 按键消抖函数 static uint8_t key_Debounce(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin) { if (HAL_GPIO_ReadPin(GPIOx, GPIO_Pin) == GPIO_PIN_RESET) // 假设低电平有效 { HAL_Delay(10); if (HAL_GPIO_ReadPin(GPIOx, GPIO_Pin) == GPIO_PIN_RESET) { return 1; // 确认按下 } } return 0; } void Update_CCR(void) { uint32_t arr_val = __HAL_TIM_GET_AUTORELOAD(&htim1); // CCR = ARR * (Duty / 100.0) uint32_t CCR_new = (uint32_t)((float)(arr_val + 1) * (current_duty / 100.0f)) - 1; // 确保 CCR 不会超出 ARR if (CCR_new >= arr_val) CCR_new = arr_val - 1; if (CCR_new < 1) CCR_new = 1; // 使用 TIM1 Channel 2 __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, CCR_new); } void Update_ARR(void) { uint32_t F_clk = HAL_RCC_GetPCLK2Freq(); uint32_t psc_val = htim1.Instance->PSC; // 保护除以零 if (current_freq == 0) current_freq = FREQ_MIN; uint32_t ARR_new = (F_clk / (current_freq * (psc_val + 1))) - 1; // 确保 ARR 不会溢出或过小 if (ARR_new > 0xFFFF) ARR_new = 0xFFFF; if (ARR_new < 1) ARR_new = 1; __HAL_TIM_DISABLE(&htim1); __HAL_TIM_SET_AUTORELOAD(&htim1, ARR_new); __HAL_TIM_ENABLE(&htim1); Update_CCR(); } void Key_Scan_All(void) { // PINA2: 频率增加 (F+) if (key_Debounce(GPIOA, GPIO_PIN_2) == 1) { if (current_freq < FREQ_MAX) { current_freq += FREQ_STEP; if (current_freq > FREQ_MAX) current_freq = FREQ_MAX; Update_ARR(); // 更新频率和 CCR } while (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_2) == GPIO_PIN_RESET); // 等待按键释放 } // PINA3: 频率减小 (F-) if (key_Debounce(GPIOA, GPIO_PIN_3) == 1) { if (current_freq > FREQ_MIN) { current_freq -= FREQ_STEP; if (current_freq < FREQ_MIN) current_freq = FREQ_MIN; Update_ARR(); // 更新频率和 CCR } while (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_3) == GPIO_PIN_RESET); } // PINA4: 占空比增加 (D+) if (key_Debounce(GPIOA, GPIO_PIN_4) == 1) { if (current_duty < DUTY_MAX) { current_duty += DUTY_STEP; if (current_duty > DUTY_MAX) current_duty = DUTY_MAX; Update_CCR(); // 更新占空比 } while (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_4) == GPIO_PIN_RESET); } // PINA5: 占空比减小 (D-) if (key_Debounce(GPIOA, GPIO_PIN_5) == 1) { if (current_duty > DUTY_MIN) { current_duty -= DUTY_STEP; if (current_duty < DUTY_MIN) current_duty = DUTY_MIN; Update_CCR(); // 更新占空比 } while (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_5) == GPIO_PIN_RESET); // 等待按键释放 } } #include "i2c.h" // 包含hi2c1的声明 #include "sdd1306.h" #include "font6x8.h" #include <string.h> #define OLED_I2C_ADDR 0x78 // OLED I2C写地址 (0x3C<<1) #define OLED_WIDTH 128 #define OLED_HEIGHT 64 static uint8_t OLED_Buffer[OLED_WIDTH * OLED_HEIGHT / 8]; static uint8_t wave_x = 0; // 波形当前的X坐标 static uint32_t last_draw_time = 0; // 上次绘制波形的时间戳 static uint8_t wave_color = 1; // 波形颜色 (1 = 白色) static float target_frequency = 1.0f; // 默认频率 1Hz static float target_duty_cycle = 50.0f; // 默认占空比 50% uint8_t OLED_GetPixel(uint8_t x, uint8_t y); void OLED_WriteCommand(uint8_t cmd) { // 发送一个命令字节 (控制字节=0x00) HAL_I2C_Mem_Write(&hi2c1, OLED_I2C_ADDR, 0x00, I2C_MEMADD_SIZE_8BIT, &cmd, 1, HAL_MAX_DELAY); } void OLED_WriteData(uint8_t data) { // 发送一个数据字节 (控制字节=0x40) HAL_I2C_Mem_Write(&hi2c1, OLED_I2C_ADDR, 0x40, I2C_MEMADD_SIZE_8BIT, &data, 1, HAL_MAX_DELAY); } void OLED_UpdateScreen(void) { // 刷新OLED显示 for (uint8_t page = 0; page < 8; page++) { OLED_WriteCommand(0xB0 + page); // 设置页地址(0xB0 = page0) OLED_WriteCommand(0x00); // 设置列低4位为0 OLED_WriteCommand(0x10); // 设置列高4位为0 (列地址0) // 按当前页,将该页的缓冲区数据写出128字节 HAL_I2C_Mem_Write(&hi2c1, OLED_I2C_ADDR, 0x40, I2C_MEMADD_SIZE_8BIT, &OLED_Buffer[page * OLED_WIDTH], OLED_WIDTH, HAL_MAX_DELAY); } } void OLED_Clear(void) { memset(OLED_Buffer, 0x00, sizeof(OLED_Buffer)); // 缓冲设0 // 刷新OLED显示 for (uint8_t page = 0; page < 8; page++) { OLED_WriteCommand(0xB0 + page); // 设置页地址(0xB0 = page0) OLED_WriteCommand(0x00); // 设置列低4位为0 OLED_WriteCommand(0x10); // 设置列高4位为0 (列地址0) // 按当前页,将该页的缓冲区数据写出128字节 HAL_I2C_Mem_Write(&hi2c1, OLED_I2C_ADDR, 0x40, I2C_MEMADD_SIZE_8BIT, &OLED_Buffer[page * OLED_WIDTH], OLED_WIDTH, HAL_MAX_DELAY); } } void OLED_Fill(uint8_t color) { // color=1 -> 全亮; color=0 -> 全黑 uint8_t fill = (color ? 0xFF : 0x00); memset(OLED_Buffer, fill, sizeof(OLED_Buffer)); // 刷新显示(OLED_Clear类似) for (uint8_t page = 0; page < 8; page++) { OLED_WriteCommand(0xB0 + page); OLED_WriteCommand(0x00); OLED_WriteCommand(0x10); HAL_I2C_Mem_Write(&hi2c1, OLED_I2C_ADDR, 0x40, I2C_MEMADD_SIZE_8BIT, &OLED_Buffer[page * OLED_WIDTH], OLED_WIDTH, HAL_MAX_DELAY); } } void OLED_Init(void) { HAL_Delay(100); // 上电延时,确保OLED电源稳定 OLED_WriteCommand(0xAE); // 1. 显示关闭 OLED_WriteCommand(0xD5); // 2. 设置显示时钟分频/振荡频率 OLED_WriteCommand(0x80); // 分频因子&振荡频率设置,0x80默认 OLED_WriteCommand(0xA8); // 3. 设置多路复用比 OLED_WriteCommand(0x3F); // 64行 (0x3F) OLED_WriteCommand(0xD3); // 4. 设置显示偏移 OLED_WriteCommand(0x00); // 无偏移 OLED_WriteCommand(0x40); // 5. 设置显示开始行 (0) OLED_WriteCommand(0x8D); // 6. 电荷泵设置 OLED_WriteCommand(0x14); // 开启电荷泵 OLED_WriteCommand(0x20); // 7. 内存地址模式 OLED_WriteCommand(0x02); // 页地址模式 (Page Mode) OLED_WriteCommand(0xA1); // 8. 列地址重映射 (A0->A1 左右翻转) OLED_WriteCommand(0xC8); // 9. 行扫描方向翻转 (上下翻转) OLED_WriteCommand(0xDA); // 10. COM 引脚配置 OLED_WriteCommand(0x12); // COM配置 (0x12 for 64行) OLED_WriteCommand(0x81); // 11. 对比度设置 OLED_WriteCommand(0x7F); // 对比度值 (0x7F) OLED_WriteCommand(0xD9); // 12. 预充电周期 OLED_WriteCommand(0xF1); // 设置为 0xF1 OLED_WriteCommand(0xDB); // 13. 设置 VCOMH 电平 OLED_WriteCommand(0x40); // 0x40 默认 OLED_WriteCommand(0xA4); // 14. 取消全显示,按照RAM内容显示 OLED_WriteCommand(0xA6); // 15. 正常显示 (非反相) OLED_WriteCommand(0xAF); // 16. 开启显示 } //OLED_DrawPixel(x, y, color) : 绘制单个像素到缓冲区 void OLED_DrawPixel(uint8_t x, uint8_t y, uint8_t color) { if (x >= OLED_WIDTH || y >= OLED_HEIGHT) return; // 越界保护 uint16_t index = (y / 8) * OLED_WIDTH + x; uint8_t bit = y % 8; if (color) OLED_Buffer[index] |= (1 << bit); // 置位 else OLED_Buffer[index] &= ~(1 << bit); // 清位 } void OLED_DrawChar(uint8_t x, uint8_t y, char chr, uint8_t color) { if (chr < 0x20 || chr > 0x7E) chr = '?'; // 非可显示字符用 '?' 代替 uint8_t index = chr - 0x20; // 每个字符宽6 for (uint8_t col = 0; col < 6; col++) { uint8_t lineBits = font6x8[index][col]; // lineBits的每个位对应该列的像素,从字模取出 for (uint8_t bit = 0; bit < 8; bit++) { uint8_t pixelColor = (lineBits >> bit) & 0x1; // 取该位 if (!color) pixelColor = !pixelColor; // 如果color=0黑底白字,可取反 OLED_DrawPixel(x + col, y + bit, pixelColor); } } } //OLED_ShowString(x, y, *str, font, color) : 从指定位置开始显示字符串。 void OLED_ShowString(uint8_t x, uint8_t y, const char *str, uint8_t color) { while (*str) { OLED_DrawChar(x, y, *str, color); x += 6; // 移动光标6列(字体宽度) if (x + 6 > OLED_WIDTH) { x = 0; // 换行到头 y += 8; // 下移一页(8像素高) } if (y >= OLED_HEIGHT) { break; // 超出屏幕则退出 } str++; } } //画线 void OLED_DrawLine(int x0, int y0, int x1, int y1, uint8_t color) { if (x0 == x1 && y0 == y1) { OLED_DrawPixel(x0, y0, color); return; } int dx = (x1 > x0 ? x1 - x0 : x0 - x1); int dy = (y1 > y0 ? y0 - y1 : y1 - y0); int sx = x0 < x1 ? 1 : -1; int sy = y0 < y1 ? 1 : -1; int err = dx + dy; // 注意dy已取负 while (1) { OLED_DrawPixel(x0, y0, color); if (x0 == x1 && y0 == y1) break; int e2 = 2 * err; if (e2 >= dy) { err += dy; x0 += sx; } if (e2 <= dx) { err += dx; y0 += sy; } } } //画矩形 void OLED_DrawRectangle(int x, int y, int width, int height, uint8_t color) { // 绘制矩形的四条边 OLED_DrawLine(x, y, x + width - 1, y, color); // 上边 OLED_DrawLine(x, y + height - 1, x + width - 1, y + height - 1, color); // 下边 OLED_DrawLine(x, y, x, y + height - 1, color); // 左边 OLED_DrawLine(x + width - 1, y, x + width - 1, y + height - 1, color); // 右边 } //画圆 void OLED_DrawCircle(int x0, int y0, int r, uint8_t color) { int a = 0; int b = r; int d = 1 - r; // 判定参数 while (a <= b) { // 八个对称点 OLED_DrawPixel(x0 + a, y0 + b, color); OLED_DrawPixel(x0 - a, y0 + b, color); OLED_DrawPixel(x0 + a, y0 - b, color); OLED_DrawPixel(x0 - a, y0 - b, color); OLED_DrawPixel(x0 + b, y0 + a, color); OLED_DrawPixel(x0 - b, y0 + a, color); OLED_DrawPixel(x0 + b, y0 - a, color); OLED_DrawPixel(x0 - b, y0 - a, color); a++; if (d < 0) { d += 2 * a + 1; } else { b--; d += 2 * (a - b) + 1; } } } //void OLED_DrawSquareWave(int x_start, int y_center, int height, float frequency, float duty_cycle) //{ // for(int page = 4; page < 8; page++) // { // memset(&OLED_Buffer[page * OLED_WIDTH], 0x00, OLED_WIDTH); // } // if (frequency <= 0.0f) return; // if (duty_cycle <= 0.0f) duty_cycle = 0.0f; // 边界保护 // if (duty_cycle >= 100.0f) duty_cycle = 100.0f; // 边界保护 // int y_high = y_center - height / 2; // 高电平 Y 坐标 // int y_low = y_center + height / 2; // 低电平 Y 坐标 // int x_end = OLED_WIDTH; // 绘制到屏幕右侧 // int width = x_end - x_start; // 绘制区域总宽度 // int num_cycles = 2; // // int cycle_width = width / num_cycles; // int high_width = (int)((float)cycle_width * (duty_cycle / 100.0f)); // int low_width = cycle_width - high_width; // // if (duty_cycle > 0.0f && high_width == 0) high_width = 1; // if (duty_cycle < 100.0f && low_width == 0) low_width = 1; // // int current_x = x_start; // for (int cycle = 0; cycle < num_cycles; cycle++) // { // int x_start_cycle = current_x; // // OLED_DrawLine(current_x, y_high, current_x + high_width - 1, y_high, wave_color); // current_x += high_width; // // if (duty_cycle > 0.0f && duty_cycle < 100.0f) { // OLED_DrawLine(current_x - 1, y_high, current_x - 1, y_low, wave_color); // } // OLED_DrawLine(current_x, y_low, current_x + low_width - 1, y_low, wave_color); // current_x += low_width; // // if (duty_cycle > 0.0f && duty_cycle < 100.0f) { // OLED_DrawLine(current_x - 1, y_low, current_x - 1, y_high, wave_color); // } // // if (current_x >= x_end) break; // } //} void OLED_UpdateWaveform(void) { uint32_t now = HAL_GetTick(); if (now - last_draw_time < 50) return; // 控制刷新率 ~20 FPS last_draw_time = now; // === 参数合法性检查 === if (target_frequency <= 0.0f) target_frequency = 0.1f; if (target_frequency > 100.0f) target_frequency = 100.0f; if (target_duty_cycle < 0.0f) target_duty_cycle = 0.0f; if (target_duty_cycle > 100.0f) target_duty_cycle = 100.0f; // === 定义波形参数 === int y_center = 48; // Y 中心位置(页面4~7,即下半屏) int height = 20; // 波形高度 int y_high = y_center - height / 2; int y_low = y_center + height / 2; // 计算当前时间对应的相位(归一化到 [0,1]) static uint32_t start_time = 0; if (start_time == 0) start_time = now; float elapsed = (now - start_time) / 1000.0f; // 秒 float phase = fmodf(elapsed * target_frequency, 1.0f); // 当前相位 0~1 uint8_t new_pixel = (phase < (target_duty_cycle / 100.0f)) ? 1 : 0; // === 滚动:将波形区域整体左移一列 === for (int y = y_high; y <= y_low; y++) { for (int x = 0; x < OLED_WIDTH - 1; x++) { uint8_t pixel = OLED_GetPixel(x + 1, y); // 获取右边像素 OLED_DrawPixel(x, y, pixel); // 左移 } } // === 在最右侧绘制新像素列(垂直线表示高低电平)=== for (int y = y_high; y <= y_low; y++) { OLED_DrawPixel(OLED_WIDTH - 1, y, new_pixel ? wave_color : 0); } // === 局部刷新后五页(波形区域)=== for (uint8_t page = 4; page < 8; page++) { OLED_WriteCommand(0xB0 + page); OLED_WriteCommand(0x00); // set low col OLED_WriteCommand(0x10); // set high col HAL_I2C_Mem_Write(&hi2c1, OLED_I2C_ADDR, 0x40, I2C_MEMADD_SIZE_8BIT, &OLED_Buffer[page * OLED_WIDTH], OLED_WIDTH, HAL_MAX_DELAY); } } void OLED_SetWaveParams(float freq, float duty_cycle) { target_frequency = freq; target_duty_cycle = duty_cycle; // 限制范围 if (target_frequency < 0.1f) target_frequency = 0.1f; if (target_frequency > 100.0f) target_frequency = 100.0f; if (target_duty_cycle < 0.0f) target_duty_cycle = 0.0f; if (target_duty_cycle > 100.0f) target_duty_cycle = 100.0f; } uint8_t OLED_GetPixel(uint8_t x, uint8_t y) { if (x >= OLED_WIDTH || y >= OLED_HEIGHT) return 0; uint16_t index = (y / 8) * OLED_WIDTH + x; uint8_t bit = y % 8; return (OLED_Buffer[index] >> bit) & 0x1; }
11-27
// 答题框内的代码仅为待实现代码,执行或提交代码时,仅包含待实现部分,不要包含其它代码。 // CodeCheck/Cmetrics工具也仅扫描待实现部分。 // 若需要完整框架用于本地调试,请点击答题框上面的“下载完整框架代码”进行下载。 #include <string> #include <vector> #include <map> using namespace std; struct Compare { bool operator()(const pair<int, vector<int>>& p1, const pair<int, vector<int>>& p2) const{ if ( p1.first != p2.first) { return p1.first < p2.first; } else { for (int i = 0 ; i < p1.second.size(); ++i) { if ((p1.second)[i] != (p2.second)[i]) return (p1.second)[i] < (p2.second)[i]; } } return false; } }; class MiniDb { private: map<int, pair<int, string>> keyMap; map<pair<int, vector<int>>, vector<int>, Compare> dataMap; public: MiniDb() { } void Create(int tableId, int colNum, const string& keys) { //pair<int, string> p = {colNum, keys}; //keyMap.insert(tableId, p); } void Insert(int tableId, const vector<int>& values) { auto it = keyMap.find(tableId); if (it != keyMap.end()) { vector<int> mark; for (int i = 0; i < ((it->second).second).size(); ++i) { mark.emplace_back(values[((it->second).second)[i] - 'a']); } pair<int, vector<int>> p = {tableId, mark}; dataMap[p] = values; } } vector<vector<int>> Select(int tableId, const vector<string>& conditions) { vector<vector<int>> result; for (auto it = dataMap.begin(); it != dataMap.end(); ++it) { if (it->first.first == tableId) { result.emplace_back(it->second); } } int num = result.size(); for (int i = 0; i < conditions.size(); ++i) { int key = conditions[i] - 'a'; int value = std::stoi(conditions[i].substr(2, conditions[i].size() - 2)); int mark = 0; for (int i = 0; i < num; ++i) { if (result[i][key] == value) { result[mark] == result[i]; mark++; } } num = mark; } vector<vector<int>> resultLast; for (int i = 0; i < num; ++i) { resultLast[i] = result[i]; } return resultLast; } }; 运行时报错main.cpp: In member function 'std::vector<std::vector<int> > MiniDb::Select(int, const std::vector<std::__cxx11::basic_string<char> >&)': main.cpp:75:37: error: no match for 'operator-' (operand types are 'const value_type' {aka 'const std::__cxx11::basic_string<char>'} and 'char') int key = conditions[i] - 'a'; In file included from /usr/include/c++/8/bits/stl_algobase.h:67, from /usr/include/c++/8/bits/char_traits.h:39, from /usr/include/c++/8/ios:40, from main.cpp:6: /usr/include/c++/8/bits/stl_iterator.h:392:5: note: candidate: 'template<class _IteratorL, class _IteratorR> constexpr decltype ((__y.base() - __x.base())) std::operator-(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_IteratorR>&)' operator-(const reverse_iterator<_IteratorL>& __x, ^~~~~~~~ /usr/include/c++/8/bits/stl_iterator.h:392:5: note: template argument deduction/substitution failed: main.cpp:75:39: note: 'const value_type' {aka 'const std::__cxx11::basic_string<char>'} is not derived from 'const std::reverse_iterator<_Iterator>' int key = conditions[i] - 'a'; ^~~ In file included from /usr/include/c++/8/bits/stl_algobase.h:67, from /usr/include/c++/8/bits/char_traits.h:39, from /usr/include/c++/8/ios:40, from main.cpp:6: /usr/include/c++/8/bits/stl_iterator.h:1188:5: note: candidate: 'template<class _IteratorL, class _IteratorR> constexpr decltype ((__x.base() - __y.base())) std::operator-(const std::move_iterator<_IteratorL>&, const std::move_iterator<_IteratorR>&)' operator-(const move_iterator<_IteratorL>& __x, ^~~~~~~~ /usr/include/c++/8/bits/stl_iterator.h:1188:5: note: template argument deduction/substitution failed: main.cpp:75:39: note: 'const value_type' {aka 'const std::__cxx11::basic_string<char>'} is not derived from 'const std::move_iterator<_IteratorL>' int key = conditions[i] - 'a'; ^~~ In file included from /usr/include/c++/8/vector:65, from main.cpp:19: /usr/include/c++/8/bits/stl_bvector.h:210:3: note: candidate: 'std::ptrdiff_t std::operator-(const std::_Bit_iterator_base&, const std::_Bit_iterator_base&)' operator-(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y) ^~~~~~~~ /usr/include/c++/8/bits/stl_bvector.h:210:3: note: no known conversion for argument 1 from 'const value_type' {aka 'const std::__cxx11::basic_string<char>'} to 'const std::_Bit_iterator_base&' In file included from /usr/include/c++/8/bits/stl_algobase.h:67, from /usr/include/c++/8/bits/char_traits.h:39, from /usr/include/c++/8/ios:40, from main.cpp:6: /usr/include/c++/8/bits/stl_iterator.h:954:5: note: candidate: 'template<class _IteratorL, class _IteratorR, class _Container> decltype ((__lhs.base() - __rhs.base())) __gnu_cxx::operator-(const __gnu_cxx::__normal_iterator<_IteratorL, _Container>&, const __gnu_cxx::__normal_iterator<_IteratorR, _Container>&)' operator-(const __normal_iterator<_IteratorL, _Container>& __lhs, ^~~~~~~~ /usr/include/c++/8/bits/stl_iterator.h:954:5: note: template argument deduction/substitution failed: main.cpp:75:39: note: 'const value_type' {aka 'const std::__cxx11::basic_string<char>'} is not derived from 'const __gnu_cxx::__normal_iterator<_IteratorL, _Container>' int key = conditions[i] - 'a'; ^~~ In file included from /usr/include/c++/8/bits/stl_algobase.h:67, from /usr/include/c++/8/bits/char_traits.h:39, from /usr/include/c++/8/ios:40, from main.cpp:6: /usr/include/c++/8/bits/stl_iterator.h:966:5: note: candidate: 'template<class _Iterator, class _Container> typename __gnu_cxx::__normal_iterator<_Iterator, _Container>::difference_type __gnu_cxx::operator-(const __gnu_cxx::__normal_iterator<_Iterator, _Container>&, const __gnu_cxx::__normal_iterator<_Iterator, _Container>&)' operator-(const __normal_iterator<_Iterator, _Container>& __lhs, ^~~~~~~~ /usr/include/c++/8/bits/stl_iterator.h:966:5: note: template argument deduction/substitution failed: main.cpp:75:39: note: 'const value_type' {aka 'const std::__cxx11::basic_string<char>'} is not derived from 'const __gnu_cxx::__normal_iterator<_Iterator, _Container>' int key = conditions[i] - 'a'; ^~~
06-04
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值