在我的研究中,我要求將結構隊列實現爲雙向鏈接列表,其中允許從兩端刪除元素的方法。 我也被要求做支持push和pop的方法的Queue(例如stack是派生類)繼承的s層級堆棧,以便推送& pop將使用父級方法從一個端點插入\取出。C++中的繼承和模板
我也創建了一個節點類來生成鏈表。另外,我不允許改變「主」。 我被要求實施兩次(兩個獨立程序,兩個獨立的主要功能):一個用於雙重類型和一個通用類型(模板)。 「雙」程序工作正常,但通用程序遇到了很多錯誤,我真的不知道它是什麼意思,全局命名空間。此外,我檢查了像分號/營房等缺少的語法,它似乎很好。
我附上兩個程序和一般程序的錯誤,在帖子的末尾:
「雙節」程序(正常工作):
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
class dNode
{
private:
dNode* prev_value;
dNode* next_value;
double data_value;
friend class ddutor;
friend class dstack;
public:
//default constructor
dNode(double val);
~dNode();
};
dNode::dNode(double val)
{
next_value=NULL;
prev_value=NULL;
data_value=val;
}
dNode::~dNode()
{
data_value=-1;
}
/*************************************************************************************************************************************/
class ddutor
{
protected:
dNode* current;
dNode* head;
dNode* tail;
int size;
public:
//default constructor
ddutor(double val);
//destructor
~ddutor();
//members functions
void add_first(double data);
void add_last(double data);
double remove_first();
void remove_last();
};
ddutor::ddutor(double val)
{
head=new dNode(val);
tail = new dNode(-1);
tail=head;
head->next_value=tail;
head->prev_value=NULL;
size=1;
}
void ddutor::add_first(double data)
{
current = new dNode(data);
head->prev_value = current;
current->next_value = head;
head = current;
head->prev_value= NULL;
size++;
}
void ddutor::add_last(double data)
{
current = new dNode(data);
tail->next_value = current;
current->prev_value= tail;
tail = current;
tail->next_value = NULL;
size++;
}
double ddutor::remove_first()
{
double temp=head->data_value;
if (head->next_value == NULL)
head = NULL;
else
head = head->next_value;
size--;
return temp;
}
void ddutor::remove_last()
{
if (tail->prev_value == NULL)
tail = NULL;
else
tail = tail->prev_value;
size--;
}
ddutor::~ddutor()
{
delete current;
delete head;
delete tail;
size=-1;
}
/******************************************************************************************************************************************/
class dstack:public ddutor
{
public:
void push(double d);
double pop();
dstack(double d);
};
dstack::dstack(double d):ddutor(d){};
void dstack::push(double d)
{
add_last(d);
}
double dstack::pop()
{
double temp=tail->data_value;
remove_last();
return temp;
}
/*******************************************************************************************************************************/
int main()
{
int i;
ddutor dd(1.1);
dstack ds(11.1);
for(i=2; i < 14; i += 2)
{
dd.add_first((double)i*1.1);
dd.add_last((double)((i+1)*1.1));
} //for
cout<<"dd print:\n";
for(i=1; i < 14; i++)
cout << " " << dd.remove_first();
cout << endl;
for(i=2; i < 14; i ++)
ds.push((double)i*11.1);
cout << "ds print:\n";
for(i=1; i < 14; i++)
cout << " " << ds.pop();
cout << endl;
cin>>i;
} // main
「通用程序(許多錯誤的)通用的
#include "stdafx.h"
#include <iostream>
using namespace std;
template <class ELEMENT_TYPE>
class gNode
{
private:
gNode* prev_value;
gNode* next_value;
ELEMENT_TYPE data_value;
friend class tdutor;
friend class tstack;
public:
//default constructor
gNode(ELEMENT_TYPE val);
~gNode();
};
template <class ELEMENT_TYPE>
gNode<ELEMENT_TYPE>::gNode(ELEMENT_TYPE val)
{
next_value=NULL;
prev_value=NULL;
data_value=val;
}
template <class ELEMENT_TYPE>
gNode<ELEMENT_TYPE>::~gNode()
{
data_value=-1;
}
//***************************************************************************************************************************//
template <class ELEMENT_TYPE>
class tdutor
{
protected:
gNode* current;
gNode* head;
gNode* tail;
int size;
public:
//default constructor
tdutor(ELEMENT_TYPE val);
//destructor
~tdutor();
//members functions
void add_first(ELEMENT_TYPE data);
void add_last(ELEMENT_TYPE data);
ELEMENT_TYPE remove_first();
void remove_last();
};
template <class ELEMENT_TYPE>
tdutor<ELEMENT_TYPE>::tdutor(ELEMENT_TYPE val)
{
head=new gNode(val);
tail = new gNode(-1);
tail=head;
head->next_value=tail;
head->prev_value=NULL;
size=1;
}
template <class ELEMENT_TYPE>
void tdutor<ELEMENT_TYPE>::add_first(ELEMENT_TYPE data)
{
current = new gNode(data);
head->prev_value = current;
current->next_value = head;
head = current;
head->prev_value= NULL;
size++;
}
template <class ELEMENT_TYPE>
void tdutor<ELEMENT_TYPE>::add_last(ELEMENT_TYPE data)
{
current = new gNode(data);
tail->next_value = current;
current->prev_value= tail;
tail = current;
tail->next_value = NULL;
size++;
}
template <class ELEMENT_TYPE>
ELEMENT_TYPE tdutor<ELEMENT_TYPE>::remove_first()
{
ELEMENT_TYPE temp=head->data_value;
if (head->next_value == NULL)
head = NULL;
else
head = head->next_value;
size--;
return temp;
}
template <class ELEMENT_TYPE>
void tdutor<ELEMENT_TYPE>::remove_last()
{
if (tail->prev_value == NULL)
tail = NULL;
else
tail = tail->prev_value;
size--;
}
template <class ELEMENT_TYPE>
tdutor<ELEMENT_TYPE>::~tdutor()
{
delete current;
delete head;
delete tail;
size=-1;
}
/*******************************************************************************************************************************/
template <class ELEMENT_TYPE>
class tstack:public tdutor
{
public:
void push(ELEMENT_TYPE d);
ELEMENT_TYPE pop();
tstack(ELEMENT_TYPE d);
};
template <class ELEMENT_TYPE>
tstack<ELEMENT_TYPE>::tstack(ELEMENT_TYPE d):tdutor(d){};
template <class ELEMENT_TYPE>
void tstack<ELEMENT_TYPE>::push(ELEMENT_TYPE d)
{
add_last(d);
}
template <class ELEMENT_TYPE>
ELEMENT_TYPE tstack<ELEMENT_TYPE>::pop()
{
ELEMENT_TYPE temp=tail->data_value;
remove_last();
return temp;
}
int main()
{
int i;
tdutor<double> dd(1.1);
tstack<double> ds(11.1);
for(i=2; i < 14; i += 2)
{
dd.add_first((double)i*1.1);
dd.add_last((double)((i+1)*1.1));
} //for
cout << "dd print:\n";
for(i=1; i < 14; i++)
cout << " " << dd.remove_first();
cout << endl;
for(i=2; i < 14; i ++)
ds.push((double)i*11.1);
cout << "ds print:\n";
for(i=1; i < 14; i++)
cout << " " << ds.pop();
cout << endl;
cin>>i;
} // main
錯誤:
錯誤23錯誤C 1903年:無法從先前的錯誤中恢復;正在停止編譯
錯誤5錯誤C2039:「add_first」:不是「`全局命名空間」「
錯誤12錯誤C2039成員:‘add_last’:不是‘`全局命名空間’」成員
錯誤13錯誤C2039: 'remove_first':不是 '`全局命名空間' '
錯誤21錯誤C2039的成員: 'remove_last':不是 '`全局命名空間''
一個構件錯誤4錯誤C2059:語法錯誤:'< '
錯誤11錯誤C2059:語法錯誤:' < '
錯誤20錯誤C2059:語法錯誤:' <'
錯誤18錯誤C2086: '詮釋tdutor':重新定義
錯誤6錯誤C2143:語法錯誤:缺少';'之前'{'
錯誤14錯誤C2143:語法錯誤:缺少';'之前'{'
錯誤8錯誤C2143:語法錯誤:缺少';' '<'
錯誤16錯誤C2143:語法錯誤:缺少';'前 '<'
錯誤9錯誤C2182: 'tdutor':非法使用類型的 '無效'
錯誤17錯誤C2182: 'tdutor':非法使用類型錯誤7的 '無效'
錯誤C2447:'{':缺少函數標題(舊式正式列表?)
錯誤15錯誤C2447:'{':缺少函數標題(舊式正式列表?)
錯誤22錯誤C2588: '::〜tdutor':非法全球析構函數
錯誤3錯誤C2988:無法識別的模板聲明/定義
錯誤10錯誤C2988:無法識別的模板聲明/定義
錯誤19錯誤C2988:無法識別的模板聲明/定義
錯誤1錯誤C2989:'tdutor':類模板已被聲明爲非類模板
錯誤2錯誤C3857:「tdutor」:多個模板參數列表不允許
您應該指定'很多錯誤',以便每個人都知道問題出在哪裏。特別是第一個編譯器錯誤是最有幫助的。 – Till
Visual Studio中的「錯誤」窗口包含不完整的信息,尤其是涉及模板時。請複製輸出窗口中的錯誤,包括他們周圍的任何「註釋」。那些列出了編譯器試圖生成的專業化,沒有它們的錯誤可能沒有意義。 –
除了你粘貼的列表全部混在一起。按照編譯器生成的順序複製錯誤。 –