lundi 8 juillet 2019

How to hand 2D vector created from file to class constructor?

I'm trying to pass a 2D vector from main to a class' constructor. The vector is created and instantiated from main, but needs to be used in the class.

I've tried replicating the issue with a small bit of code which hasn't been successful:

class ExampleClass {

public:
    ExampleClass(vector<string> fileVector) {
        vector<string> vector;
        vector = fileVector;
    }

    void printVector() {
        cout << "vector[1][1]" << vector[1][1] << endl;
    }
};



int main()
{
string fileName;
ifstream inputFile;

cout << "Enter file name: " << endl;
cin >> fileName;


inputFile.open("filename");
vector<string> fileVector{ istream_iterator<string>(inputFile), 
istream_iterator<string>() };



    ExampleClass example(fileVector);
    example.printVector;
    inputFile.close();
}

I'd expect the fileVector from main to be accessible in ExampleClass, where it is copied to vector. Instead, vector throws an error:

"argument list for class template "std::vector" is missing"

Aucun commentaire:

Enregistrer un commentaire