我想創建一個鏈接列表的程序。我想創建一個case語句,用戶可以選擇 1.添加節點 2.刪除節點 3.搜索節點 4.顯示鏈表(每行5個節點) 5.退出程序創建鏈接列表 - 編譯器錯誤
但是該程序仍在進行中,我無法將其編譯到目前爲止。我試圖找出是什麼導致它不能編譯。看來我的主程序不會鏈接頭文件。任何建議,而我不能編譯或鏈接?我想在繼續工作之前弄清楚出錯的原因。另外哪裏是創建我的結構最好的地方?
從流血開發的C++list.cpp: In constructor list::list():
list.cpp:7: error: node undeclared (first use this function)
list.cpp:7: error: (Each undeclared identifier is reported only once for each function it appears in.)
list.cpp:7: error: head undeclared (first use this function)
list.cpp:8: error: precurrent undeclared (first use this function)
list.cpp:9: error: current undeclared (first use this function)
list.cpp: At global scope:
list.cpp:16: error: expected constructor, destructor, or type conversion before :: token
list.cpp:16: error: expected , or ; before :: token
list.cpp: In member function void list::insert(int):
list.cpp:24: error: head undeclared (first use this function)
list.cpp:24: error: null undeclared (first use this function)
list.cpp:24: error: stray \ in program
list.cpp:24: error: stray \ in program
list.cpp:24: error: If undeclared (first use this function)
Main.cpp的
錯誤
#include <string>
#include <iostream>
using namespace std;
#include <cstdlib>
#include "list.h"
using namespace std;
int main(int argc, char *argv[])
{
list* list1;
system("PAUSE");
return EXIT_SUCCESS;
}
Link.cpp
#include <iostream>
using namespace std;
#include "list.h"
list::list()
{
node *head = NULL;
node *precurrent = NULL;
node *current = NULL;
int *temp = 0;
int _insert = 0;
int _search = 0;
int _remove = 0;
}
List::~list()
{
while (head != 0)
remove();
}
void list::insert(int _insert)
{
if (head==null) \\If there is no list already, create a new head.
{
temp = new Node;
temp->data = _insert;
head = temp;
}
else \\otherwise, insert the new node after current
{
temp = new Node;
temp->data = _insert;
temp->next = current->next;
current->next = temp;
}
}
void list::search(int _search)
{
current=head;
while (head->next != 0) //Cycle through the list, and if the number is found, say so
{
if (current->data = _search)
cout<<"Number found."<<endl;
else
cout<<"Number not found."<<endl;
}
}
void list::remove(int _remove)
{
if (head == null)
cout <<"Error. No list."<<endl;
else if (head->next == null)
{
num = head->data;
delete head;
head=null;
current=null;
}
else if (head == current)
{
temp = head->next;
num = head->data;
delete head;
head=temp;
current=temp;
}
else
{
temp = current->next;
num = current->data;
delete current;
precurrent->next = temp;
current = temp;
}
}
ostream &operator<<(ostream& osObject, list& list){
nodeType<Type>* current = list.head;
int i = 0;
while (current != NULL) //while more data to print
{
osObject << current->info << " ";
current = current->link;
++i;
if (i % 5 == 0)
{
cout << '\n';
i = 0;
}
}
osObject << '\n'; // print the ending newline
return osObject;
}
Link.h
//CLASS PROVIDED: list
//
// CONSTRUCTOR for the list class:
// list()
// Description: Constructor will initialize variables
// Preconditions: None
// Postcondition: int insert = ""
// int search = ""
// int remove = ""
// ~list()
// Description: Destructor destroys variables
// Preconditions: None
// Postcondition: variable deleted
//
// MEMBER FUNCTIONS for the list class
//
// string insert(int)
// Description: Inserts an integer into a linked list
// Precondition: none
// Postcondition: function returns Success/Error message.
//
// string search(int);
// Description: Searches for certain linked list member and returns int to set current variable
// Precondition: none
// Postcondition: function returns int
//
// string remove(int);
// Description: removes linked list member
// Precondition: user sends int to be deleted
// Postcondition: function returned string sddress
//
// void display(void);
// Description: displays entire linked list
// Precondition: none
// Postcondition: function returns screen output
//
// void quit(void);
// Description: closes program
// Precondition: none
// Postcondition: none
//
#ifndef LIST_H
#define LIST_H
#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;
class list
{
friend ostream& operator<< (ostream &os, const list&);
public:
//CONSTRUCTOR/DESTRUCTOR---------------------
list();
~list();
//GETS---------------------------------------
void insert(int);
string search(int);
string remove(int);
void display(void);
void quit(void);
private:
int insert;
int search;
int remove;
};
#endif
什麼是'#inclide'? – Drahakar 2012-02-22 05:57:47
我們需要你有編譯錯誤。 – Drahakar 2012-02-22 05:59:18
而傳統的方法是輸入幾行代碼,編譯它們(並糾正它們直到它們編譯),然後重複。 – 2012-02-22 06:04:42