0

I have class model:

class model
{
private:
    link_list list;
    float parameter_B1;
    float parameter_B0;
public:
    model();
    float getparameter_B1() const;
    float getparameter_B0() const;
    float predict();
    void info();
};

and as you can see it's has a link_list attribute which is:

class link_list
{
private:
    node* head;
public:

    link_list();
    link_list insertNewNode(Pair _pairs);
    Pair average();
    float parameters1(Pair _average);
    float parameters2(Pair _average, float par1);
    float error(float para1,float para2);
    ~link_list();
    link_list(const link_list& p)
    {

            /*head = new node;
            head->setpair(p.head->getpair());
            head->setnextnode(p.head->getnextnode());*/

    };
};

and I have to return the object (itself) from my insertNewNode function and use copy constructor in order to add new nodes to my list.

I'm new in c++ , but I have read something about need of defining copy constructor when we have dynamic data , since default copy constructor only copies the address of the original data member.

but I have no idea how to write my copy constructor?

I tried this:

    link_list(const link_list& p)
    {

            head = new node;
            head->setpair(p.head->getpair());
            head->setnextnode(p.head->getnextnode());

    };

but it will only work for my first node, for others I will get an segmentation fault. this is my class node:

struct Pair
{
float x;
float y;
};
class node
{
private:
    Pair _pair;
    node* next;
public:
    node() {};
    void setpair(Pair p);
    Pair getpair() const;
    node* getnextnode() const;
    void setnextnode(node* temp);
};

this is model constructor, where I call my insertNewNode funciton:

model::model()
{
    char filename[300];
    cout << "enter file name(path\\filname.txt):" << endl;
    cin >> filename;
    FILE* fp;
    fp = fopen(filename, "r+");
    float x, y;
    if (fp == NULL)
    {
        cout << "Unable to open the file!" << endl;
        exit(EXIT_FAILURE);
    }
    else
    {
        while (!feof(fp))
        {
            if (fscanf(fp, "%f\t%f", &x, &y) == 2)
            {
                Pair p(x, y);
                list.insertNewNode(p);

            }
        }
    }
    Pair _average = list.average();
    parameter_B1 = list.parameters1(_average);
    parameter_B0 = list.parameters2(_average, parameter_B1);
}

and this my insertNewNode

link_list link_list::insertNewNode(Pair _pairs)
{
    node* temp;
    temp = new node;
    temp->getpair().set_counter();
    /*temp->setpair(_pairs);
    temp->setnextnode(head);
    head = temp;*/
    temp->setpair(_pairs);
    temp->setnextnode(NULL);
    if (head == NULL)
    {
        head = temp;
        return *this;
    }
    node* tmp = head;
    while (tmp->getnextnode() != NULL)
        tmp = tmp->getnextnode();
    tmp->setnextnode(temp);
    return *this;
}

and finally this is my destructor:

link_list::~link_list()
{
    node* current = head;
    node* next;

    while (current != NULL)
    {
        next = current->getnextnode();
        delete(current);
        current = next;
    }
    head = NULL;
}

PS: Pair is a class , and if included it too , my question would become very long. and I only want to know about idea of such copy constructor ,assuming data is anything and there is linked list class which is used as attribute of another class.

Thanks in advance for your help, and sorry for long question.

hanie
  • 1,809
  • 2
  • 6
  • 18
  • Is `insertNewNode` really supposed to both modify the existing list *and* return a copy of it with the new node added? One or the other is common, but doing both is pretty rare. – molbdnilo Apr 19 '20 at 11:03
  • @molbdnilo I'm pretty new in c++ , how could I return a copy of list with a new node added in another function?and how could I write my copy constructor? – hanie Apr 19 '20 at 11:11

0 Answers0