1

I had this code in a simple file yesterday and it worked perfectly. But now I need to separate the header and the source file. And I did but code gives an error and I don't know why.

This is my header

#ifndef LISTE_HPP
#define LISTE_HPP

#include <malloc.h>
#include <stdio.h>

class Liste {
public:
  struct node {
    int veri;
    struct node *sonraki;
    struct node *onceki;
  };

  struct node *baslangic = NULL;
  int maxobeb = 0;

  struct node *dugumOlustur(int);
  void yazdir();
  void ListeyeEkle(int);
  int ModAl(int, int);
  int ObebHesapla(int, int);
};
#endif

and this is the source file

#include "Liste.hpp"


 node* Liste::dugumOlustur(int veri) //here gives error code doestn recognize node please help me 
{
    struct node* yeniDugum = (struct node*)malloc(sizeof(struct node));
    yeniDugum->veri = veri;
    yeniDugum->sonraki = NULL;
    yeniDugum->onceki = NULL;

    return yeniDugum;
}

My source file includes other fuctions too but the error is caused in that line so I didn't included all the source.

2
  • 1
    It should be Liste::node* instead of node* on that line. Commented Aug 15, 2020 at 14:00
  • Thanks dude ı missed that Commented Aug 15, 2020 at 14:09

1 Answer 1

1

There are a couple of things that you could fix and improve in your code:

class Liste {
public:
  // Have a consistent naming scheme.
  // If Liste is capitalized, why isn't Node?
  struct Node {
    int veri;
    // You don't have to write struct Node, just Node is enough.
    Node *sonraki;
    Node *onceki;
  };

  // Don't use NULL, use nullptr in C++
  Node *baslangic = nullptr;
  int maxobeb = 0;

  Node *dugumOlustur(int);
  void yazdir();
  void ListeyeEkle(int);
  int ModAl(int, int);
  int ObebHesapla(int, int);
};

// It should be Liste::Node, not just node
Liste::Node* Liste::dugumOlustur(int veri)
{
    // Don't use malloc, use new.
    // Remember to delete using the delete keyword, not free().
    // Also you can use aggregate initialization to turn this into a single line.
    return new Node{veri, nullptr, nullptr};
}

Besides all that, I would strongly advise against managing memory manually if you can. Use std::unique_ptr if possible. Then you don't need to manually delete any memory.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. Your advice helped me a lot 🙏

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.