Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Can't figure out this error...

Can't figure out this error...

Scheduled Pinned Locked Moved C / C++ / MFC
csharpc++visual-studiographicsregex
17 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B BadKarma

    Hi, I don't see it either, but you can try to split the code up to make it easier top spot it. According to the error messages the iterator believes it should use the const iterator instead of the non-const version.

    std::vector lstLigacao = this->_m_graf_map[_onde];
    std::vector::iterator it;
    
    for(it = lstLigacao.begin() ; it != lstLigacao.end() ; it++)
    {
    	if (
    		((*it)->esquerda()->id() == _esq && (*it)->direita()->id() == _dir) ||
    		((*it)->esquerda()->id() == _dir && (*it)->direita()->id() == _esq)
    	) return true;
    }
    

    Learn from the mistakes of others, you may not live long enough to make them all yourself.

    A Offline
    A Offline
    AndreFratelli
    wrote on last edited by
    #6

    That works! But I can't afford to copy an entire array like that =/ If that's the case, I rather use vector<>::size_type to iterate through the elements But why does this happen? I mean, lstLigacao and _m_graf_map[_ond] have exactly the same type, right? So why does the iterator work with one, but not the other? :confused: Best regards Fratelli

    B 1 Reply Last reply
    0
    • N Nibu babu thomas

      AndreFratelli wrote:

      std::vector >

      Just a suggestion! Use a typedef to create type for this vector and will make things easier for you...

      typedef std::vector<std::vector<Ligacao *> > LigacaoVector2d;

      Then create iterators like LigacaoVector2d::iterator it;

      Nibu babu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

      A Offline
      A Offline
      AndreFratelli
      wrote on last edited by
      #7

      Yes, I usually do that.. But when dealing with arrays I also define an "iterator" type, like: typedef std::vector<std::vector<Ligacao *> > LigacaoVector2d; typeded LigacaoVector2d::iterator iterator; That way, I can even do: iterator it; Thx anyway ;) =) best regards Fratelli

      N 1 Reply Last reply
      0
      • A AndreFratelli

        Yes, I usually do that.. But when dealing with arrays I also define an "iterator" type, like: typedef std::vector<std::vector<Ligacao *> > LigacaoVector2d; typeded LigacaoVector2d::iterator iterator; That way, I can even do: iterator it; Thx anyway ;) =) best regards Fratelli

        N Offline
        N Offline
        Nibu babu thomas
        wrote on last edited by
        #8

        AndreFratelli wrote:

        typeded LigacaoVector2d::iterator iterator;

        It's better to use it directly so that readers of your code can understand from where this iterator is being used/to which vector this iterator belongs. Isn't it?

        Nibu babu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

        A 1 Reply Last reply
        0
        • A AndreFratelli

          That works! But I can't afford to copy an entire array like that =/ If that's the case, I rather use vector<>::size_type to iterate through the elements But why does this happen? I mean, lstLigacao and _m_graf_map[_ond] have exactly the same type, right? So why does the iterator work with one, but not the other? :confused: Best regards Fratelli

          B Offline
          B Offline
          BadKarma
          wrote on last edited by
          #9

          Hi, I have tried your code, and it works. I'm using VS2005. But you don't need to copy the array, you could use a reference to it;

          std::vector<Ligacao *>& refList = _m_graf_map[_ond];
          

          and use the reference

          Learn from the mistakes of others, you may not live long enough to make them all yourself.

          A 1 Reply Last reply
          0
          • N Nibu babu thomas

            AndreFratelli wrote:

            typeded LigacaoVector2d::iterator iterator;

            It's better to use it directly so that readers of your code can understand from where this iterator is being used/to which vector this iterator belongs. Isn't it?

            Nibu babu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

            A Offline
            A Offline
            AndreFratelli
            wrote on last edited by
            #10

            True =) but, on the other hand, if you have a class with the following definitions:

            template <typename T> class MyClass
            {
            typedef vector<T> my_vector;
            typedef my_vector::iterator iterator;
            };

            Then you/others can do something like:

            int main()
            {
            MyClass<type>::iterator it;
            return 0;
            }

            Instead of:

            int main()
            {
            MyClass<type>::my_vector::iterator it; // Or even vector<type>::iterator
            }

            Would you agree that it is practical? regards Fratelli

            N 1 Reply Last reply
            0
            • A AndreFratelli

              True =) but, on the other hand, if you have a class with the following definitions:

              template <typename T> class MyClass
              {
              typedef vector<T> my_vector;
              typedef my_vector::iterator iterator;
              };

              Then you/others can do something like:

              int main()
              {
              MyClass<type>::iterator it;
              return 0;
              }

              Instead of:

              int main()
              {
              MyClass<type>::my_vector::iterator it; // Or even vector<type>::iterator
              }

              Would you agree that it is practical? regards Fratelli

              N Offline
              N Offline
              Nibu babu thomas
              wrote on last edited by
              #11

              AndreFratelli wrote:

              Would you agree that it is practical?

              Yeah, I thought you were letting the iterator typedef float around, if it's in a class then should be fine. :)

              Nibu babu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

              1 Reply Last reply
              0
              • B BadKarma

                Hi, I have tried your code, and it works. I'm using VS2005. But you don't need to copy the array, you could use a reference to it;

                std::vector<Ligacao *>& refList = _m_graf_map[_ond];
                

                and use the reference

                Learn from the mistakes of others, you may not live long enough to make them all yourself.

                A Offline
                A Offline
                AndreFratelli
                wrote on last edited by
                #12

                I actually get another error =/

                e:\projects\netsim\netsim\grafo.cpp(64) : error C2440: 'initializing' : cannot convert from 'const std::vector<_Ty>' to 'std::vector<_Ty> &'
                with
                [
                _Ty=Ligacao *
                ]
                Conversion loses qualifiers

                The code is:

                std::vector &ref = this->_m_graf_map[_onde];

                Regards Fratelli

                B 1 Reply Last reply
                0
                • A AndreFratelli

                  I actually get another error =/

                  e:\projects\netsim\netsim\grafo.cpp(64) : error C2440: 'initializing' : cannot convert from 'const std::vector<_Ty>' to 'std::vector<_Ty> &'
                  with
                  [
                  _Ty=Ligacao *
                  ]
                  Conversion loses qualifiers

                  The code is:

                  std::vector &ref = this->_m_graf_map[_onde];

                  Regards Fratelli

                  B Offline
                  B Offline
                  BadKarma
                  wrote on last edited by
                  #13

                  Hi, I think i got it. The function where you use this list is probably defined as const. Therefore all calls are made to the constant versions of the vector object. Either change the function not to be const (not good) or use

                  const std::vector<ligacao*>& ref = lstTT[1];
                  for (std::vector<ligacao*>::const_iterator it = ref.begin(); it != ref.end(); ++it)
                  

                  Learn from the mistakes of others, you may not live long enough to make them all yourself.

                  A 1 Reply Last reply
                  0
                  • B BadKarma

                    Hi, I think i got it. The function where you use this list is probably defined as const. Therefore all calls are made to the constant versions of the vector object. Either change the function not to be const (not good) or use

                    const std::vector<ligacao*>& ref = lstTT[1];
                    for (std::vector<ligacao*>::const_iterator it = ref.begin(); it != ref.end(); ++it)
                    

                    Learn from the mistakes of others, you may not live long enough to make them all yourself.

                    A Offline
                    A Offline
                    AndreFratelli
                    wrote on last edited by
                    #14

                    It works! :-D You were right, the function is const. Just two things I don't get, though: 1) Why does the iterator must be constant too? 2) Why do you say that making the function not const is not good? I'm asking this because I've seen different opinions in this matter. On one hand, they would argue that making a function const allows the compiler to optimize the function in several ways. On the other hand, I remember reading an article that said that "marking a function const is only a guarantee you're giving to the user that the method will not change the class's attributes" (nor its input, I think...). What do you think? Regards

                    Fratelli

                    B 1 Reply Last reply
                    0
                    • A AndreFratelli

                      It works! :-D You were right, the function is const. Just two things I don't get, though: 1) Why does the iterator must be constant too? 2) Why do you say that making the function not const is not good? I'm asking this because I've seen different opinions in this matter. On one hand, they would argue that making a function const allows the compiler to optimize the function in several ways. On the other hand, I remember reading an article that said that "marking a function const is only a guarantee you're giving to the user that the method will not change the class's attributes" (nor its input, I think...). What do you think? Regards

                      Fratelli

                      B Offline
                      B Offline
                      BadKarma
                      wrote on last edited by
                      #15

                      AndreFratelli wrote:

                      1. Why do you say that making the function not const is not good?

                      - When marking a member function as const. You're telling the user of the class that this call to will not change the internal data of the class. This is 'guaranteed'. - You're alsoo telling the compiler to help you the writer too fulfill this design requirement. The compiler will modify for example a member int m_x; to const int m_x;

                      AndreFratelli wrote:

                      1. Why does the iterator must be constant too?

                      - The compiler has modified the constantness of this member, meaning that this member cannot be changed. Hence you need to use the const version of the iterator;

                      Learn from the mistakes of others, you may not live long enough to make them all yourself.

                      A 1 Reply Last reply
                      0
                      • B BadKarma

                        AndreFratelli wrote:

                        1. Why do you say that making the function not const is not good?

                        - When marking a member function as const. You're telling the user of the class that this call to will not change the internal data of the class. This is 'guaranteed'. - You're alsoo telling the compiler to help you the writer too fulfill this design requirement. The compiler will modify for example a member int m_x; to const int m_x;

                        AndreFratelli wrote:

                        1. Why does the iterator must be constant too?

                        - The compiler has modified the constantness of this member, meaning that this member cannot be changed. Hence you need to use the const version of the iterator;

                        Learn from the mistakes of others, you may not live long enough to make them all yourself.

                        A Offline
                        A Offline
                        AndreFratelli
                        wrote on last edited by
                        #16

                        All clear now =D Thanks a lot ! ;) Best regards

                        Fratelli

                        B 1 Reply Last reply
                        0
                        • A AndreFratelli

                          All clear now =D Thanks a lot ! ;) Best regards

                          Fratelli

                          B Offline
                          B Offline
                          BadKarma
                          wrote on last edited by
                          #17

                          Glad I could help

                          Learn from the mistakes of others, you may not live long enough to make them all yourself.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups