C++11 将stringstream馈送给类成员函数

C++11 将stringstream馈送给类成员函数,c++11,stringstream,C++11,Stringstream,我是一个新手,试图通过实践来学习。我想将stringstream馈送到名为print的类成员函数中,但出现了错误。一旦这样做了,我就可以继续编写更多的类成员函数来处理我提供给它们的数据 现在,我已经创建了一个具有成员函数“print”的类 class Month { public: string m_month; void print() { cout << m_month << endl; } }; 当我打电话给month1.print;它打印的是一月,这是正确的

我是一个新手,试图通过实践来学习。我想将stringstream馈送到名为print的类成员函数中,但出现了错误。一旦这样做了,我就可以继续编写更多的类成员函数来处理我提供给它们的数据

现在,我已经创建了一个具有成员函数“print”的类

class Month
{
public:
string m_month;

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

};
当我打电话给month1.print;它打印的是一月,这是正确的

我使用stringstream和for循环将月份+1连接到12,我想将stringstream提供给print函数

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

for(int i = 0; i < 12; ++i) 
{
    os << mValue << "" << iValue << "\n"; 
    iValue += 1; 
}
结果出现错误:“std::stringstream{aka class std::uu cxx11::basic_stringstream}”没有名为“print”的成员

将stringstream转换为char,然后将其馈送到print函数会导致错误:“cstr”中的成员“print”请求,该成员属于非类类型“const char*”

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

cstr.print();
长话短说:我试图做的是将月份+1连接到12,并将其提供给类成员函数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,month2。。。您在上面定义的变量。这不是它的工作原理。您不能也不应该尝试这样动态引用变量。在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应该像其他流一样被视为流,只是它碰巧是文本并保存在内存中。因此,将其转换为字符串很便宜,事实上,它们通常用于构建字符串


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

很难理解您试图用os.print;做什么;。正如错误所说,print不是stringstream的方法。您是否打算将stringstream字符串分配给m_month?此外,您从未使用月份变量。我不知道你和艾瓦鲁的关系该怎么办。请澄清。等等,您是否正在尝试获取带有mValue for循环的month对象?我可能在这里没有使用正确的术语,但我所做的是创建了一个包含成员“m_month”和“print”的类。我已经初始化了第1个月到第12个月,所以当我调用'month1.print;'它打印“一月”月2.打印打印“二月”等等。现在,我希望程序打印“一月、二月、三月等”,而不必键入“month1.print;到12月1日打印;这就是for循环的作用。它递增并连接“month1到12”,我想将其输入到print函数中。我将在原始帖子中发布完整的代码。你的意图是让这些代码真正成为月份变量吗?在当前代码中,您从不使用月份。。。变量。您的目的似乎是将一个数字连接到一个月字符串的末尾,并让它们充当month1,month2。。。上面定义的变量。。。是的,这是我的意图。感谢您建议使用std::vector。我试试看。我来自Powershell背景,有着“自动化所有事情”的心态。动态创建变量的另一个原因是通过避免冗余来保持代码简洁。@TwoGoldFish如果您需要通过数字引用变量,请将其放入索引容器(如向量)中,然后通过索引引用它。如果您需要通过数字以外的其他方式来引用它,请将它放在地图中并按键查找。我不知道PowerShell,但如果您尝试的是PS中的常见做法,则需要改变您的想法。AFEK,除非你用调试标志编译,否则,当编译时,你将丢失所有的名字信息,所以C++中甚至不可能。你也可以使用一个哈希图来处理所有的事情,尽管它不一定能提供最好的性能。看看标准的库结构。处理这种情况有很多选择。当然!如果我想创建很多变量,比如说100个,我应该用一个循环创建它们,并将它们存储在一个向量中,然后通过索引号引用它们。谢谢你的洞察力,谢谢你的努力。非常感谢。
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"
std::vector<Month> months{ month1, month2, month3, ..., month12 }

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