**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)
stringSet(str);Didn't you wantstringSet mySet{"SomeString"};strdeclared instringSet. But that is a member variable, which means that it only exists after astringSethas been constructed. So that doesn't make a lot of sense. I guess the code you are looking for isstringSet("that's a string for parameterized constructor")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.stringSet(str);is equivalent tostringSet str;. See, e.g., stackoverflow.com/q/26832321/580083 for details.