[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 231
  • Last Modified:

Problem with defining a classfunction for Linked list

I'm making a object-oriented linked list in c++, and I'm having problems with asigning new value to my head pointer:

#include "Lista.h"
#include <cstddef>
#include <cassert>

LankadLista::LankadLista(): size(0), head(NULL){}

     //while (!KollaLista())

void LankadLista::LaggTill(int index, dataTyp nyNod)
     int nyLangd = ReturElement() + 1;

     Nod *nyPek = new Nod;  
     size = nyLangd;

     nyPek->data = nyNod;

     if (index == 1)
          nyPek->next = head;
          head = nyPek;
          Nod *prev = SokElement(index-1);
          nyPek->next = prev->next;
          prev->next = nyPek;
/*void Tabort(){
void Sortera(){
void skrivUt(){
int LankadLista::SokElement(int index)
     /*if ((index < 1) || (index > ReturElement()))
          return NULL;
          Nod *cur = head;
          for (int skip = 1; skip < index; ++skip)
               cur = cur->next;
          return cur;
bool LankadLista::KollaLista() const
     return bool(size==0);
int LankadLista::ReturElement()
     return size;

The problem is in the Laggtill(insert) function, head = nyPek. "cannot convert 'LankadLista::Nod*' to 'Nod*' in assignment"
Here is the classfile:

#include <iostream>
using namespace std;

typedef int dataTyp;
typedef struct Nod *pek;

class LankadLista {


     void LaggTill(int index, dataTyp NyNod);
     //void Tabort();
     //void Sortera();
     //void skrivUt();
     int SokElement(int index);
     bool KollaLista() const;
     int ReturElement();


     struct Nod {
          dataTyp data;
          pek next;

     int size;
     Nod *head;


I hope someone can help me, thanks.
  • 2
  • 2
1 Solution
Why are you making object-oriented linked list in c++?
C++ already has a built-in link list called std::list.

It looks like you're trying to reinvent the wheel.

Is there a paticular reason why you can't use std::list?
>>The problem is in the Laggtill(insert) function, head =
>>nyPek. "cannot convert 'LankadLista::Nod*' to 'Nod*' in

Try moving your Nod struct OUT-SIDE of your LankadLista class.

If you want to keep the Nod struct inside your class, then you should move your "typedef struct Nod*pek;" inside your class as well.
In either case, they should be located in the same scope.

If you keep them both inside your class, then your external class will need to access it via LankadLista::Nod
And you're going to have to put them in a public area, or make the using class a friend.
beelsepubAuthor Commented:
Why I'm making a linked list object-oriented is because it's a part of my project in my c++ course.
I guess we're only doing it because we need to learn to think object-oriented for future courses.

Axter, I'll give it a try, thanks.
beelsepubAuthor Commented:
I solved it by deleting the typedef struct completely and write Nod *next; in the struct instead. But I'll accept your comment as answer since I didnt understand that typedef struct was the problem.

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now