Request for member X of Y which is of non-class type Z
When trying to compile the following snippet of C++ code (complete source below)
A::A(istream& i) {
vector<string> words( istream_iterator<int>(i), istream_iterator<int> );
words.begin();
}
I'm getting the error
istream_it.cpp:12: error: request for member ‘begi开发者_高级运维n’ in ‘words’, which is of non-class type
‘std::vector<int, std::allocator<int> >(
std::istream_iterator<int, char, std::char_traits<char>, long int>,
std::istream_iterator<int, char, std::char_traits<char>, long int>)’
I know this error is usually caused by accidentally declaring a function using the no-parameters operator, as in
string s(); s.size();
but in this case, I've already stripped all unnecessary code, and still cant see what exactly is going wrong, or what the correct syntax would be.
Full source:
#include <sstream>
#include <vector>
#include <iterator>
#include <string>
using namespace std;
class A {
public:
A(istream& i) {
vector<int> words(istream_iterator<int>(i), istream_iterator<int> );
words.begin();
}
};
int main(int argc, char** argv)
{
istringstream iss("1 2 3");
A a(iss);
return 0;
}
Correct,
vector<int> words(istream_iterator<int>(i), istream_iterator<int> );
this is a function named words taking two istream_iterator<int>
parameters, one of which named i and the other has no name, and returning vector. Change to this:
vector<int> words((istream_iterator<int>(i)), istream_iterator<int>() );
The first parentheses added (istream_iterator<int>(i))
make it an expression so there will be no ambiguity. The other parentheses istream_iterator<int>()
are mandatory because you construct a temporary. A type by itself, like istream_iterator<int>
, doesn't construct a temporary.
精彩评论