我試圖嘗試從方法中使用字符串輸入,並將其設置爲結構的變量,然後將其放置在鏈接列表中。我沒有包括所有的代碼,但我發佈了構造函數和所有好東西。現在代碼是打破在線路C++:字符串,結構和訪問衝突寫入位置
node->title = newTitle;
node->isbn = newISBN;
所以newTitle的是,我正在嘗試設置到變量節點的書結構的標題變量的方法的字符串輸入。現在,我假設這與指針問題有關,並嘗試將數據設置給它們,但我無法找出修復/替代方案。 另外,我嘗試使用
strcpy(node->title, newTitle)
但是,有一個問題與字符串轉換成字符的列表,因爲strcpy的只使用字符的列表。也嘗試了一些其他的東西,但沒有人似乎泛,幫助解釋將不勝感激。
struct Book
{
string title;
string isbn;
struct Book * next;
};
//class LinkedList will contains a linked list of books
class LinkedList
{
private:
Book * head;
public:
LinkedList();
~LinkedList();
bool addElement(string title, string isbn);
bool removeElement(string isbn);
void printList();
};
//Constructor
//It sets head to be NULL to create an empty linked list
LinkedList::LinkedList()
{
head = NULL;
}
//Description: Adds an element to the link in alphabetical order, unless book with
same title then discards
// Returns true if added, false otherwise
bool LinkedList::addElement(string newTitle, string newISBN)
{
struct Book *temp;
struct Book *lastEntry = NULL;
temp = head;
if (temp==NULL) //If the list is empty, sets data to first entry
{
struct Book *node;
node = (Book*) malloc(sizeof(Book));
node->title = newTitle;
node->isbn = newISBN;
head = node;
}
while (temp!=NULL)
{
... //Rest of Code
你從哪裏學習?它看起來像是「C with strings」,而不是C++。例如,你不需要用一個struct關鍵字作爲結構的指針聲明,它只是'Book * temp;'(最好是初始化)。 ''new''上面''malloc''''''''''''' – Coda17 2014-08-29 19:39:03
對於C和C++,我沒有那麼棒的老師(他從字面上告訴我們,他放在板子上的代碼不會在編譯器上正確運行),而且我不知道許多「正確的」或更新的C++聲明 – 2014-08-29 19:56:58