[問題] string & char 沒有可接受的轉換?

看板C_and_CPP (C/C++)作者 (K.L)時間15年前 (2011/05/02 15:45), 編輯推噓0(0013)
留言13則, 3人參與, 最新討論串1/1
程式碼(Code):(請善用置底文網頁, 記得排版) #include <iostream> using namespace std; char store[10]; struct a{ string iWanttostoreit; }; typedef struct a TreeNode; typedef TreeNode *TreeNodePtr; int main() { TreeNodePtr treePtr = new TreeNode; cin >> store; treePtr->iWanttostoreit = store; cout << treePtr->iWanttostoreit; } Q2.(已解決) 他寫說找不到適當的轉換 可是他不是字串嗎? 為什麼不能直接輸出呢? james732:nclude <string> Q1.(已解決) 想請問一下,字串沒有辦法直接用上述方式存進去嗎? 他會顯示存入錯誤@ @ 謝謝各位! ->此題解法請參考 james732 http://pastie.org/1855925 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.116.246.179

05/02 15:46, , 1F
TreeNodePtr treePtr
05/02 15:46, 1F

05/02 16:08, , 2F
用C++的string,最好不要用malloc,要嘛就用new吧
05/02 16:08, 2F

05/02 16:18, , 3F
忘記要怎麼用new建立新結構了@ @ 可以提示一下嗎?
05/02 16:18, 3F

05/02 16:23, , 4F

05/02 16:28, , 5F
謝謝樓上 原來直接接上去就好了
05/02 16:28, 5F

05/02 16:56, , 6F
其實 C++ 不需要把 typedef struct 名稱...
05/02 16:56, 6F

05/02 17:08, , 7F
cout << treePtr->iWanttostoreit; 他寫說不能轉換?
05/02 17:08, 7F
※ 編輯: kimgtob 來自: 140.116.191.88 (05/02 17:38)

05/02 17:42, , 8F
試試看 #include <string>
05/02 17:42, 8F

05/02 17:44, , 9F
請問一下#include<string.h>->有這個東西嗎?
05/02 17:44, 9F
※ 編輯: kimgtob 來自: 140.116.191.88 (05/02 17:46)

05/02 17:47, , 10F
有,但是 string 跟 string.h 內容是不一樣的
05/02 17:47, 10F

05/02 17:48, , 11F
string 是 C++ 「string」這個類別的相關定義
05/02 17:48, 11F

05/02 17:48, , 12F
哦哦,了解 謝謝樓上神手!!
05/02 17:48, 12F

05/02 17:48, , 13F
string.h 是 C語言 strlen, strcmp, strcpy 這些函式
05/02 17:48, 13F
文章代碼(AID): #1Dlc3-O5 (C_and_CPP)
文章代碼(AID): #1Dlc3-O5 (C_and_CPP)