0

So, i am trying to do a program to generalize adding,deleting, showing a double linked list. but i have encountered a problem at the addition part. When compiling it i encounter " undefined reference to insertNodeBeggining(List*, void*)". What is the problem?

main.cpp

#include <iostream>
#include <cstring>
#include "methods.h"

using namespace std;

int main()
{
    List *head=createList();

    void *p=NULL;
    insertNodeBeggining(head,p);

    return 0;
}

methods.cpp

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <cstring>
#include <math.h>

using namespace std;

typedef struct nodeType{
    nodeType *next;
    nodeType *prev;
    void* data;
} NodeT;

typedef struct Lists{
    NodeT *first;
    NodeT *last;
} List;

List *createList()
{
    return (List*)malloc(sizeof(List));
}

void insertNodeBeggining(List *head, void *value)
{
    NodeT *nou=(NodeT*)malloc(sizeof(NodeT));

    nou->data=value;

    if(head->first==NULL)
    {
        head->first=nou;
        head->last=nou;
        nou->next=NULL;
        nou->prev=NULL;
    }
    else
    {
        nou->next=head->first;
        head->first->prev=nou;
        nou->prev=NULL;
        head->first=nou;
    }
}

methods.h

#ifndef METHODS_H_INCLUDED
#define METHODS_H_INCLUDED

typedef struct  NodeT;
typedef struct List;

List *createList();
void insertNodeBeggining(List *head, void *value);

#endif // METHODS_H_INCLUDED
honk
  • 9,137
  • 11
  • 75
  • 83
gliga bogdan
  • 143
  • 1
  • 5
  • 3
    I don't see anything that is really C++, it's all C code. – Some programmer dude Mar 17 '14 at 20:27
  • There are so many problems with your code that it's hard to list all of them. To start with, you have to remember that when you allocate memory with `malloc`, the allocated memory will *not* be initialized in any way, so using the data in it leads to *undefined behavior*, which is the most common cause of application crashes. – Some programmer dude Mar 17 '14 at 20:32

2 Answers2

0

It seems that the problem is that the compiler considers two names List, one in the header and other in methods.cpp, as two different names.

The typedef in the header is invalid though the compiler may not issue an error

typedef struct List;

You should exclude these definitions

typedef struct nodeType{
             nodeType *next;
             nodeType *prev;
             void* data;
           } NodeT;

typedef struct Lists{
               NodeT *first;
               NodeT *last;
             }List;

from metods.cpp and include them in the header instead of your typedef(s).

methods.cpp has to contain this new header included.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • I think yo'ure almost right. Looking at it, the problem appears to be that in the definitions `List` is not a real `struct` name but is defined via a `typedef`. In order to be hooked up with the incomplete type declared earlier/elsewhere, it needs to be `struct List { ... }`. – Cheers and hth. - Alf Mar 17 '14 at 20:42
  • The advice about moving is still good. ;-) Unless the OP did this for *information hiding*. – Cheers and hth. - Alf Mar 17 '14 at 20:43
0

Basically, an incomplete type can't be completed via a typedef.

Hence your typedef-ed List in the implementation file is not considered to be a definition of the earlied forward-declared incomplete List.

To fix that, replace the C style

typedef struct Lists{
    NodeT *first;
    NodeT *last;
} List;

with C++ style

struct List
{
    NodeT*  first;
    NodeT*  last;
};

Disclaimer: I haven't run this through a compiler.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331