error when using const keyword

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2009
Posts: 10
Reputation: I_Empire is an unknown quantity at this point 
Solved Threads: 0
I_Empire I_Empire is offline Offline
Newbie Poster

error when using const keyword

 
0
  #1
Sep 20th, 2009
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

  1.  
  2. #include <iostream>
  3. #include <string>
  4. #include <sstream>
  5. #include <list>
  6. using namespace std;
  7. class A
  8. {
  9. private:
  10. list<int> number;
  11. string ConverttoS(int) const;
  12. public:
  13. A();
  14. string print();
  15. };
  16.  
  17. string A::ConverttoS(int input) const
  18. {
  19. std::stringstream out;
  20. out << input ;
  21. return out.str();
  22. }
  23.  
  24. string A::print()
  25. {
  26. string output = "";
  27. list<int>::iterator i;
  28.  
  29. for(i = number.begin() ; i != number.end() ; ++i)
  30. output = output + ConverttoS(*i);
  31.  
  32. return output;
  33. }

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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 397
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 72
StuXYZ StuXYZ is offline Offline
Posting Whiz

Re: error when using const keyword

 
0
  #2
Sep 20th, 2009
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
  1. std::string
  2. A::print() const
  3. {
  4. std::ostringstream oSS;
  5. copy(number.begin(),number.end(),
  6. std::ostream_iterator<int>(oSS,""));
  7. return oSS.str();
  8. }

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
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 10
Reputation: I_Empire is an unknown quantity at this point 
Solved Threads: 0
I_Empire I_Empire is offline Offline
Newbie Poster

Re: error when using const keyword

 
0
  #3
Sep 21st, 2009
i tried to do this but i could not it is giving me an error
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 397
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 72
StuXYZ StuXYZ is offline Offline
Posting Whiz

Re: error when using const keyword

 
0
  #4
Sep 21st, 2009
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.

  1. class A
  2. {
  3. std::string print() const ;
  4. };
  5.  
  6. std::string
  7. A::print() const
  8. {
  9. std::string output;
  10. std::list<int>::const_iterator iterI;
  11. //...
  12. return output;
  13. }
experience is the most expensive way to learn anything
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 10
Reputation: I_Empire is an unknown quantity at this point 
Solved Threads: 0
I_Empire I_Empire is offline Offline
Newbie Poster

Re: error when using const keyword

 
0
  #5
Sep 22nd, 2009
yes that was the problem , it worked fine thanks alot
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 278 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC