943,915 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 428
  • C++ RSS
Sep 20th, 2009
0

error when using const keyword

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
I_Empire is offline Offline
10 posts
since Sep 2009
Sep 20th, 2009
0

Re: error when using const keyword

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
c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 749
Solved Threads: 134
Practically a Master Poster
StuXYZ is offline Offline
660 posts
since Nov 2008
Sep 21st, 2009
0

Re: error when using const keyword

i tried to do this but i could not it is giving me an error
Reputation Points: 10
Solved Threads: 0
Newbie Poster
I_Empire is offline Offline
10 posts
since Sep 2009
Sep 21st, 2009
0

Re: error when using const keyword

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.

c++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 749
Solved Threads: 134
Practically a Master Poster
StuXYZ is offline Offline
660 posts
since Nov 2008
Sep 22nd, 2009
0

Re: error when using const keyword

yes that was the problem , it worked fine thanks alot
Reputation Points: 10
Solved Threads: 0
Newbie Poster
I_Empire is offline Offline
10 posts
since Sep 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: convert ::vector<> to List<>
Next Thread in C++ Forum Timeline: [HELP]Saving variables to an external file D:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC