体系结构x86_64的未定义符号:简单C ++ LinkedList

时间:2015-03-24 02:01:06

标签: c++

尝试用C ++编写基本的链表。我收到错误: Node.cpp编译得很好,似乎工作正常。我知道这个问题与继承有关,但我无法解决这个问题!我是否需要为链表添加​​构造函数?是makefile吗?我在Mac上运行它,如果这有任何区别。

Undefined symbols for architecture x86_64:
    "Node::setData(int)", referenced from:
        LinkedList::addFront(int) in linkedlist.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[1]: *** [linkedlist] Error 1
make: *** [all] Error 2

我的makefile:

# set the C compiler, for C++ use g++
# use this setting on most sane machines
CC = g++

# use this line on the engin linux machines
# CC = gcc296

# set the path to the include directory
INCDIR =../include

# set the flags for the C and C++ compiler to give lots of warnings
CFLAGS = -I$(INCDIR) -O2 -Wall -Wstrict-prototypes -Wnested-externs -Wmissing-prototypes -Wmissing-declarations
CPPFLAGS = $(CFLAGS)

# path to the object file directory
ODIR = obj

# path to the lib directory
LIBDIR =../lib

# path to the bin directory
BINDIR =../bin

# libraries to include
LIBS = -lm -limageIO
LFLAGS = -L$(LIBDIR)

# put all of the relevant include files here
_DEPS = ppmIO.h node.h linkedlist.h

# convert them to point to the right place
DEPS = $(patsubst %,$(INCDIR)/%,$(_DEPS))

# put a list of the executables here
EXECUTABLES = ppmtest node linkedlist

# put a list of all the object files here for all executables (with .o endings)
_OBJ = ppmmain.o node.o linkedlist.o

# convert them to point to the right place
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))

# patterns for compiling source code
# $< is the file that caused the action to occur
$(ODIR)/%.o: %.c $(DEPS)
    $(CC) -c -o $@ $< $(CFLAGS)

$(ODIR)/%.o: %.C $(DEPS)
    $(CC) -c -o $@ $< $(CFLAGS)

$(ODIR)/%.o: %.cpp $(DEPS)
    $(CC) -c -o $@ $< $(CFLAGS)


all:
    for i in $(EXECUTABLES); do (make $$i) ; done


# put the specific executable rules here
ppmtest: $(ODIR)/ppmmain.o
    $(CC) -o $(BINDIR)/$@ $^ $(LFLAGS) $(LIBS)

node: $(ODIR)/node.o
    $(CC) -o $(BINDIR)/$@ $^ $(LFLAGS) $(LIBS)

linkedlist: $(ODIR)/linkedlist.o
    $(CC) -o $(BINDIR)/$@ $^ $(LFLAGS) $(LIBS)

.PHONY: clean

clean:
    rm -f $(ODIR)/*.o *~ core $(INCDIR)/*~ 

我的linkedlist.h:

#ifndef LINKEDLIST_H
#define LINKEDLIST_H

class LinkedList {    
    public:
        Node* head;
        void addFront(int data);
};

#endif  /* LINKEDLIST_H */ 

我的linkedlist.cpp

#include "node.h"
#include "linkedlist.h"
#include <stdio.h>

void LinkedList::addFront(int data){
    Node node;
    node.setData(0);

    // if head is not empty, set this node's next pointer to current head
    if(head != 0){
        node.setNext(head);
    }

    // otherwise, just set head to this node 
    head = &node;
}


int main(){
    LinkedList list;
    list.addFront(0);
    //list.head -> getData();    
}

我的node.h

#ifndef NODE_H
#define NODE_H

class Node {
    // don't want the data contained in the node to be accessible. 
    int data;
    Node* next;

    public:
        int getData(void);
        void setData(int num);
        Node* getNext(void);
        void setNext(Node* node);
};

#endif  /* NODE_H */ 

我的node.cpp

#include "node.h"
#include <stdio.h>

//using namespace std;

int Node::getData(void){
    return data;
    }

void Node::setData(int num){
    data = num;
}

Node* Node::getNext(void){
    return next;
}

void Node::setNext(Node* node){
    next = node;
}

int main(){
    Node node1;
    Node node2;

    node1.setData(0);
    node2.setData(1);
    node1.setNext(&node2);
    int x = node1.getNext()->getData();
    printf("node data:%i", x);
}

0 个答案:

没有答案