-2

So i have my linked list class, and my node struct. Here is the code:

struct node{
    int data;
    node *next;
};

class linkedlist{
    private:
    node *head, *tail;
    public:
    linkedlist()
    {
      head=NULL;
      tail=NULL;
    }

  };

When i call a function which uses head and tail to, for example, add a value, the compiler says that node and tail was not declared in this scope.

EDIT: Here is a function in my main file in which head and tail isnt declared

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include "func.h"
using namespace std;        
int errorCode;
void add(tADT &adt, tValue value, int pos){
    node *pre=new node;
    node *cur=new node;
    node *temp=new node;
    cur=head;
    for(int i=1;i<pos;i++)
    {
      pre=cur;
      cur=cur->next;
    }
    temp->data=value;
    pre->next=temp; 
    temp->next=cur; 
}

And my function call:

cin >> val >> pos;
add(adt, val, pos);
Poats
  • 9
  • 2
  • I have changed it, but doesnt work – Poats Jun 19 '18 at 12:34
  • 2
    then the usage might be wrong. Please provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Joseph D. Jun 19 '18 at 12:35
  • There should be as many uses of `new` as the number of nodes you're adding to the list (that is, one (1)). – molbdnilo Jun 19 '18 at 12:51
  • If i manually add "node *head, *tail;" to every function it works. – Poats Jun 19 '18 at 12:53
  • What is `tADT`? Don't you think you should be using `adt` for something? – molbdnilo Jun 19 '18 at 12:54
  • 2
    There are so many problems in your code. Please, read some good book about C++ first. And do not reinvent the wheel (implement the linked list, which has been implemented zillion times in all possible flavours). – Daniel Langr Jun 19 '18 at 12:54
  • 3
    `head` and `tail` are private members of `linkedlist`. You can only access them in member functions of `linkedlist`. A list of good books is [here](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Jun 19 '18 at 12:55
  • 3
    @Daniel Langr Creating your own implementations of containers can be a great way to learn about the many facets of the language. For example, if the OP were able to create a generic linked list interface with both a singly and doubly linked list class that inherited from it that followed the rule of five, they could learn a great deal about templates, inheritance, and move semantics all in one project. – Mike Borkland Jun 19 '18 at 12:59
  • 2
    @MikeBorkland Totally agree, but according to the code, OP hasn't learnt even the very basics of C++ such as scope of variables, initialization, dynamic memory usage, etc. But you're right that my comment was too strict :) – Daniel Langr Jun 19 '18 at 13:03
  • Please provide [MCVE]. I note that your function "add" is apparently not part of "linkedlist" as the snippet implementing "add" does not mention the class name. Unless the code for the definition of "add" is inside your class header, you need to use "linkedlist::add(...)", but this contradicts the class header shown in your posting (which does not have "add"). – 2785528 Jun 19 '18 at 13:41

2 Answers2

1

Your class is named linkedlist, but your constructor list(). It should be named linkedlist() instead, since constructor and class name should match.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • 1
    It should work @Poats, as you can see in the [Live Demo](https://wandbox.org/permlink/QNAgEy9woah1K4fB). – gsamaras Jun 19 '18 at 12:35
0

Your class is named linkedlist,

a) but your class declaration does not mention "add", and

b) your function "add" does not mention the class membership of "linkedlist".

The class declaration should declare all member functions, including "add", and the function definition needs to include the class membership of "linkedlist".

For MCVE your goal is to minimize (both declaration and definition), so perhaps your first goal should be to get "linkedlist::add()" to compile. And add minimal additional functionality (i.e. one at a time) until you get the hang of it.

2785528
  • 5,438
  • 2
  • 18
  • 20