2012-11-09 58 views
0

我目前遇到的問題與添加對我的列表程序,然後吐出來讓它在我的爭奪程序中工作,這意味着找到單詞sorta像boggle,但沒有設置大小,我必須生成文字/字母。C++未定義的參考錯誤使用List類和Pair類

此代碼位於我的scramble.cc程序中。

List history; 

bool placeAlreadyUsed(int x, int y, List history) 
{ 
for(size_t i=0; i < history.getSize(); i++) 
    { 
    Pair p1 = history.get(i) 
    if(p1.r == x && p1.c == y) 
    return true; 
    } 
return false 
} 

bool findUsersWord(string findThis, string alreadyFound, List &history, int maxR, int maxC) 
{ 
    // need to find the findThis base case 
    if (findThis == alreadyFound) 
    cout << "SOLVED" << endl; 
    return true; 

    // need to find the first letter within the board and then progress around that. 
    if (alreadyFound.empty()) 
    { 
    for (int rows = 0; rows < maxR; rows++) 
     for (int cols = 0; cols < maxC; cols++) 
     // find the each character within the 
     if (theBoard[rows][cols] == findThis[0]) 
     { 
      alreadyFound = findThis[0]; 
      Pair newR; 
      newR.r = rows; 
      newR.c = cols; 
      history.add(newR); 
      if (findUsersWord(findThis, alreadyFound, history, maxR, maxC)) 
      return true; 
      else 
      { 
      // clear out the found Board 
      size_t temp = history.getSize() 
      for(size_t i=0; i<temp; i++ 
      { 
      history.removeAt(i); 
      } 
      } 
     } 
    } 
    else 
    { 
    // try and find the next letters within the area around the base letter 
    // spin around the letter 3 * 3 grid 
    for (int x= (p1.r > 0 ? p1.r-1: p1.r); y <=(p1.r == (maxR-1) ? p1.r : p1.r+1);x++) 
     for (int y= (p1.c> 0 ? p1.c-1: p1.c); x<=(p1.c == (maxC-1) ? p1.c : p1.c+1);y++) 
     if ((board[x][y] == findThis[alreadyFound.length()]) && (!(x==p1.r && y==p1.c))) 
      // already used letter 
      if (!placeAlreadyUsed(y,x,history)) 
      { 
      alreadyFound += findThis[alreadyFound.length()]; 
      Pair newR; 
      newR.r = x; 
      newR.c = y; 
      history.add(newR, alreadyFound.length()); 
      if (findUsersWord(findThis, alreadyFound, history, maxR, maxC)) 
       return true; 
      else 
      { 
       if (alreadyFound.length() > 1) 
       alreadyFound = alreadyFound.substr(0, alreadyFound.length()-1); 
       history.removeAt(history.getSize()-1); 
      } 
      } 
    return false; 
    } 
    return false; 
} 

我list.cc是有這個代碼有問題的東西:

#include <iostream> 
#include <cassert> 
#include <cstdlib> 
#include "list.h" 

using namespace std; 

List::Node::Node() 
{ 
prev = next = NULL; 
} 

List:: List() 
{ 
front = new Node() 
rear = new Node() 
front->next = rear; 
rear->prev = front; 

currentIndex=0; 
current = front->next; 
size=0; 
} 

List::~List() 
{ 
_setCurrentIndex(0); 
while(current) 
    { 
    Node *temp = current; 
    current = current -> next; 
    delete temp; 
    } 
//not showing deep copy function b/c it isn't important for this program 
void List::add(const ElementType & item, size_t index) 
{ 
assert(0<=index && index <= size); 
_setCurrentIndex(index); 
size++; 

Node *born = new Node; 
born->data = item; 
born->prev = current->prev; 
born->prev->next = current; 
born->prev = born; 
current = born; 
} 

void List::removeAt(size_t index) 
{ 
assert(0<=index<=getSize()); 
_setCurrentIndex(index); 

Node *old = current; 
current->prev->next = current->next; 
current->next->prev = current->prev; 
delete old; 
size--; 
} 

void List::remove(const ElementType & item) 
{ 
for(size_t i=0; i<size; i++) 
    { 
    _setCurrentIndex(i); 
    if(find(item)<getSize()) 
    { 
    Node *tempOld = current; 
    current->next->prev = current->prev; 
    current->prev->next = current->next; 
    current = current->next; 

    delete tempOld; 
    size--; 
    } 
    } 
} 

size_t List::find(const ElementType & item) const 
{ 
for(size_t i=0; i<size; i++) 
    { 
    _setCurrentIndex(i) 
    if(get(i) == item) 
    return i; 
    } 
return getSize(); 
} 

List::ElementType List::get(size_t index) const 
{ 
assert(0 <= index < size); 
_setCurrentIndex(index); 
assert(current->next != NULL); 
return current->data; 
} 

size_t List::getSize() const 
{ 
return size; 
} 

void List::output(std::ostream & ostr) const 
{ 
for(size_t i=0; i<size; i++) 
    { 
    _setCurrentIndex(i); 
    ostr << current->data << " "; 
    } 
ostr << endl; 
} 

void List:: _setCurrentIndex(size_t index) const 
{ 
int x; 
if(currentIndex > index) 
    x = currentIndex - index; 
else 
    x = index-currentIndex; 

if(index < (sizez_t)x) 
    { 
    current = front->next; 
    curentIndex=0; 
    while(currentIndex != index) 
    { 
    current = current->next; 
    currentIndex++; 
    } 
    } 
else if((size-index) < (size_t)x) 
    { 
    current = rear; 
    currentIndex = size; 
    while(currentIndex != index) 
    { 
    current = current->prev; 
    currentIndex--; 
    } 
    } 
else 
    { 
    if(currentIndex > index) 
    { 
    while(currentIndex!=index) 
    { 
     current = current->prev; 
     currentIndex--; 
    } 
    } 
    else 
    { 
    while(currentIndex!=index) 
     { 
     current = current->next; 
     currentIndex++; 
     } 
    } 
    } 
} 

我正的錯誤是 scramble.cc(+的.text 0x480):未定義的引用列表::名單(名單常量&)」 collect2:LD返回1退出狀態 化妝:* [爭奪錯誤1

任何想法究竟是什麼爲g在做什麼和如何解決這個問題?

編輯:我不會錯過任何包含語句,只是沒有把它們放在

+0

您是否在scramble.cc中包含了list.h? –

+0

是的,我只是沒有把它放在這個代碼 –

+0

錯誤是告訴你,你沒有寫出構造函數'List :: List(const List&)'。您發佈的代碼中沒有這樣的構造函數。所以我認爲解決方案是編寫該構造​​函數。'List :: List(const List&)'是一個拷貝構造函數,你可能想在寫它之前先閱讀它。同時您還應該寫作業操作員。閱讀約佔「三法則」,http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three – john

回答

2

它看起來像你的list.h大概聲明拷貝構造函數,List::List(List const&),並且您的scramble.cc嘗試使用它。但是,實際上並沒有在list.cc文件中實現複製構造函數,所以在鏈接時,找不到複製構造函數。你需要在某個地方list.cc實現這個功能:

List::List(List const& other) 
{ 
    // Implement this 
} 

前提示:當你看起來像這樣(file.cc(.text+0x12AB)並提到ld)一個錯誤,它意味着你有一個連接錯誤。這幾乎總是因爲您試圖使用您在一個翻譯單元中聲明的內容,但從未在其他地方定義/實施過。編譯器階段工作正常,因爲通常它只需要找到一個聲明來構成一個格式良好的程序,但是當涉及到將程序鏈接在一起時,鏈接器會拋出,因爲它找不到實際的實現。

+1

是的,沒有;-) - 檢查我的答案。清單類可能不應該需要可拷貝,因爲它是一個複雜的深層結構 - 它是按值傳遞給'PlaceAlreadyUsed'而不是通過const引用(意外?)。 – Roddy

+0

我現在在我的findWord函數中得到seg錯誤。我已經把破發點,並發現它出現在我的第一個遞歸點,但它甚至不與它穿過去,我把一個破發點,在我else語句的開始和它甚至不打印出它只需重新調用該函數後即可將Seg錯誤斷開。 –

+0

@Roddy很好的捕獲。應該檢查一下他是否真的想要複製。 –

0

您必須包括list.h到您的scramble.cc

1

儘管報告的錯誤是您已聲明但未實現複製構造函數,但真正的問題在於您意外地嘗試複製列表的副本。

下面是它發生在哪裏

bool placeAlreadyUsed(int x, int y, List history) 

更改爲const引用和錯誤應該消失。

bool placeAlreadyUsed(int x, int y, const List &history) 

聲明(但不實現)複製構造函數實際上是一種'防止'你的類被意外複製的方法。 boost::noncopyable使用類似的技術,使複製構造函數爲私有。