This program reads information from a text file with an istream_iterator and initializes a vector with the data. Everything has been working fine, but when I created a constructor for the class to use for insertion into the vector, I get a compiler error involving the istream_iterator.

#include <vector>
#include <fstream>
#include <string>
#include <iterator>
#include <iostream>
#include <algorithm>
using namespace std;


struct Item {
    string name;
    int iid;
    double value;
    Item(string n, int id, double p) :name(n), iid(id), value(p) {}
    /* ... */
};

struct namesort {   // name function object
    bool operator()(const Item& a, const Item& b) const
    { return a.name < b.name; }
};

struct idsort {
    bool operator()(const Item& a, const Item& b) const
    { return a.iid < b.iid; }
};

struct valuesort {
    bool operator()(const Item& a, const Item& b) const
    { return a.value > b.value; }
};

istream& operator>>(istream& in, Item& i)
{
    in >> i.name >> i.iid >> i.value;
    return in;
}

ostream& operator<<(ostream& out, const Item& o)
{
    out << o.name << '\t' << o.iid << '\t' << o.value;
    return out;
}

int main()
{
    string file;
    cin >> file;

    ifstream is(file.c_str());

    istream_iterator<Item> ii(is);
    istream_iterator<Item> eos;
    vector<Item> vi(ii,eos);
    vector<Item>::iterator it = vi.begin();

    sort(vi.begin(), vi.end(), valuesort());
    vi.insert(it, Item("horse shoe", 99, 12.34));
    vi.insert(it, Item("Canon S400", 9988, 499.95));
    for (int i = 0; i < vi.size(); ++i)
        cout << vi[i] << endl;
}

The code works if you take out the constructor (and obviously the insert), but for some reason when I try to compile with the constructor I get this message.

C:\Program Files (x86)\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\bits\stream_iterator.h|68|error: no matching function for call to `Item::Item()'|

Any help is appreciated. Thanks

Recommended Answers

All 3 Replies

If you specify a default contsructor for you Item struct definition, this error will go away:

Item(void) :name(""), iid(0), value(0.0) {}

If you specify a default contsructor for you Item struct definition, this error will go away:

Item(void) :name(""), iid(0), value(0.0) {}

Thanks! Need to take note of my class construction guidelines.

No problem. If that answered you questions, please mark as solved.

Cheers!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.