feeding a stringstream to a class member function

时间:2017-08-13 13:45:13

标签: c++11 stringstream

I am a newbie trying to learn by doing. I want to feed a stringstream into a class member function called "print()" but I get errors. Once this works, I can proceed to write more class member functions that work with the data I feed them.

For now I have created a class that has a member function 'print'.

class Month
{
public:
string m_month;

void print()
{
cout << m_month << endl;
}

};

Next, I initialized 12 months:

Month month1 = { "January" };
Month month2 = { "February" };
Month month3 = { "March" };
etc.

When I call "month1.print();" it prints January which is correct.

I used stringstream and a for loop to concatenate month + 1 to 12 and I want to feed the stringstream to the print function.

stringstream os; 
string mValue = "month"; 
int iValue = 1; 

for(int i = 0; i < 12; ++i) 
{
    os << mValue << "" << iValue << "\n"; 
    iValue += 1; 
}

However, the stringstream can't be combined with the print function.

os.print(); and os.str().print();

result in "error: ‘std::stringstream {aka class std::__cxx11::basic_stringstream}’ has no member named ‘print’"

Converting the stringstream to char and then feeding it into the print function results in "error: request for member ‘print’ in ‘cstr’, which is of non-class type ‘const char*’"

const string tmp = os.str();
const char* cstr = tmp.c_str();

cstr.print();

Long story short: What I am trying to do is concatenate month + 1 to 12 and feed that to the class member function "print". This seems trivial but I can't get it to work. Any suggestions?

Edit: Full code:

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

class Month
{
public:
string m_month;


void print()
{
cout << m_month << endl;
}

};

int main()
{
Month month1 = { "January" };
Month month2 = { "February" };
Month month3 = { "March" };
Month month4 = { "April" };
Month month5 = { "May" };
Month month6 = { "June" };
Month month7 = { "July" };
Month month8 = { "August" };
Month month9 = { "September" };
Month month10 = { "October" };
Month month11 = { "November" };
Month month12 = { "December" };

stringstream os; // Initialize stringstream "os"
string mValue = "month"; // Initialize mValue "month"
int iValue = 1; // Initialize iValue "1"

for(int i = 0; i < 12; ++i) 
{
os << mValue << "" << iValue << "\n"; // Glue mValue and iValue    
// together
iValue += 1; // Increment iValue by one
}

send stringstream "os" to the print function // mock code: Here I want to send month1.print(); month2.print(); etc. to the print function. The output should be January, February etc. 

return 0;
}

2 个答案:

答案 0 :(得分:1)

这不符合你的想法:

for(int i = 0; i < 12; ++i) 
{
    // iValue is actually unnecessary. You could have just used (i + 1)
    os << mValue << "" << iValue << "\n"; 
    iValue += 1;
}

所有这一切都是用字符串填充stringstream:

"month1\nmonth2\nmonth3\nmonth4\nmonth5\nmonth6\nmonth7\nmonth8\nmonth9\nmonth10\nmonth11\nmonth12"

您的意图似乎是将一个数字连接到"month"字符串的末尾,并让它们充当您在上面定义的month1month2 ...变量。这不是它的工作原理。你不能(也不应该)尝试“动态”引用这样的变量。在os.print();中,字符串流不会充当Month,因为它包含一个与Month变量同名的字符串。

相反,将变量添加到某种容器(如std::vector),并循环遍历它:

std::vector<Month> months{ month1, month2, month3, ..., month12 }

for (unsigned int i = 0; i < months.size(); i++)
{
    months[i].print();
}

答案 1 :(得分:0)

字符串流应该被视为与其他任何流一样的流,除了它恰好是文本并保存在内存中。因此将它转换为字符串很便宜,实际上它们通常用于构建字符串。

但是&#34; Print&#34;类的方法没有业务知道流是字符串流。所有它应该关心的是它获得一个文本流,并输入。事实上,由于历史上的弱点可以追溯到很长一段时间,前者有点难以执行。如果你只是逐字节地读取流,传递给std :: cout,并在EOF上终止那么那可能没问题。