2017-08-13 35 views
0

我是一個試圖通過做學習的新手。我想給一個名爲「print()」的類成員函數提供一個stringstream,但是我得到錯誤。一旦這個工作,我可以繼續編寫更多的類成員函數來處理我提供的數據。給一個類成員函數提供一個字符串流

現在我已經創建了一個具有成員函數'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();」它打印一月份是正確的。

我使用了stringstream和for循環來連接month + 1到12,並且我想將stringstream饋送給打印函數。

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

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

但是,stringstream不能與print函數結合使用。

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

導致 「錯誤: '的std :: stringstream的{又名類的std :: __ cxx11 :: basic_stringstream}' 沒有名爲成員 '打印'」

轉換的字符串流爲char,然後餵養它進入打印功能結果「錯誤:請求成員在‘CSTR’打印',這是‘爲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; 
} 
+0

很難理解你想用'os.print();'來做什麼。打印不是字符串流的方法,正如錯誤所述。你是否打算將stringstream字符串分配給'm_month'?另外,你永遠不會使用月份變量。我不確定你的iValue循環應該做什麼。請澄清。 – Carcigenicate

+0

等一下,你是否想用mValue for-loop獲取月份對象? – Carcigenicate

+0

我可能在這裏沒有使用正確的術語,但我所做的是我創建了一個具有成員'm_month'和'print'的類。我已經初始化了1到12個月,所以當我打電話'month1.print();'它打印'1月'; 'month2.print();'打印'二月'等。現在我想讓程序打印'1月,2月,3月等',而不必鍵入'month1.print();到month12.print();這就是for循環的目的。它遞增並連接'month1到12',這是我想要輸入到打印功能中的。我將在原帖中發佈完整的代碼。 – TwoGoldFish

回答

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

這一切確實是充滿字符串的字符串流:

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

您的意圖似乎是將一個數字連接到一個"month"字符串的末尾,並讓它們作爲第e 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(); 
} 
+0

「你的意圖似乎是將一個數字連接到」月「字符串的末尾,並讓它們充當上面定義的month1,month2 ...變量......」是的,那是我的意圖。謝謝你建議使用std :: vector。我會試一試。我來自Powershell背景,以「自動化所有事物」爲理念。動態創建變量的另一個原因是通過避免冗餘來保持我的代碼簡潔。 – TwoGoldFish

+0

@TwoGoldFish如果你需要通過一個數字引用一個變量,把它放在一個索引容器中(比如'vector'),並通過索引引用它。如果您需要通過數字以外的方式引用它,請將其放入地圖中,然後按鍵查找。我不知道PowerShell,但如果你想要的是PS中的常見做法,那麼你需要改變你的想法。 Afaik除非使用調試標誌進行編譯,否則無論如何編譯時都會丟失所有的名稱信息,所以這在C++中甚至是不可能的。 – Carcigenicate

+0

你也可以使用HashMap來處理所有事情,儘管它不一定能夠提供最好的性能。看看標準的庫結構。處理這種情況有很多選擇。 – Carcigenicate

0

一個字符串流應該被看作是一個像任何其他數據流,但它發生在成爲文本並保存在內存中。所以將其轉換爲字符串很便宜,實際上它們通常用於構建字符串。

但是類的「打印」方法沒有業務知道流是一個串流。所有它應該關心的是它得到一個文本流,並輸入。事實上,由於歷史上的弱點可能會延續很久,前者有點難以執行。如果您只是逐字節讀取流,則傳遞給std :: cout,然後在EOF上終止,那麼這可能是確定的。

相關問題