2012-03-11 74 views
0

我正在編寫一個程序,它可以讀取輸入文件,並將讀取的數據存儲在由「鏈接列表」鏈接的節點中。但是,我得到了一些錯誤:用Structs創建鏈表 - C++

  1. 在構造函數中List::List(),在*((List*)this)->List::list[0] = 0
  2. 敵不過 '運算符=' 在構造函數中Polynomial::Polynomial():敵不過 '運算符=' 在*((Polynomial*)this)->Polynomial::poly = (operator new(400u), (<statement>), ...)

我有一種感覺,我在做什麼:我嘗試通過數組訪問某個節點是我出錯的地方,但是我無法弄清楚。

下面是代碼:

#include <iostream> 
#include <fstream> 

using namespace std; 

enum result{success, failure};       

struct Node 
{ 

double coefficient;       
int power;        

Node();     
Node(double coef, int pwr);  
}; 

struct List 
{ 
Node *list[100]; 

//Default constructor 
List(); 
}; 

Node::Node() 
{ 
coefficient = 0; 
power = 0; 
} 

List::List() 
{ 
*list[0] = NULL; 
} 

Node::Node(double coef, int pwr) 
{ 
coefficient = coef; 
power = pwr; 
} 


    class Polynomial 
    { 
    public: 
    Polynomial();      
    result multiply(Polynomial &p, Polynomial &q); 
    result add(Polynomial p, Polynomial &q); 
    void initialize(ifstream &file); 
    void simplify(Polynomial &var); 
    void print_poly(); 
    ~Polynomial(); 

private: 
    List *poly;       //Store the pointer links in an array 
    Node first_node; 
    int val; 
}; 

Polynomial::Polynomial() 
{ 
*poly = new List(); 
} 

Polynomial::void initialize(ifstream &file) 
{ 
int y[20]; 
double x[20]; 
int i = 0, j = 0; 

//Read from the file 
file >> x[j]; 
file >> y[j]; 

first_node(x[j], y[j++]);      //Create the first node with coef, and pwr 
*poly->list[i] = &first_node;      //Link to the fist node 

//Creat a linked list 
while(y[j] != 0) 
{ 
    file >> x[j]; 
    file >> y[j]; 
    *poly->list[++i] = new Node(x[j], y[j++]); 
} 

val = i+1;        //Keeps track of the number of nodes 
} 


Polynomail::result multiply(Polynomial &p, Polynomial &q) 
{ 
int i, j, k = 0; 

for(i = 0; i < p.val; i++) 
{ 
    for(j = 0; j < q.val; j++) 
    { 
     *poly->list[k] = new Node(0, 0); 
     *poly->list[k].coefficient = (p.poly->list[i].coefficient)*(q.poly->list[j].coefficient); 
     *poly->list[k++].power = (p.poly->list[i].power)+(q.poly->list[j].power); 
    } 
} 

val = k+1;        //Store the nunber of nodes 
return success; 
} 

Polynomial::void simplify(Polynomial &var) 
{ 
int i, j, k = 0; 

//Create a copy of the polynomial 
for(j = 0; j < var.val; j++) 
{ 
    *poly->list[j] = new Node(0, 0); 
    *poly->list[j].coefficient = var.poly->list[j].coefficient; 
    *poly->list[j].power = var.poly->list[j].power; 
} 

//Iterate through the nodes to find entries which have the same power and add them, otherwise do nothing  
for(k = 0; k < var.val; k++) 
{ 
    for(i = k; i < var.val;) 
    { 
     if(*poly->list[k].power == var.poly->list[++i].power) 
     { 
      if(*poly->list.power[0] == 0) 
      { 
       NULL; 
      } 
      else 
      { 
       *poly->list[k].coefficient = *poly->list[k].coefficient + var.poly->list[i].ceofficient; 
       var.poly->list[i] = Node(0, 0); 
      } 
     } 
    } 
} 
} 

Polynomial::void print_pol() 
{ 
int i = 0; 
for(i = 0; i < temp.val; i++) 
{ 
    cout << "Coefficient: " << temp.poly->list[i].coefficient << ", and " << "Power: " << temp.poly->list[i].power << endl; 
} 
} 

回答

0

的問題是錯誤的取消引用。 34號線大概應該是

list[0] = NULL; // remove the * 

您嘗試將NULL值賦給類型節點的變量,但你可能指的是指針到節點。 而在同爲線63

此外,第66行要高度重視可能b真:

void Polynomial::initialize(ifstream &file) // start with return type