0
#include<iostream>
#include<string>
#include<cstdlib>
#include<sstream>
#include<vector>
#include <iterator>
using namespace std;

int main()
{
    string str;
    unsigned int i;
    vector<string> arr;
    getline(cin,str);

    istringstream it(str);
    vector<string> arr(istream_iterator<string>(),istream_iterator<string>(it));
    arr.push_back('\0');

    //boost::split(arr, str, [](char c){return c == ' ';});
    //auto splitText = str | view::split(' ');
    for(i=0; i<arr.size(); i++){
        cout<<arr[i]<<endl;
    }

    return 0;
}

The above code shows an error as follows: error: 'std::vector > arr(std::istream_iterator > (*)(), std::istream_iterator >)' redeclared as different kind of symbol

when building with g++. Any help or hint would be highly appreciated.

  • While the title of the presumed duplicate question points into the right direction, it is a very bad example. I would be surprised, if a beginner learns anything at all from it. – Olaf Dietsche Aug 17 '18 at 21:04

1 Answers1

0

When running g++, it also hints at where the problem lies:

a.cpp:13:20: note: previous declaration 'std::vector<std::__cxx11::basic_string<char> > arr' 
     vector<string> arr; 
                    ^~~

This means, you declared arr twice. First at line 13 and a second time at 17. This is what

redeclared as different kind of symbol

means.


Removing the first declaration at line 13, fixes this error message, but reveals the next problem

a.cpp:18:9: error: request for member 'push_back' in 'arr', which is of non-class type 'std::vector<std::__cxx11::basic_string<char> >(std::istream_iterator<std::__cxx11::basic_string<char> > (*)(), std::istream_iterator<std::__cxx11::basic_string<char> >)'
 arr.push_back('\0');
     ^~~~~~~~~

This is more complicated, and is why @DietmarKühl closed the question as a duplicate to question Most vexing parse: why doesn't A a(()); work?. The second answer is the most useful in this case and solves it by using curly braces around the parameters

vector<string> arr{istream_iterator<string>(),istream_iterator<string>(it)};
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198