个人c++ 错误记录
1.類定義輸入運算符 >> 時候,代碼最后應(yīng)該檢測 輸入流是否為有效,如果無效,應(yīng)該把輸入對象置于默認(rèn)初始化狀態(tài)。
??? 定義>>時候,類必須是非常量引用。定義<<,最好是常量引用。
???? 定義的時候不能加friend.
2.關(guān)于日期輸入的正確格式:
Date & Date::operator=(const string &date) { istringstream is(date); char ch1,ch2; is >> year >> ch1 >> month >> ch2 >> day; if(!is || ch1 != '-' || ch2 != '-' )throw invalid_argument("Bad date"); if(month < 1 || month > 12 || day < 1 || day > 21)throw invalid_argument("Bad date"); return *this; }3.
#include <string> #include <iostream> using namespace std; class Test {int &fun()const{return a;}private:int a;}; int main() { Test t1;return 0; } binding reference of type ‘int&’ to ‘const int’ discards qualifiersconst成員函數(shù)不能返回類成員的非常量引用。因為有修改類成員的風(fēng)險。
3.
g++報錯"undefined reference to `vtable for XXX' "的原因 原因就一個:沒有全部實現(xiàn)XXX基類的純虛函數(shù).4.
// items定義成一個multiset,可以將同一本書的多條交易信息保存到一個multiset中,
// 這里不能用圓括號,因為default member initializer來初始化成員有2
// 個方法,list initialization,即一對花括號.第二種是copy initializa//tion,即 等號.如果嘗試用圓括號,編譯器會誤以為是函數(shù)聲明,所以/報告compare is not a type..
?? ??? ?multiset<shared_ptr<Quote>,decltype(compare)*> items{compare};
所以這里給items初始化有不能使用圓括號。
5.
MoveBase_cmd_vel_sub = node_handle.subscribe( "/movebase_cmd_vel", 60, DHRobotBase::moveBase_cmd_vel); // 訂閱 topic在使用這句話時出現(xiàn)這個問題:
?error: invalid use of non-static member function? ? ? ?"/movebase_cmd_vel", 60, DHRobotBase::moveBase_cmd_vel);
折騰了好久終于弄好了:
MoveBase_cmd_vel_sub = node_handle.subscribe("/movebase_cmd_vel", 60, &DHRobotBase::moveBase_cmd_vel,this); // 訂閱topic原因大概是這個:在C++語言中,對于一個由類名加倆冒號再加成員名構(gòu)成的東西(學(xué)名叫“quilified-id”),比如:A::x,只有當(dāng)x是A類的靜態(tài)成員的時候,A::x才能表示一個左值。而對于函數(shù)類型到函數(shù)指針類型的默認(rèn)轉(zhuǎn)換,只有當(dāng)函數(shù)類型是左值的時候才行
6.關(guān)于c、c++的輸入輸出函數(shù)的掌握:
/***********************************************************************************這個讀取字符的函數(shù)可以把所有的輸入字符都?xì)w結(jié)為' '和非' '兩類,減輕輸入字符后的處理難度。 */ char read_char(void) { int ch = getchar(); if(ch == '\t' || ch == '\n')return ' '; return ch; }讀入單詞的一個經(jīng)典程序,c現(xiàn)代方法上的
?
自己的c語言太菜了,真是無地自容了。
7.
//本程序只有一個主題: //對于代碼中的某個節(jié)點來說,基類的公有成員是否是可訪問的,則派生類向基類的轉(zhuǎn)換也是可訪問的;反之則不行。 //我自己的語言理解這句話是:對于某個用戶來說,如果這個用戶能透過派生類看到(或者使用)基類的公有成員,那么這個用戶就可以使用從派生類向基類的轉(zhuǎn)換;反之,則不行。 //如果上面的難理解,那我就這樣說:某個用戶能否使用派生類對象向基類對象的轉(zhuǎn)換,關(guān)鍵是看對這個用戶來說,能否通過派生類對象使用基類的共有成員,B b;b.pub_men//(如果能這樣使用就對,不能就錯);#include <string> #include <iostream> using namespace std; class A {public:int a= 1;protected:int b = 2;private:int c = 3;};class B:protected A { public: void f()const{A * a;B * b = new B;//下面的這句,能否實現(xiàn),關(guān)鍵在于這個用戶能否通過派生類看到基類的共有成員。//下面這句的用戶其實就是派生類本身(A是基類,B是派生類),肯定能看見基類的共有成員了//所以下面的語句是合法的a = b; //派生類對象到基類對象的轉(zhuǎn)換cout << "B::b" << b->b << endl;delete b;} };class C:protected B {public:void f(){cout << "A::b" <<b << endl;}};int main() { A * a = new A; B * b = new B; //下面這個語句 c=b 是否合理,關(guān)鍵是這個語句的用戶能否透過派生類(B類)看見基類(A類)的共有成員, //答案是否定的,因為B:protected A,這句話說明了,A類的共有成員在B類中成為了protected成員。 //對于普通用戶而言,B類protected成員是不可見的。所以下面這句代碼匯報錯。 //A * c = b; //使用了派生類對象向基類對象的轉(zhuǎn)換 b->f();delete a; delete b;return 0; }8.自己在做15.9的課后習(xí)題15.41時候,把OrQuery的eval求并集的set_intersection的函數(shù)中,前兩個迭代器,本應(yīng)該是shared_ptr<set<line_no>>的迭代器,結(jié)果帶入shared_ptr<vector<string>>的迭代器,結(jié)果出現(xiàn)如下看不懂的錯誤:
a.out query.cc quote.h textquery.cc r@r:~/coml/c++/15/15.9/ex/15.41$ g++ main.cc query.cc -o 123 In file included from /usr/include/c++/9/algorithm:62,from query.h:38,from query.cc:7: /usr/include/c++/9/bits/stl_algo.h: In instantiation of ‘_OutputIterator std::__set_intersection(_InputIterator1, _InputIterator1, _InputIterator2, _InputIterator2, _OutputIterator, _Compare) [with _InputIterator1 = __gnu_cxx::__normal_iterator<std::__cxx11::basic_string<char>*, std::vector<std::__cxx11::basic_string<char> > >; _InputIterator2 = __gnu_cxx::__normal_iterator<std::__cxx11::basic_string<char>*, std::vector<std::__cxx11::basic_string<char> > >; _OutputIterator = std::insert_iterator<std::set<long unsigned int> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]’: /usr/include/c++/9/bits/stl_algo.h:5307:48: required from ‘_OIter std::set_intersection(_IIter1, _IIter1, _IIter2, _IIter2, _OIter) [with _IIter1 = __gnu_cxx::__normal_iterator<std::__cxx11::basic_string<char>*, std::vector<std::__cxx11::basic_string<char> > >; _IIter2 = __gnu_cxx::__normal_iterator<std::__cxx11::basic_string<char>*, std::vector<std::__cxx11::basic_string<char> > >; _OIter = std::insert_iterator<std::set<long unsigned int> >]’ query.h:425:93: required from here /usr/include/c++/9/bits/stl_algo.h:5258:16: error: no match for ‘operator=’ (operand types are ‘std::insert_iterator<std::set<long unsigned int> >’ and ‘std::__cxx11::basic_string<char>’)5258 | *__result = *__first1;| ~~~~~~~~~~^~~~~~~~~~~ In file included from /usr/include/c++/9/bits/stl_algobase.h:67,from /usr/include/c++/9/bits/char_traits.h:39,from /usr/include/c++/9/string:40,from query.cc:1: /usr/include/c++/9/bits/stl_iterator.h:716:7: note: candidate: ‘std::insert_iterator<_Container>& std::insert_iterator<_Container>::operator=(const typename _Container::value_type&) [with _Container = std::set<long unsigned int>; typename _Container::value_type = long unsigned int]’716 | operator=(const typename _Container::value_type& __value)| ^~~~~~~~ /usr/include/c++/9/bits/stl_iterator.h:716:56: note: no known conversion for argument 1 from ‘std::__cxx11::basic_string<char>’ to ‘const value_type&’ {aka ‘const long unsigned int&’}716 | operator=(const typename _Container::value_type& __value)| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~ /usr/include/c++/9/bits/stl_iterator.h:724:7: note: candidate: ‘std::insert_iterator<_Container>& std::insert_iterator<_Container>::operator=(typename _Container::value_type&&) [with _Container = std::set<long unsigned int>; typename _Container::value_type = long unsigned int]’724 | operator=(typename _Container::value_type&& __value)| ^~~~~~~~ /usr/include/c++/9/bits/stl_iterator.h:724:51: note: no known conversion for argument 1 from ‘std::__cxx11::basic_string<char>’ to ‘std::set<long unsigned int>::value_type&&’ {aka ‘long unsigned int&&’}724 | operator=(typename _Container::value_type&& __value)| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~ /usr/include/c++/9/bits/stl_iterator.h:665:11: note: candidate: ‘constexpr std::insert_iterator<std::set<long unsigned int> >& std::insert_iterator<std::set<long unsigned int> >::operator=(const std::insert_iterator<std::set<long unsigned int> >&)’665 | class insert_iterator| ^~~~~~~~~~~~~~~ /usr/include/c++/9/bits/stl_iterator.h:665:11: note: no known conversion for argument 1 from ‘std::__cxx11::basic_string<char>’ to ‘const std::insert_iterator<std::set<long unsigned int> >&’ /usr/include/c++/9/bits/stl_iterator.h:665:11: note: candidate: ‘constexpr std::insert_iterator<std::set<long unsigned int> >& std::insert_iterator<std::set<long unsigned int> >::operator=(std::insert_iterator<std::set<long unsigned int> >&&)’ /usr/include/c++/9/bits/stl_iterator.h:665:11: note: no known conversion for argument 1 from ‘std::__cxx11::basic_string<char>’ to ‘std::insert_iterator<std::set<long unsigned int> >&&’總結(jié)
以上是生活随笔為你收集整理的个人c++ 错误记录的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 通过标题区别自己的发帖
- 下一篇: c++成员运算符的重载