【问题标题】:Two Different Classes Double linked list and Pair class that won't work together两个不同的类 不能一起工作的双链表和 Pair 类
【发布时间】:2012-11-09 03:34:50
【问题描述】:

我正在编写一个类似于 boggle 的程序,但难度要小得多。我的程序程序旨在接收字母的行数(所有相同数量的字母),然后在不同的行中接收字母。按照我要猜的单词数和我要猜的单词后的每一行进行; 如下图:

5
SRENG
OLIOA
VISKE
THAOR
PDTAL
4
LORE
NOSE
ROILS
SAILORS

输出显然是板,但不需要什么特别的,找到了哪些单词,哪些单词没有找到成功的单词的方向:

如:

LORE
"FOUND!"
"W, NE, E"

所以现在这让我感到困惑。我有一个名为 scramble.cc 的主程序,其中包含解决实际问题的所有代码,而我正在使用的其他程序是 pair.h、pair.cc、list.h 和 list.cc(当然我的编译器也是) 我的配对码:

#include <iostream>
#include <cstdlib>
#ifndef PAIR
#define PAIR

using namespace std;

struct Pair{
 int r, c;
 Pair(size_t r1, size_t c1);
 Pair();
 void print(ostream & ostr)const;
 size_t get(size_t index);
};
ostream & operator<<(ostream & ostr, const Pair & p);
bool operator==(const Pair & p1, const Pair & p2);
bool operator!=(const Pair & p1, const Pair & p2);

#endif

pair.cc

#include <iostream>
#include <cstdlib>
#include "pair.h"
using namespace std;

Pair::Pair(size_t r1, size_t c1)
{
 r=r1;
 c=c1;
}

Pair::Pair()
{
r=c=0;
}

void Pair::print(ostream & ostr) const
{
 ostr << r << "," << c;
}

ostream & operator<<(ostream & ostr, const Pair & p)
{
 p.print(ostr)
 return ostr;
}

bool operator==(const Pair & p1, const Pair & p2)
{
 return p1.r == p2.r and p1.c == p2.c
}

bool operator!=(const Pair & p1, const Pair & p2)
{
 return not(p1==p2)
}

我的列表.h:

#include <cstdlib>
#include <iostream>
#include "pair.h"
using namespace std;
class List
{
 public:
  typedef Pair ElementType;
  typedef int ElementType //this is what I used before putting in pair.h/.cc

 List();
 ~List();
 List(const List & orig)
 void add(const ElementType & item, size_t index);
 void removeAt(size_t index);
 void remove(const ElementType & item)
 size_t find(const ElementType & item) const;
 ELementType get(size_t index) const;
 size_t getSize() const;
 void output(std::ostream & ostr) const;

private:
 struct Node{
  Node *prev;
  ELementType data;
  Node*next;
  Node();
  Node(Node *p, Node *n);
  Node(Node *p, const ElementType & d, Node *n);
};

void _setCurrentIndex(size_t index) const;

size_t size;
mutable size_t currentIndex;
Node *front;
Node *rear;
mutable Node *current;

};

我的 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 get(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:

#include <iostream>
#include <cstdlib>
#include "list.h"
#include "pair.h"
using namespace std;

List history;
Pair p1 = Pair(1,1);

int main()
{

}

我的 Makefile

scramble: scramble.o list.o pair.o
     g++ -o scramble scramble.o list.o pair.o
scramble.o: scramble.cc list.h pair.h
     g++ -c scramble.cc
list.o: list.cc list.h pair.h
     g++ -c list.cc
pair.o pair.cc pair.h
     g++ -c pair.cc

这就是我的代码,我必须手动写出来,因为我使用的程序不会复制和粘贴,所以如果有小错误(例如忘记;或拼写错误),请原谅我。

所以我最大的问题是,当我尝试制作 Pairs 并将它们放入 List 时,它会因为无法在 Find 等函数中将 Pair 转换为 size_t 的错误而吓坏了。还有大量未定义的引用List::List()List::~List()Pair::Pair(unsigned long, unsigned long)List::add(Pair const&amp;, unsigned long)List::getSize() constList::removeAt(unsigned long)

那么任何人都可以在这里帮助我了解我需要对我的 List 类和配对做些什么吗?我的目标是将对存储在列表中并能够删除它们,这是我的主要目标。

【问题讨论】:

  • "b/c 我使用的程序不能复制和粘贴" - 呃,什么?
  • 我现在最大的问题是 IDE 不允许我 c/p :) 代码有点多,但我会看看能不能帮上忙。
  • 我没有在我的 scramble.cc 中添加很多内容,因为我主要关心的是通用类文件以及它们何时被普遍应用
  • @icctherdral 非常感谢

标签: c++ list std-pair


【解决方案1】:

作为一条建议,您应该在以后的帖子中包含更少的代码。如果您将问题缩小到特定的代码段,您更有可能获得帮助。此外,准确的错误消息非常有用。

至于你描述的问题,

它给我的错误是无法将 Pair 转换为 Find等函数中的size_t

你的问题在于这段代码:

size_t List::find(const ElementType & item) const
{
   ...
   if(get(i) == item)
    return get(i); 
   ...
}

List::ElementType List::get(size_t index) const

请注意,List::get 返回 List::ElementTypeList::find 返回 size_t。因此,语句return get(i); 试图在一个期望返回size_t 的函数中返回List::ElementType。您很可能希望它改为 return i;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-17
    • 2020-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多