【问题标题】:Creating a Linked list with Structs - C++使用结构创建链接列表 - C++
【发布时间】:2023-04-03 22:13:01
【问题描述】:

我正在编写一个程序,它可以读取输入文件并将读取的数据存储在由“链接列表”链接的节点中。但是,我遇到了一些错误:

  1. 在构造函数List::List() 中,与*((List*)this)->List::list[0] = 0 中的'operator =' 不匹配
  2. 在构造函数Polynomial::Polynomial():与*((Polynomial*)this)->Polynomial::poly = (operator new(400u), (<statement>), ...) 中的'operator =' 不匹配

我有一种感觉:我尝试通过数组访问某个节点是我出错的地方,但是我想不通。

代码如下:

#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;
}
}

【问题讨论】:

    标签: class pointers struct linked-list


    【解决方案1】:

    问题是错误的取消引用。第 34 行应该是

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

    您尝试将值 NULL 分配给 Node 类型的变量,但您可能是指一个指向 Node 的 指针。 第 63 行也是如此。

    另外,第 66 行可能是 b:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-02
      • 2017-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-29
      相关资源
      最近更新 更多