?
Solved

Problem with defining a classfunction for Linked list

Posted on 2003-03-29
4
Medium Priority
?
222 Views
Last Modified: 2013-12-14
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){}

LankadLista::~LankadLista(){
     //while (!KollaLista())
          //remove(1);
}

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;
     }
     else
     {
          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;
     else
     {
          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 {

public:
     LankadLista();
     ~LankadLista();

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

private:

     struct Nod {
          dataTyp data;
          pek next;
     };

     int size;
     Nod *head;

};

I hope someone can help me, thanks.
//Olle
0
Comment
Question by:beelsepub
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 8230348
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?
0
 
LVL 30

Accepted Solution

by:
Axter earned 150 total points
ID: 8230367
>>The problem is in the Laggtill(insert) function, head =
>>nyPek. "cannot convert 'LankadLista::Nod*' to 'Nod*' in
>>assignment"

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.
0
 

Author Comment

by:beelsepub
ID: 8230937
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.
0
 

Author Comment

by:beelsepub
ID: 8231161
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.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question