Thread: Why does the compiler complain when trying to construct this vector.

  1. #1
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901

    Why does the compiler complain when trying to construct this vector.

    basically i have a code like this

    Code:
    #include <iostream>
    #include <vector>
    #include <iterator>
    #include <algorithm>
    
    
    int main()
    {
    	using namespace std;
    
    	vector<int> vec(istream_iterator<int>(cin), istream_iterator<int>());
            copy(vec.begin(), vec.end(), ostream_iterator<int>(cout, "-"));
    	return 0;
    }
    when I try to compile I get the errors
    error: request for member `begin' in `vec', which is of non-class type `std::vector<int, std::allocator<int> > ()(std::istream_iterator<int, char, std::char_traits<char>, ptrdiff_t>, std::istream_iterator<int, char, std::char_traits<char>, ptrdiff_t> (*)())' Test main.cpp line 12 1168529845046 1101

    error: request for member `end' in `vec', which is of non-class type `std::vector<int, std::allocator<int> > ()(std::istream_iterator<int, char, std::char_traits<char>, ptrdiff_t>, std::istream_iterator<int, char, std::char_traits<char>, ptrdiff_t> (*)())' Test main.cpp line 12 1168529845046 1102
    Now I usually get that error when explicitly using the () when constructing and trying to call a member for it, but this time I get it even when I propperly use a constructor with arguments.



    I changed the code to this, which works

    Code:
    	vector<int> vec = 
    		vector<int>(istream_iterator<int>(cin), istream_iterator<int>());
            copy(vec.begin(), vec.end(), ostream_iterator<int>(cout, "-"));
    But why?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> vector<int> vec(istream_iterator<int>(cin), istream_iterator<int>());
    This line is declaring a function named vec that returns a vector<int>. It is similar to why you cannot use empty parentheses when declaring an object (e.g. vector<int> vec();).

    I don't remember where I saw the more detailed explanation, perhaps in Sutter's gotw or in the C++ FAQ Lite.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    One possible solution is to wrap the arguments in an extra pair of parentheses.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Meyers's Effective STL, Item #1, "Prepare for C++'s most vexing parse."

    Try this one, as laserlight suggested.
    Code:
    vector<int> vec((istream_iterator<int>(cin)), istream_iterator<int>());
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Guru of the Week #75 is almost identical to your problem, and yes it's because it's treating vec as a function prototype.

    http://www.gotw.ca/gotw/075.htm
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  6. #6
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    I see, I feel kind of glad that It's not something so easily seen. Thanks for the solution.

    I also noticed it works propperly if I pre-declare the istream iterators or ostream iterators before hand and pass them to the constructor.
    Last edited by indigo0086; 01-12-2007 at 06:35 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting vector via recursive functions
    By porsche911nfs in forum C++ Programming
    Replies: 18
    Last Post: 05-04-2009, 06:54 AM
  2. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 08:21 AM
  3. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 12:34 PM
  4. my vector class..easy dynamic arrays
    By nextus in forum C++ Programming
    Replies: 5
    Last Post: 02-03-2003, 10:14 AM
  5. Operators for 3D Vector Mathematics
    By Anarchist in forum C++ Programming
    Replies: 10
    Last Post: 01-31-2003, 07:33 PM