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

This is a discussion on Why does the compiler complain when trying to construct this vector. within the C++ Programming forums, part of the General Programming Boards category; basically i have a code like this Code: #include <iostream> #include <vector> #include <iterator> #include <algorithm> int main() { using ...

  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,317
    >> 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
    21,712
    One possible solution is to wrap the arguments in an extra pair of parentheses.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,893
    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
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    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 05: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, 09:14 AM
  5. Operators for 3D Vector Mathematics
    By Anarchist in forum C++ Programming
    Replies: 10
    Last Post: 01-31-2003, 06:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21