| | |
error when using const keyword
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Sep 2009
Posts: 10
Reputation:
Solved Threads: 0
Hi everybody,
i was writing a small c++ class that uses a list as a data member , i then wrote a small print function to print the numbers in the list
everything is working fine until i decided to set the print member function as a const , since it is not modifying anything and it is only calling const data members but when i did that , the compiler gave the following error:
error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class std::list<int,class std::allocator<int> >::const_iterator' (or there is no acceptable conversion)
error C2679: binary '!=' : no operator defined which takes a right-hand operand of type 'class std::list<int,class std::allocator<int> >::const_iterator' (or there is no acceptable conversion)
it is not a big deal i can simply remove the const keyword from the implementation and declaration and everthing will work fine , but i am only curious to know why this is happening
i was writing a small c++ class that uses a list as a data member , i then wrote a small print function to print the numbers in the list
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> #include <sstream> #include <list> using namespace std; class A { private: list<int> number; string ConverttoS(int) const; public: A(); string print(); }; string A::ConverttoS(int input) const { std::stringstream out; out << input ; return out.str(); } string A::print() { string output = ""; list<int>::iterator i; for(i = number.begin() ; i != number.end() ; ++i) output = output + ConverttoS(*i); return output; }
everything is working fine until i decided to set the print member function as a const , since it is not modifying anything and it is only calling const data members but when i did that , the compiler gave the following error:
error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class std::list<int,class std::allocator<int> >::const_iterator' (or there is no acceptable conversion)
error C2679: binary '!=' : no operator defined which takes a right-hand operand of type 'class std::list<int,class std::allocator<int> >::const_iterator' (or there is no acceptable conversion)
it is not a big deal i can simply remove the const keyword from the implementation and declaration and everthing will work fine , but i am only curious to know why this is happening
Last edited by I_Empire; Sep 20th, 2009 at 10:58 am.
•
•
Join Date: Nov 2008
Posts: 397
Reputation:
Solved Threads: 72
This is simple (I hope). You are using an iterator. The iterator allows the method to change the data.
Now since an iterator is a class and your compiler cannot know until link time that the iterator does not change the data, you compiler HAS to assume that the iterator does, hence it doesn't allow const to be added to print.
A simple way to get round this is to use const_iteraror.
Another way is this
The later method avoids the convert to S construction that creates a number of stringstreams, and avoids having an explicit iterator. So is slightly more preferable to some peoples tastes.
Note: you have to add
Now since an iterator is a class and your compiler cannot know until link time that the iterator does not change the data, you compiler HAS to assume that the iterator does, hence it doesn't allow const to be added to print.
A simple way to get round this is to use const_iteraror.
Another way is this
c++ Syntax (Toggle Plain Text)
std::string A::print() const { std::ostringstream oSS; copy(number.begin(),number.end(), std::ostream_iterator<int>(oSS,"")); return oSS.str(); }
The later method avoids the convert to S construction that creates a number of stringstreams, and avoids having an explicit iterator. So is slightly more preferable to some peoples tastes.
Note: you have to add
#include <iterator> , to your C++ file. Last edited by StuXYZ; Sep 20th, 2009 at 11:48 am.
experience is the most expensive way to learn anything
•
•
Join Date: Nov 2008
Posts: 397
Reputation:
Solved Threads: 72
Can you post your new non-compiling/non-running code please.
The only thing that I can think you might have done wrong is to forget to at const to both the declaration and the instance eg.
The only thing that I can think you might have done wrong is to forget to at const to both the declaration and the instance eg.
c++ Syntax (Toggle Plain Text)
class A { std::string print() const ; }; std::string A::print() const { std::string output; std::list<int>::const_iterator iterI; //... return output; }
experience is the most expensive way to learn anything
![]() |
Similar Threads
- Elementary Class/const Question (C++)
- confused by const (C++)
- Unknown syntax error in C++ class (C++)
- Count/Find in const char * (C++)
- const object (C++)
- only const static integral data members can be initialized inside a class or struct (C++)
- Error in the Operaror- function ?? (C++)
- C++ const help (C++)
- error in copy constructor (C)
- Parse error, syntax error, Forbids declaration (C++)
Other Threads in the C++ Forum
- Previous Thread: convert ::vector<> to List<>
- Next Thread: [HELP]Saving variables to an external file D:
Views: 278 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





