C++ 打印通过复制程序创建的e2
我创建了一个名为e的链表。我使用复制构造函数来创建e2,但由于某些原因,当我试图打印e2时,程序总是崩溃。有人能给我解释一下,帮我解决这个问题吗?谢谢C++ 打印通过复制程序创建的e2,c++,debugging,dynamic,linked-list,C++,Debugging,Dynamic,Linked List,我创建了一个名为e的链表。我使用复制构造函数来创建e2,但由于某些原因,当我试图打印e2时,程序总是崩溃。有人能给我解释一下,帮我解决这个问题吗?谢谢 #include <iostream> #include <ctime> #include <cmath> using namespace std; class Element { public: Element();//constructor Element(const Element&
#include <iostream>
#include <ctime>
#include <cmath>
using namespace std;
class Element
{
public: Element();//constructor
Element(const Element&); //copy constructor
//Element& Element::operator =(const Element & from);
void Addelement(int row, int col, int value);
void swap(int num,int x, int arr[100]);
void printelement();
void rowordermajor();
private:
typedef Element* ElementPtr;
int row;
int col;
int value;
ElementPtr next;
ElementPtr head;
bool comparegreater(ElementPtr temp1, ElementPtr temp2);
};
int main()
{
Element e;
for (int i=0;i<5;i++)
{
e.Addelement(i,i,i);
}
e.printelement();
Element e2(e);
//e2.printelement();
system("PAUSE");
}
Element::Element()//normal constructor
{
head=NULL;
}
Element::Element(const Element& e)
{
this->row=e.row;
this->col=e.col;
this->value=e.value;
this->next=e.next;
}
void Element::Addelement(int row, int col, int value )
{
ElementPtr temp= new Element;
temp->row=row;
temp->col=col;
temp->value=value;
temp->next=head;
head=temp;
}
void Element::printelement()//why does it print backwards
{
ElementPtr temp=head;
while (temp != NULL)
{
cout<<"( "
<<temp->row
<<" , "
<<temp->col
<<" , "
<<temp->value
<<" ) ";
cout<<endl;
temp=temp->next;
}
}
#包括
#包括
#包括
使用名称空间std;
类元素
{
public:Element();//构造函数
元素(常量元素&);//复制构造函数
//元素和元素::运算符=(常量元素和起始元素);
无效加法元素(整数行、整数列、整数值);
无效交换(整数、整数x、整数arr[100]);
void printelement();
void rowordermajor();
私人:
typedef元素*ElementPtr;
int行;
int col;
int值;
ElementPtr-next;
元件PTR头;
布尔比较器(ElementPtr temp1、ElementPtr temp2);
};
int main()
{
元素e;
对于(inti=0;irow=e.row;
这->col=e.col;
此->值=e.value;
这->下一步=下一步;
}
void元素::Addelement(int行、int列、int值)
{
ElementPtr temp=新元件;
临时->行=行;
温度->颜色=颜色;
温度->值=值;
温度->下一步=头部;
压头=温度;
}
void Element::printelement()//为什么它会向后打印
{
元件PTR温度=压头;
while(temp!=NULL)
{
cout因为当您使用带有元素&e
作为参数的构造函数时,您没有将NULL
(或者可能this->head=e.head;
)赋值给head