Thread: Invalid Iterator argument

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    114

    Invalid Iterator argument

    Hi, I am trying to create a program that will take 10 numbers and then print the sum of the all two adjacent numbers and then sum of 1st and last,2nd and 2nd last;here is the code
    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    vector<int> input;
    
    for(int i=0;i!=10;i++)
    {
      int x;
      cin>>x;
      input.push_back(x);
    }
    for(auto it=input.begin();it!=input.end()-1;it++)
    {
      int k;
      k=*it+(*(it+1));
      cout<<k<<" "<<endl;
      auto j=input.end()-it;
      cout<<*it+*j<<" "<<endl;
    
    }
    
    }
    .. but at the 2nd output the compiler is giving the error invalid type argument of unary '*'. where am I wrong?

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Since I do not see any experienced guys answering, I am going say that auto is supported in the new standard, so if your compiler does not support it then you can not play with it. But this is just a thought..
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    114
    Quote Originally Posted by std10093 View Post
    Since I do not see any experienced guys answering, I am going say that auto is supported in the new standard, so if your compiler does not support it then you can not play with it. But this is just a thought..
    Auto isnt the problem! my compiler is set to C11. the error messege is given when I tend to dereference j.I dont understand, if dereferencing "it" dont cause any problem.. why would "j" do..

  4. #4
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Not an expert either but when you subtract two pointers (iterators) you are getting the distance between them, not another meaningful pointer. j is type int and not type iterator.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Tamim Ad Dari View Post
    why would "j" do..
    j is not an iterator. when you subtract two iterators, it returns an integer result, indicating the difference between the two iterators.

    so....

    Quote Originally Posted by Tamim Ad Dari View Post
    Auto isnt the problem!
    yes it is, at least indirectly. I'm guessing you used auto because the compiler puked on your attempt to declare it as a std::vector<int>::iterator. the trouble is that now you, as the programmer, don't really know what type j is or why it doesn't let you dereference it.

  6. #6
    Registered User
    Join Date
    Jan 2013
    Posts
    114
    Thanks!! that helped.. I have rewrite the code. though a bit longer. And it worked.
    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    vector<int> input;
    
    for(int i=0;i!=10;i++)
    {
      int x;
      cin>>x;
      input.push_back(x);
    }
     auto j=input.end();
    for(auto it=input.begin();it!=input.end()-1;it++)
    {
      int k;
      k=*it+(*(it+1));
      cout<<k<<" ";
    
      if(j !=input.begin())
      {
          j--;
      cout<<*it+*j<<" ";
      }
    
    
    }
    
    }

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    why are you using end() - 1? end() returns the iterator that is one-past the last item. the only way end() - 1 is correct, is if you do not want to process the last element.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The code uses it[1], so if it == end() - 1 (last iteration), then it[1] == end(), which is wrong.
    Can't say the code looks very pretty, though.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Jan 2013
    Posts
    114
    Quote Originally Posted by Elkvis View Post
    why are you using end() - 1? end() returns the iterator that is one-past the last item. the only way end() - 1 is correct, is if you do not want to process the last element.
    I am using the != syntax! not the < syntax! which means end()-1 is the last element..

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by Tamim Ad Dari View Post
    I am using the != syntax! not the < syntax! which means end()-1 is the last element..
    In this case that difference is actually irrelevant.
    Last edited by whiteflags; 02-10-2013 at 06:15 AM.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    A better solution:
    Code:
    auto it = input.begin();
    if (it != input.end())
    {
        auto j = input.end();
        int prevValue = *it++;
        while (it != input.end())
        {
            cout << (prevValue + *it) << " ";
            --j;
            cout << *it + *j << " ";
            prevValue = *it++;
        }
    }
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. accept() invalid argument
    By hauzer in forum Networking/Device Communication
    Replies: 1
    Last Post: 05-28-2011, 08:56 AM
  2. msgsnd : Invalid argument
    By Krusty in forum C Programming
    Replies: 3
    Last Post: 09-10-2009, 01:50 PM
  3. Pointer to List Iterator As Function Argument
    By bengreenwood in forum C++ Programming
    Replies: 8
    Last Post: 06-17-2009, 05:30 AM
  4. DialogBox Argument Invalid?
    By execute in forum Windows Programming
    Replies: 4
    Last Post: 04-25-2006, 01:56 PM
  5. invalid type argument of `->'
    By BianConiglio in forum C Programming
    Replies: 2
    Last Post: 05-19-2005, 12:57 AM