Summary of common debugging errors during C ++ primer Learning

Source: Internet
Author: User
Tags ranges
Summary of common debugging errors during C ++ primer Learning

This is the list of Solutions to errors that occur during debugging when learning the C ++ primer program.

No.1: prog2.cpp (8 ):
Error c2664: '_ Thiscall STD: List <int, class STD: Allocator <int >:: STD: List <int, class STD :: allocator <int> (unsigned int, const Int &,
Const class STD: Allocator <int> &) ': cannot convert parameter 1 from 'class STD: deque <int, class STD: Allocator <int> :: iterator 'to 'unsigned int'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

//prog2.cpp#include <iostream>#include <list>#include <deque>using namespace std;void main(){      //Define a list that holds elements that are deques that hold ints deque<int>   ideque(10,1); list<int>  ilist(ideque.begin(),ideque.end()); for(list<int>::const_iterator itbegin=ilist.begin(),itend=ilist.end();itbegin!=itend;++itbegin) cout<<*itbegin<<endl;}


Solution: VC 6.0 does not support the template library well. It is compiled using vs2010.


No. 2 textquery. cpp (63 ):
Warning c00002: returning address of local variable or temporary

// Return the row number setconst set <int> & textquery: runquery (string word) const {Map <string, set <int> :: const_iterator it = m_mapwordline.find (Word); If (it! = M_mapwordline.end () return it-> second; elsereturn set <int> (); // emptyset}


Solution: Willing to return the const reference of the Set object to reduce the burden on copying the set object. However, it is wrong to return the partial reference of an empty set object, the original C ++ primer uses the method to return the set object without reference, which is also a solution. In addition, the use of STD: vector <STD: String >:: size_type is better than the int type set.


No. 3 printchar. cpp (13 ):
Error c2440: 'initializing': cannot convert from 'Char * 'to 'const class STD: basic_string <char, struct STD: char_traits <char>, class STD :: allocator <char> *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

// Output the void printchar (string & line) {istringstream ISS (line); string word; while (ISS> word) for (vector <string> :: const_iterator itbegin = word. begin (), itend = word. end (); itbegin! = Itend; ++ itbegin) cout <* itbegin <Endl ;}

Solution: The String object of the standard library can be operated by the iterator. To use the iterator correctly, use the string: const_iterator and the subscript operation to obtain characters in the string object.

No. 4Serious error "vector iterator + offset out of range" "Standard C ++ libraries out of range"

The Code is as follows:

# Include <iostream> # include <iterator> // use back_inserter # include <algorithm> # include <vector> using namespace STD; void main () {vector <int> ivec; try {fill_n (ivec. begin (),); // error shocould use fill_n (back_inserter (ivec), 10, 1); For (vector <int >:: iterator itbegin = ivec. begin (), itend = ivec. end (); itbegin! = Itend; ++ itbegin) cout <* itbegin <Endl;} catch (runtime_error ERR) {cerr <"error:" <err. what () <Endl;} catch (out_of_range or) {cerr <"error:" <or. what () <Endl;} catch (exception ex) {cerr <"error:" <ex. what () <Endl ;}}

Solution: The fill_n () function starts from the beginning in the vector and sets the specified number of elements to the given value.The fill_n function assumes that it is safe to write a specified number of elements. The common mistake for beginners is to call an empty container without elements.Fill_nTherefore, back_inserter must be used to insert the iterator. When you use the insert iterator to assign values, a new element is added to the container, whose value is equal to the value of the right operand of the value assignment operation. Therefore, you need to change the code:

Fill_n (back_inserter (ivec), 10, 1 );

No. 5Prog7.cpp (8 ):
Error c2780:
'Void _ cdecl STD: Sort (_ Ri, _ Ri, _ Pr) ': expects 3 arguments-2 provided
C: \ Program Files \ Microsoft Visual Studio \ vc98 \ include \ algorithm (588): See Declaration of 'sort'
Prog7.cpp (8 ):Error c2782:'Void _ cdecl STD: Sort (_ Ri, _ RI) ': Template parameter' _ ri' is ambiguous
Cocould be 'class STD: reverse_iterator <int *, Int, Int &, int *, int>'
Or 'int *'

The Code is as follows:

# Include <iostream> # include <algorithm> # include <vector> using namespace STD; void main () {vector <int> ivec1 (10, 1); sort (ivec1.begin (), ivec1.rend (); // errors with mismatched types can be checked during compilation}


There are two versions of the sort function overload, so the error message above is displayed. No matter which version, a pair of iterator ranges must be specified. In the standard library, generic algorithms with input ranges require that their two iterators have the same type, including the const attribute. Either const or non-Const. Otherwise, compilation fails.

The above begin function returns a common iterator, while the rend function returns a reverse iterator. Therefore, the two real parameters do not match and the above error occurs. The solution is to pass the real parameters correctly, use an iterator of the same type to mark the range.

No. 6.. \ Sales_item \ sales_item.h (24 ):Error c2804: Binary 'operator + 'has too parameter Parameters

The Code is as follows:

// header file Sales_item.h#include <iostream>#include <string>class Sales_item {   // private members private:std::string isbn; unsigned units_sold;double revenue;    //public methodpublic:.. //Overloaded Operator as member function   Sales_item& operator+=(const Sales_item&);//Compound Assignment OperatorsSales_item operator+(const Sales_item& lhs, const Sales_item& rhs);};// implement file  Sales_item.cppusing std::istream;  using std::ostream;//Overloaded Operator as nonmember functionsinline Sales_item Sales_item::operator+(const Sales_item& lhs, const Sales_item& rhs){  Sales_item ret(lhs);  // copy lhs into a local object that we'll return                  ret += rhs;           // add in the contents of rhs                return ret; // return ret by value ,not by reference}


SolutionThe + operator includes two operands and should be overloaded as a normal non-member function.

Note that the number of parameters of the overload operator (including the implicit this pointer of the member function) is the same as the number of operands of the operator. Symmetric operators, such as arithmetic operators, equal operators, Relational operators, and bit operators, are best defined as common non-member functions. Therefore, the + should be a normal non-member function. Here, when the overload is a member function, there is an extra this parameter. Therefore, for the + operator, too many parameters are incorrect.

That is, the writing is:

// header fileSales_item operator+(const Sales_item& lhs, const Sales_item& rhs);//implement file//Overloaded Operator as nonmember functionsinline Sales_item operator+(const Sales_item& lhs, const Sales_item& rhs){  Sales_item ret(lhs);  // copy lhs into a local object that we'll return      ret += rhs;           // add in the contents of rhs      return ret; // return ret by value ,not by reference}


No. 7Compiling... Main. cpplinking... Main. OBJ:
Error lnk2001: Unresolved external symbol "class STD: basic_ostream <char, struct STD: char_traits <char >> &_ Cdecl
Operator <(class STD: basic_ostream <char, struct STD: char_traits <char> &, class sales_item const &)"
(?? 6 @ yaaav? $ Basic_ostream @ du $ char_traits @ d @ STD @ aav01 @ abvsales_item @ Z)
... (Omitted)

Sales_item.exe-4 error (s), 0 warning (s)

One of the causes of the error is that the inline function is implemented in a separate implementation file. solution:

Place the Implementation of The Inline Function in the header file. For more information, see :《Class inline function implementation should be put in the article.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.