0

so I'm getting this error

Ld /Users/robertrenecker/Library/Developer/Xcode/DerivedData/linkedlist-cmxkapfibtnbikengkflkhhlrwta/Build/Products/Debug/linkedlist normal x86_64
    cd "/Users/robertrenecker/Desktop/C++ Projects/Beginnings/test/linkedlist"
    export MACOSX_DEPLOYMENT_TARGET=10.10
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk -L/Users/robertrenecker/Library/Developer/Xcode/DerivedData/linkedlist-cmxkapfibtnbikengkflkhhlrwta/Build/Products/Debug -F/Users/robertrenecker/Library/Developer/Xcode/DerivedData/linkedlist-cmxkapfibtnbikengkflkhhlrwta/Build/Products/Debug -filelist /Users/robertrenecker/Library/Developer/Xcode/DerivedData/linkedlist-cmxkapfibtnbikengkflkhhlrwta/Build/Intermediates/linkedlist.build/Debug/linkedlist.build/Objects-normal/x86_64/linkedlist.LinkFileList -mmacosx-version-min=10.10 -stdlib=libc++ -Xlinker -dependency_info -Xlinker /Users/robertrenecker/Library/Developer/Xcode/DerivedData/linkedlist-cmxkapfibtnbikengkflkhhlrwta/Build/Intermediates/linkedlist.build/Debug/linkedlist.build/Objects-normal/x86_64/linkedlist_dependency_info.dat -o /Users/robertrenecker/Library/Developer/Xcode/DerivedData/linkedlist-cmxkapfibtnbikengkflkhhlrwta/Build/Products/Debug/linkedlist

duplicate symbol __ZN4ListC2Ev in:
    /Users/robertrenecker/Library/Developer/Xcode/DerivedData/linkedlist-cmxkapfibtnbikengkflkhhlrwta/Build/Intermediates/linkedlist.build/Debug/linkedlist.build/Objects-normal/x86_64/main.o
    /Users/robertrenecker/Library/Developer/Xcode/DerivedData/linkedlist-cmxkapfibtnbikengkflkhhlrwta/Build/Intermediates/linkedlist.build/Debug/linkedlist.build/Objects-normal/x86_64/lists.o
duplicate symbol __ZN4ListC1Ev in:
    /Users/robertrenecker/Library/Developer/Xcode/DerivedData/linkedlist-cmxkapfibtnbikengkflkhhlrwta/Build/Intermediates/linkedlist.build/Debug/linkedlist.build/Objects-normal/x86_64/main.o
    /Users/robertrenecker/Library/Developer/Xcode/DerivedData/linkedlist-cmxkapfibtnbikengkflkhhlrwta/Build/Intermediates/linkedlist.build/Debug/linkedlist.build/Objects-normal/x86_64/lists.o
duplicate symbol __ZN4List7addNodeEi in:
    /Users/robertrenecker/Library/Developer/Xcode/DerivedData/linkedlist-cmxkapfibtnbikengkflkhhlrwta/Build/Intermediates/linkedlist.build/Debug/linkedlist.build/Objects-normal/x86_64/main.o
    /Users/robertrenecker/Library/Developer/Xcode/DerivedData/linkedlist-cmxkapfibtnbikengkflkhhlrwta/Build/Intermediates/linkedlist.build/Debug/linkedlist.build/Objects-normal/x86_64/lists.o
duplicate symbol __ZN4List10deleteNodeEi in:
    /Users/robertrenecker/Library/Developer/Xcode/DerivedData/linkedlist-cmxkapfibtnbikengkflkhhlrwta/Build/Intermediates/linkedlist.build/Debug/linkedlist.build/Objects-normal/x86_64/main.o
    /Users/robertrenecker/Library/Developer/Xcode/DerivedData/linkedlist-cmxkapfibtnbikengkflkhhlrwta/Build/Intermediates/linkedlist.build/Debug/linkedlist.build/Objects-normal/x86_64/lists.o
duplicate symbol __ZN4List9PrintListEv in:
    /Users/robertrenecker/Library/Developer/Xcode/DerivedData/linkedlist-cmxkapfibtnbikengkflkhhlrwta/Build/Intermediates/linkedlist.build/Debug/linkedlist.build/Objects-normal/x86_64/main.o
    /Users/robertrenecker/Library/Developer/Xcode/DerivedData/linkedlist-cmxkapfibtnbikengkflkhhlrwta/Build/Intermediates/linkedlist.build/Debug/linkedlist.build/Objects-normal/x86_64/lists.o
ld: 5 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

and I wan't to know what this means. I read through some things online and it seems that something might be wrong with me header file...

So what i'm writing is a simple linked list with a main file and 1 source cpp + header file attached.

Here is the header file code

#ifndef __linkedlist__lists__
#define __linkedlist__lists__

#include <stdio.h>
class List{

private:

  //a list is created with a bunch of nodes
  typedef struct node{

    int data;
    node* next;

  } *nodePtr;

  //typedef struct node* nodePtr;
  //since we made a define nodePtr we can make more pointers by declaring them as (EDIT: we put the two sentences together when declaring the typedef struct node{} *nodePtr;!!!!!)

  nodePtr head;
  nodePtr curr;
  nodePtr temp;

public:

  List();
  void addNode(int addData);
  void deleteNode(int delData);
  void PrintList();

};

#endif /* defined(__linkedlist__lists__) */

and here is the cpp source file that attaches to the header file

#include "lists.h"
#include <cstdlib>
#include <iostream>
using namespace std;

List::List(){

  head = NULL;
  curr = NULL;
  temp = NULL;

}

void List::addNode(int addData){
  nodePtr n = new node;

  n->next = NULL;
  n->data = addData;

  if(head != NULL){
    curr = head;
    while(curr->next != NULL){

      curr = curr->next;

    }

    curr->next = n;

  }
  else{

    head = n;

  }

}



void List::deleteNode(int delData){

  //were gonna walk through the list and find a value that = deldata and then delete it from the list

  nodePtr delPtr = NULL; //same thing as node* delPtr, we define nodePtr as a node* pointer type

  temp = head;
  curr = head;

  //while loop to advance the two pointers
  while (curr != NULL && curr->data != delData){
    //make sure current pointer isn't pointing to null, which means it's got to the end of the list

    temp = curr;
    curr = curr->next;
    //temp will be right behind (1 behind) curr while going through the list.

  }
  //after weve either gone through the list or we found the curr->data to = delData

  if(curr == NULL){


    cout << delData << " was not in the list\n";
    delete delPtr; //if it wasn't in the list, we don't want the                                delPtr to be sitting there in memory.   

  }
  else{   

    delPtr = curr;//make the deletion pointer point to the node we want to delete
    curr = curr->next;
    //make it so curr goes to the next one so once we delete the node curr won't be pointing to nothing
    temp->next = curr;//the next node after the node that is right before the one that will be deleted will be the next one once the deleted one is gone. Patching the list
    delete delPtr; //deleted the node to the value we wanted gone.

    cout << "The value " << delData << " was deleted.\n" ;


  }

}

void List::PrintList(){

  //this function is just going to print out our list.

  curr = head;

  while(curr != NULL){


    cout << curr->data << endl;
    curr = curr->next;

  }

}

So those are the two header/cpp files that go along with my main file... the main file only has on instance of my object i created.. so literally nothing and it holds no errors.. but just since some of you NEED to have all of the code, here it is:

#include "lists.cpp"

int main(){

  List Paul;
  Paul.addNode(3);
  Paul.addNode(5);
  Paul.addNode(7);
  Paul.addNode(9);
  Paul.addNode(10);

  return 0;
} 
user657267
  • 20,568
  • 5
  • 58
  • 77
  • 2
    Presumably you wanted `#include "lists.h"` in your main file. It's incredibly ironic that you seem to complain about people wanting your complete source when the actual error is in the main file. – user657267 Sep 11 '15 at 04:46
  • 1
    Yes, the error is in your main file, which is why people NEED to see it. If your judgment about what we need to see was reliable then you would already know what the problem was and wouldn't need to ask the question. – Jonathan Wakely Sep 11 '15 at 14:47
  • 1
    Also `__linklist__lists__` is a [reserved name](http://stackoverflow.com/q/228783/981959) and you must not use it, pick another name (e.g. `LISTS_H` or `LISTS_H_INCLUDED`) – Jonathan Wakely Sep 11 '15 at 14:50

1 Answers1

2

You are including not the header file (list.hpp), but the source file list.cpp into your main. As a consequence, all code is compiled twice and the symbols are duplicated.

Walter
  • 44,150
  • 20
  • 113
  • 196