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)

4
  • 1
    Your problem is here: stringSet(str); Didn't you want stringSet mySet{"SomeString"}; Commented Nov 9, 2022 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") Commented Nov 9, 2022 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. Commented Nov 9, 2022 at 13:46
  • 1
    stringSet(str); is equivalent to stringSet str;. See, e.g., stackoverflow.com/q/26832321/580083 for details. Commented Nov 9, 2022 at 13:52

1 Answer 1

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).
Sign up to request clarification or add additional context in comments.

2 Comments

@drescherjm: misread scope, stringSet has a member str. So It is even less clear what OP expects from stringSet(str);...
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.