0

**the first constructor is supposed to take words from a txt file and the second one takes words from the string and add it to fileVec vector.

i'm trying to call a parameterized constructor but it doesn't work, instead it calls the default constructor **

#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
using namespace std;

class stringSet
{
private:
    int i = 0;
    string fileName, line, word, word2;
    istringstream iss;
    fstream file;
    vector<string> fileVec;

public:
    string str = "that's a string for parameterized constructor";

    stringSet()
    {
        cout << "Please enter filename: ";
        getline(std::cin, fileName);
        fileName += ".txt";
        file.open(fileName, ios::in);
        if (file.is_open())
        {
            while (getline(file, line))
            {
                iss.clear();
                iss.str(line);
                while (iss.good())
                {
                    iss >> word;
                    fileVec.push_back(word);
                    cout << fileVec[i] << endl;
                    i++;
                }
            }
        }
    }
    
    stringSet(string str)
    {
        istringstream iss2(str);
        while (iss2 >> word2)
            fileVec.push_back(word2);
        cout << fileVec[i] << endl;
        i++;
    }
};

int main()
{
    // stringSet();
    stringSet(str);
    return 0;
}

that code calls stringSet() instead of stringSet(str)

  • 1
    Your problem is here: `stringSet(str);` Didn't you want `stringSet mySet{"SomeString"};` – drescherjm Nov 09 '22 at 13:39
  • Take a look at what you have written. It seems you want to call the parameterized constructor with the `str` declared in `stringSet`. But that is a member variable, which means that it only exists after a `stringSet` has been constructed. So that doesn't make a lot of sense. I guess the code you are looking for is `stringSet("that's a string for parameterized constructor")` – john Nov 09 '22 at 13:46
  • I think your confusion comes from this `string str = "that's a string for parameterized constructor";` member variable. You expect this to be defined in main but it's not. This is a class member variable and requires an object / instance of the class to access. If it was a static variable you would need a different syntax to use. – drescherjm Nov 09 '22 at 13:46
  • 1
    `stringSet(str);` is equivalent to `stringSet str;`. See, e.g., https://stackoverflow.com/q/26832321/580083 for details. – Daniel Langr Nov 09 '22 at 13:52

1 Answers1

0

Issue similar to most vexing parse.

stringSet(str); // Variable declaration!

declares a variable str.

It is similar to

stringSet str;

You might use {..} syntax:

stringSet{str}; // Temporary created (assuming some std::string str available).
Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • 1
    @drescherjm: misread scope, `stringSet` has a member `str`. So It is even less clear what OP expects from `stringSet(str);`... – Jarod42 Nov 09 '22 at 15:20
  • I think somehow the OP expected `string str = "that's a string for parameterized constructor";` to be a global variable or at least somehow available for use in the parameterized constructor. – drescherjm Nov 09 '22 at 15:22