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. iterator & operator overloading error

iterator & operator overloading error

Scheduled Pinned Locked Moved C / C++ / MFC
c++helplinuxregex
12 Posts 2 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.
  • K khomeyni

    hello can anyone help me why this takes errors? #include <stdio.h> #include<iostream> #include<list> #include<iterator> using namespace std; class code_ch{ public: char ch; int number; code_ch(){ ch=NULL; number=0; } code_ch(int n,char ach){ ch=ach; number=n; } friend istream &operator >> (istream &is,const code_ch &C){ cin>>C.number>>C.ch; return is; } friend ostream &operator << (ostream &os,const code_ch &C){ cout<<C.ch<<" number="<<C.number; return os; } bool operator < (code_ch &h2){ if(number<h2.number) return true; return false; } bool operator > (code_ch &h2){ if(number>h2.number) return true; return false; } bool operator <= (code_ch &h2){ if(number<=h2.number) return true; return false; } bool operator >= (code_ch &h2){ if(number>=h2.number) return true; return false; } bool operator == (code_ch &h2){ if(number==h2.number) return true; return false; } }; int main(int argc,char **argv){ if(argc==1){ cout<<"you haven't entered a file! exiting..."<<endl; return 1; } list<code_ch>w; int *a=new int [200];//for saving the number of each ch for(int i=0;i<200;i++) a[i]=0; list<code_ch>::iterator l=w.begin(); cout<<"the list is:"<<endl; copy(l.begin(),l.end(),ostream_iterator<code_ch>(cout," ")); } and the error: [sajad@sajad Desktop]$ g++ c++file.cpp c++file.cpp: In function ‘std::istream& operator>>(std::istream&, const code_ch&)’: c++file.cpp:19: error: ambiguous overload for ‘operator>>’ in ‘std::cin >> C->code_ch::number’ /usr/lib/gcc/i586-redhat-linux/4.4.0/../../../../include/c++/4.4.0/istream:119: note: candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>& (*)(std::basic_istream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>] <near match> /usr/lib/gcc/i586-redhat-linux/4.4.0/../../../../include/c++/4.4.0/istream:123: note: std::basic_istream<_CharT, _Tr

    A Offline
    A Offline
    Avi Berger
    wrote on last edited by
    #2

    This isn't the problem you are asking about, but shouldn't your operator >> and operator << be using the stream passed to it as an argument instead of using cin/cout?

    K 1 Reply Last reply
    0
    • A Avi Berger

      This isn't the problem you are asking about, but shouldn't your operator >> and operator << be using the stream passed to it as an argument instead of using cin/cout?

      K Offline
      K Offline
      khomeyni
      wrote on last edited by
      #3

      if you mean this yet the same error: friend istream &operator >> (istream &is,const code_ch &C){ is>>C.number>>C.ch; return is; } friend ostream &operator << (ostream &os,const code_ch &C){ os<<C.ch<<" number="<<C.number; return os; } or : `friend istream &operator >> (istream &is,const code_ch &C){ return is>>C.number>>C.ch; //return is; } friend ostream &operator << (ostream &os,const code_ch &C){ return os<<C.ch<<" number="<<C.number; //return os; }`

      A 1 Reply Last reply
      0
      • K khomeyni

        if you mean this yet the same error: friend istream &operator >> (istream &is,const code_ch &C){ is>>C.number>>C.ch; return is; } friend ostream &operator << (ostream &os,const code_ch &C){ os<<C.ch<<" number="<<C.number; return os; } or : `friend istream &operator >> (istream &is,const code_ch &C){ return is>>C.number>>C.ch; //return is; } friend ostream &operator << (ostream &os,const code_ch &C){ return os<<C.ch<<" number="<<C.number; //return os; }`

        A Offline
        A Offline
        Avi Berger
        wrote on last edited by
        #4

        Yes, that is what I was meant. As I said, it wasn't the problem that you were asking about, so it doesn't surprise me that it gives you the same error message. It was still a problem. I just spotted something.

        khomeyni wrote:

        friend istream &operator >> ( istream &is, const code_ch &C )
        {
        is >> C.number >> C.ch;
        return is;
        }

        This function changes the code_ch object passed to it. So the parameter can not be const.

        K 1 Reply Last reply
        0
        • A Avi Berger

          Yes, that is what I was meant. As I said, it wasn't the problem that you were asking about, so it doesn't surprise me that it gives you the same error message. It was still a problem. I just spotted something.

          khomeyni wrote:

          friend istream &operator >> ( istream &is, const code_ch &C )
          {
          is >> C.number >> C.ch;
          return is;
          }

          This function changes the code_ch object passed to it. So the parameter can not be const.

          K Offline
          K Offline
          khomeyni
          wrote on last edited by
          #5

          ok thanks it solve my first problem but yet: [sajad@sajad Desktop]$ g++ c++file.cpp c++file.cpp: In function ‘int main(int, char**)’: c++file.cpp:66: error: ‘struct std::_List_iterator<code_ch>’ has no member named ‘begin’ c++file.cpp:66: error: ‘struct std::_List_iterator<code_ch>’ has no member named ‘end’

          K A 2 Replies Last reply
          0
          • K khomeyni

            ok thanks it solve my first problem but yet: [sajad@sajad Desktop]$ g++ c++file.cpp c++file.cpp: In function ‘int main(int, char**)’: c++file.cpp:66: error: ‘struct std::_List_iterator<code_ch>’ has no member named ‘begin’ c++file.cpp:66: error: ‘struct std::_List_iterator<code_ch>’ has no member named ‘end’

            K Offline
            K Offline
            khomeyni
            wrote on last edited by
            #6

            oh sorry ,it is a part of huge program so i only copy it and no member is in the list.but when i fix it and add: for(int i=0;i<10;i++){ code_ch *h=new code_ch; h->number=i; h->ch='1'; w.push_front(*h); } or : code_ch H; H.number=10; H.ch='k'; w.push_front(H); yet the same problem.

            1 Reply Last reply
            0
            • K khomeyni

              ok thanks it solve my first problem but yet: [sajad@sajad Desktop]$ g++ c++file.cpp c++file.cpp: In function ‘int main(int, char**)’: c++file.cpp:66: error: ‘struct std::_List_iterator<code_ch>’ has no member named ‘begin’ c++file.cpp:66: error: ‘struct std::_List_iterator<code_ch>’ has no member named ‘end’

              A Offline
              A Offline
              Avi Berger
              wrote on last edited by
              #7

              True, you still have this problem. Why don't you take a closer look at it.

              khomeyni wrote:

              int main(int argc,char **argv)
              {
              if(argc==1)
              {
              cout<<"you haven't entered a file! exiting..."<<endl;
              return 1;
              }
              list<code_ch>w;
              int *a=new int [200];//for saving the number of each ch
              for(int i=0;i<200;i++)
              a[i]=0;

              list<code\_ch>::iterator l=w.begin();
              cout<<"the list is:"<<endl;
              copy(l.begin(),l.end(),ostream\_iterator<code\_ch>(cout," "));
              

              }

              ; And your error message was:

              khomeyni wrote:

              c++file.cpp: In function ‘int main(int, char**)’: c++file.cpp:66: error: ‘struct std::_List_iterator’ has no member named ‘begin’ c++file.cpp:66: error: ‘struct std::_List_iterator’ has no member named ‘end’

              That says that your iterator does not have a begin or end member. It is right, iterators don't. What does have a begin and end? What is it that you want to be copying from? You should be able to get it from here.

              K 1 Reply Last reply
              0
              • A Avi Berger

                True, you still have this problem. Why don't you take a closer look at it.

                khomeyni wrote:

                int main(int argc,char **argv)
                {
                if(argc==1)
                {
                cout<<"you haven't entered a file! exiting..."<<endl;
                return 1;
                }
                list<code_ch>w;
                int *a=new int [200];//for saving the number of each ch
                for(int i=0;i<200;i++)
                a[i]=0;

                list<code\_ch>::iterator l=w.begin();
                cout<<"the list is:"<<endl;
                copy(l.begin(),l.end(),ostream\_iterator<code\_ch>(cout," "));
                

                }

                ; And your error message was:

                khomeyni wrote:

                c++file.cpp: In function ‘int main(int, char**)’: c++file.cpp:66: error: ‘struct std::_List_iterator’ has no member named ‘begin’ c++file.cpp:66: error: ‘struct std::_List_iterator’ has no member named ‘end’

                That says that your iterator does not have a begin or end member. It is right, iterators don't. What does have a begin and end? What is it that you want to be copying from? You should be able to get it from here.

                K Offline
                K Offline
                khomeyni
                wrote on last edited by
                #8

                if you mean this? adding this in the main: for(int i=0;i<10;i++){ code_ch *h=new code_ch; h->number=i; h->ch='1'; w.push_front(*h); } or : code_ch H; H.number=10; H.ch='k'; w.push_front(H); yet the same problem.

                A 1 Reply Last reply
                0
                • K khomeyni

                  if you mean this? adding this in the main: for(int i=0;i<10;i++){ code_ch *h=new code_ch; h->number=i; h->ch='1'; w.push_front(*h); } or : code_ch H; H.number=10; H.ch='k'; w.push_front(H); yet the same problem.

                  A Offline
                  A Offline
                  Avi Berger
                  wrote on last edited by
                  #9

                  No, I don't mean that. Play closer attention to the error message and the line it is referring to. It is telling you what the problem is and where it is. Go over each part of that line and look for a difference between what you intended and what you wrote.

                  K 1 Reply Last reply
                  0
                  • A Avi Berger

                    No, I don't mean that. Play closer attention to the error message and the line it is referring to. It is telling you what the problem is and where it is. Go over each part of that line and look for a difference between what you intended and what you wrote.

                    K Offline
                    K Offline
                    khomeyni
                    wrote on last edited by
                    #10

                    i can not understand: even for this does not work: list<int> PL; PL.push_front(100); list<int>::iterator p=PL.begin(); copy(p.begin(),p.end(),ostream_iterator<int>(cout," ")); cout<<endl; while in another where i wrote: list<code_ch>::iterator p=t.begin(); int i=0; while(p!=t.end()){ heap[i]=*p; p++; i++; heapsize++; } //part of a program and correctly works. does list has not such ability to iterate over list elements as i want to do?

                    K 1 Reply Last reply
                    0
                    • K khomeyni

                      i can not understand: even for this does not work: list<int> PL; PL.push_front(100); list<int>::iterator p=PL.begin(); copy(p.begin(),p.end(),ostream_iterator<int>(cout," ")); cout<<endl; while in another where i wrote: list<code_ch>::iterator p=t.begin(); int i=0; while(p!=t.end()){ heap[i]=*p; p++; i++; heapsize++; } //part of a program and correctly works. does list has not such ability to iterate over list elements as i want to do?

                      K Offline
                      K Offline
                      khomeyni
                      wrote on last edited by
                      #11

                      ok i must write: copy(x.begin(),w.end(),ostr.....) very simple error and stupidly error !!! thanks.

                      A 1 Reply Last reply
                      0
                      • K khomeyni

                        ok i must write: copy(x.begin(),w.end(),ostr.....) very simple error and stupidly error !!! thanks.

                        A Offline
                        A Offline
                        Avi Berger
                        wrote on last edited by
                        #12

                        Good. You got it. Though of course its: copy(w.begin(),w.end(),ostr.....)

                        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