我一直在嘗試學習一些關於C++中的套接字編程的知識,因此一直在開發基本的IRC bot。我有它連接和工作,但我有一個問題,確定誰加入了通道(即是機器人本身,隨機用戶或我的JOIN命令)。變量賦值重寫其他變量的值
我使用的克利翁在Windows 7
我有下面的代碼:
//determine if message is JOIN command
if(regex_search(line, match, std::regex(R"(:([^!]*)!.*?JOIN)")))
{
//get the nickname of the bot (details.getNickName() shown in next code segment)
const char *trueNick = details.getNickName();
//reformat nickname because the following is returned from the method alone
//0x10042d0f9 <_ZStL6ignore+119> "CPlusPlusBotTest"
const char *nick = string(details.getNickName()).c_str();
//get the name of the user who joined
const char *name = match.str(1).c_str();
//debugging
std::cout << name << " - name\n";
std::cout << nick << " - nick\n";
//might not be the correct way to compare the two? but I'll sort that out later
if(name != nick)
{
//welcome the user
char param[1024];
sprintf(param, "%s :Hello, %s", details.getChannel(), name);
sendData("PRIVMSG", param);
}
}
我不確定爲什麼我得到的多餘的「東西」(我不知道它是什麼是)從我的吸氣劑,因爲它是簡單地回送一個專用變量的情況下:
const char* BotDetails::getNickName() { return nickName; }
無論如何,這不是給我可以擺脫它在我的問題(儘管它可能是相當哈克)。
我的問題是,當我連接到測試通道,和我在該行指定trueNick,所以我可以看到我通過程序步驟發生了什麼斷點,會出現以下情況:
1) trueNick被分配值:0x10042d0f9 < _ZStL6ignore + 119> 「CPlusPlusBotTest」
2)切口的值指定爲: 「CPlusPlusBotTest」
3)名稱被分配的值: 「Seanharrs」 和切口被分配了價值:「Seanharrs」
這意味着當我的調試語句運行時,nick和name是相同的值。我不確定爲什麼nick被重新分配爲與name相同的值,這不應該發生。它每次都發生,不僅僅是爲了我的名字。我嘗試使用char數組和字符串,但無濟於事。我也發現奇怪的是,trueNick從不受影響,它只是這兩個變量。任何幫助都是值得讚賞的(即使它只是一種更好的方法來檢查這個問題而不是解決方法,因爲在我的最終結果中,沒有人會遇到這種情況)。
你有一些東西類似於常見的字符串流問題發生:http://stackoverflow.com/questions/1374468/stringstream-string-and-char-conversion-confusion – chris
停止使用'char *'s,並使用改爲使用'std :: string'。這將自動解決您的許多問題。 – Mat
'const char * nick = string(details.getNickName())。c_str();'不會使用'nick'是UB? –