我正在學習C++,在使用類時非常新,在嘗試使用它們時我感到非常困惑。我試圖轉換我現有的代碼(它使用了結構),以便它使用類 - 所以當我知道我在做什麼時,我不知道我是否正確地做了它。使用類功能
有人告訴我,當使用類中的函數時,我首先需要實例化類的一個對象。所以我在我的主要功能中嘗試過(一個片段)是:
int main()// line 1
{
string message_fr_client = "test"; //line2
msgInfo message_processed; //line 3
message_processed.incMsgClass(message_fr_client); //line 4
if (!message_processed.priority_check(qos_levels, message_processed)) //line 5
cout << "failure: priority level out of bounds\n"; //line 6
return 0; //line 7
}
你能幫我澄清一下,我的下列假設是正確的嗎?編譯器沒有顯示任何錯誤,所以我不知道它是否沒有錯誤,或者是否有潛在的錯誤。
- 在第4行中,對字符串
message_fr_client
正在執行的功能和incMsgClass
返回所得到的(改性)message_processed
? - 在第5行,函數
priority_check
正在message_processed
上執行並返回一個布爾值? - 在我的班級定義中,我有一個功能
getPath
,意思是修改nodePath
的值 - 這僅僅是使用message_processed.getPath(/*arguments*/)
的問題嗎?
我沒有包括的功能的身體,因爲我知道他們的工作 - 我只是想找出如何類的功能進行交互。請讓我知道,如果我可以更清楚 - 只是想澄清一些混淆。
這是我的類:
#ifndef clientMsgHandling_H
#define clientMsgHandling_H
#include <list>
#include <map>
#include <queue>
class msgInfo
{
public:
msgInfo();
msgInfo(int, int, int, std::string, std::list<int>);
/*classifying message*/
msgInfo incMsgClass(std::string original_msg);
/*message error checks*/
bool priority_check(int syst_priority, msgInfo msg); //check that message is within qos levels
bool route_check(std::map<std::pair<int, int>, int> route_table, msgInfo msg); //check that route exists
void getPath(msgInfo msg, std::map<std::pair<int, int>, int> route_info, int max_hop);
private:
int source_id;
int dest_id;
int priority;
std::string payload;
std::list<int> nodePath;
};
#endif
對不起,我不能接受多個答案,但謝謝大家! - 所有位和bobs幫助 – sccs 2013-03-27 07:25:33