0

我目前在将 Pairs 添加到我的 List 程序中遇到问题,然后将其吐出以使其在我的 scramble 程序中工作,这意味着要找到类似于 boggle 的单词,但没有设置大小,我必须生成单词/字母.

这段代码在我的 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): undefined reference to List::List(List const&)' collect2: ld returned 1 exit status make: * [scramble Error 1

任何想法到底发生了什么以及如何解决这个问题?

编辑:我没有遗漏任何包含语句,只是没有将它们放入

4

3 回答 3

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)时,这意味着您遇到了链接器错误。这几乎总是因为您试图使用您在一个翻译单元中声明但从未在其他任何地方定义/实现的东西。编译器阶段工作得很好,因为它通常只需要找到一个声明即可构成一个格式良好的程序,但是在将您的程序链接在一起时,链接器会因为找不到实际的实现而抛出。

于 2012-11-09T10:31:05.887 回答
1

虽然报告的错误是您已声明但未实现复制构造函数,但真正的问题是您一开始不小心尝试制作列表的副本。

这就是它发生的地方

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

将此更改为 const 引用,错误应该会消失。

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

声明(但不实现)复制构造函数实际上是一种“防止”您的类被意外复制的方法。boost::noncopyable使用类似的技术,将复制构造函数设为私有。

于 2012-11-09T10:42:52.293 回答
0

您必须将 list.h 包含在您的 scramble.cc 中

于 2012-11-09T10:27:08.443 回答