所以我收到了这个错误
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)
我不知道这意味着什么。我在线阅读了一些内容,似乎我的头文件可能有问题......
所以我写的是一个简单的链接列表,其中包含一个主文件和一个源cpp +头文件。
这是头文件代码
#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__) */
,这是附加到头文件
的cpp源文件#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;
}
}
所以这些是与我的主文件一起出现的两个头文件/ cpp文件...主文件只有我创建的对象的实例..所以字面上没什么,它没有错误..但只是因为一些你需要拥有所有的代码,这里是:
#include "lists.cpp"
int main(){
List Paul;
Paul.addNode(3);
Paul.addNode(5);
Paul.addNode(7);
Paul.addNode(9);
Paul.addNode(10);
return 0;
}
答案 0 :(得分:2)
您不是包含头文件(list.hpp
),而是将源文件list.cpp
包含在主文件中。因此,所有代码都被编译两次,符号被复制。