2013-05-07 67 views
0

對不起,我對此很陌生,希望任何人都可以提供一點方向。我正在學習一個數據結構類並學習C++。我們正在使用eclipse,並且在直接從教科書中構建一些代碼時遇到問題。我在Mac OS X上運行Mac OS X 10.7.5的Eclipse 3.5.2上。eclipse C++ build error體系結構x86_64的未定義符號:找不到體系結構x86_64的ld:symbol(s)

我有以下文件:

List.h

// List.h 
#ifndef _LIST_H_ 
#define _LIST_H_ 

#include <cstdlib> 

class List { 

public: 
    List(size_t capacity=10); // constructor - allocates dynamic array 
    List(const List &a); // copy constructor 
    ~List(); // destructor 

    int& operator[](size_t pos); // bracket operator 
    List& operator=(const List &a); // assignment operator 
    List& operator+=(const List &a); // += operator 

    void append(int item); 
    size_t size() const { return size_; } 

private: 
    void copy(const List &a); 
    void resize(size_t new_size); // allocate new larger array 
    int *data_; // dynamic array 
    size_t size_; // size of dynamic array 
    size_t capacity_; // capacity of dynamic array 
}; 

inline int& List::operator[](size_t pos) 
{ 
    return data_[pos]; 
} 

#endif // _LIST_H_ 

List.cpp

// List.cpp 
#include "List.h" 

List::List(size_t capacity) 
{ 
    data_ = new int[capacity]; 
    capacity_ = capacity; 
    size_ = 0; 
} 

List::List(const List &list) 
{ 
    copy(list); 
} 

List::~List() 
{ 
    delete [] data_; 
} 

void List::copy(const List &list) 
{ 
    size_t i; 

    size_ = list.size_; 
    capacity_ = list.capacity_; 
    data_ = new int[list.capacity_]; 
    for (i=0; i<list.capacity_; ++i) { 
    data_[i] = list.data_[i]; 
    } 
} 

List& List::operator=(const List &list) 
{ 
    if (&list != this) { 
    // deallocate existing dynamic array 
    delete [] data_; 
    // copy the data 
    copy(list); 
    } 
    return *this; 
} 

List& List::operator+=(const List &list) 
{ 
    size_t i; 
    size_t pos = size_; 

    if ((size_ + list.size_) > capacity_) { 
    resize(size_ + list.size_); 
    } 

    for (i=0; i<list.size_; ++i) { 
    data_[pos++] = list.data_[i]; 
    } 
    size_ += list.size_; 
    return *this; 
} 

void List::append(int item) 
{ 
    if (size_ == capacity_) { 
    resize(2 * capacity_); 
    } 
    data_[size_++] = item; 
} 

// should this method have a precondition? see end of chapter exercises 
void List::resize(size_t new_size) 
{ 
    int *temp; 
    size_t i; 

    capacity_ = new_size; 
    temp = new int[capacity_]; 
    for (i=0; i<size_; ++i) { 
    temp[i] = data_[i]; 
    } 
    delete [] data_; 
    data_ = temp; 
} 

下出現在控制檯後,我建:

**** Build of configuration Debug for project CList **** 

make all 
Building file: ../List.cpp 
Invoking: GCC C++ Compiler 
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"List.d" -MT"List.d" -o"List.o" "../List.cpp" 
Finished building: ../List.cpp 

Building target: CList 
Invoking: MacOS X C++ Linker 
g++ -o "CList" ./List.o 
Undefined symbols for architecture x86_64: 
    "_main", referenced from: 
     start in crt1.10.6.o 
ld: symbol(s) not found for architecture x86_64 
collect2: ld returned 1 exit status 
make: *** [CList] Error 1 

在此先感謝。

回答

1

該程序必須包含功能main。將它添加到現有的.cpp文件或添加新的.cpp文件到項目中,這樣的事情:

int main(int argc, char** argv) 
{ 
    // use List class here 
    return 0; 
} 

功能main是該計劃的起點。鏈接器期望這個函數在一個源文件中定義。

相關問題