将字符串流提供给 class 成员函数

feeding a stringstream to a class member function

我是一个尝试边做边学的新手。我想将 stringstream 提供给名为 "print()" 的 class 成员函数,但出现错误。一旦成功,我就可以继续编写更多 class 成员函数来处理我提供给它们的数据。

现在我已经创建了一个 class,它有一个成员函数 'print'。

class Month
{
public:
string m_month;

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

};

接下来我初始化12个月:

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

当我调用 "month1.print();" 时,它打印正确的一月。

我使用字符串流和 for 循环将月份 + 1 连接到 12,我想将字符串流提供给打印函数。

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

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

但是,stringstream 不能与打印功能结合使用。

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

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

将 stringstream 转换为 char,然后将其送入 print 函数会导致 "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();

长话短说:我想做的是将月份 + 1 连接到 12 并将其提供给 class 成员函数 "print"。这看起来微不足道,但我无法让它发挥作用。有什么建议么?

编辑:完整代码:

#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;
}

这与您认为的不同:

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

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

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

您的意图似乎是将一个数字连接到 "month" 字符串的末尾,并让它们充当您定义的 month1month2... 变量多于。它不是这样工作的。您不能(也不应该)尝试 "dynamically" 那样引用变量。在 os.print(); 中,stringstream 不作为 Month 只是因为它包含一个与 Month 变量同名的字符串。

相反,将变量添加到某种容器(如 std::vector),然后对其进行循环:

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

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

stringstream 应该被认为是与其他任何流一样的流,除了它恰好是文本并保存在内存中。所以把它转换成字符串很便宜,事实上它们经常被用来构建字符串。

但是 class 的 "Print" 方法没有必要知道流是字符串流。它应该关心的只是它得到一个文本流,并且是输入的。事实上,由于历史悠久的弱点,前者有点难以执行。如果您只是逐字节读取流,传递给 std::cout,并在 EOF 处终止,那么这可能没问题。