【问题标题】:Sorted list: must have class/struct/union排序列表:必须有类/结构/联合
【发布时间】:2014-03-20 05:48:52
【问题描述】:

所以我已经编写了两个多星期的代码,但进展并不顺利。以下是说明和代码,以及错误:

任务 1:创建此类的一个实例。 (排序列表;他还有其他关于如何启动代码的说明,但我已经在下面的代码中完成了,例如 typedef ...)您还需要从一个数据文件中读取数据:float.dat ,其中包含以下数字:

5.5

6.2

7.1

8.0

9.0

10.0

1.0

2.0

3.3

4.4

float.dat 中的数据包含浮点数,应该插入到 SortedList 的对象中。请注意,您对 float.dat 中的数据值没有任何先验知识,但我们假设数据文件中有 10 个元素。

任务 2:使用 GetNextItem( ) 在计算机屏幕上按排序顺序打印出列表中的所有元素。

任务 3:使用 GetNextItem( ) 将列表中的所有元素按排序顺序输出到数据文件 output.dat。

任务 4:设计测试用例以证明 InsertItem( )、DeleteItem( ) 和 RetrieveItem( ) 按预期工作。

代码如下:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

#define MAX_ITEMS  10
typedef   float  ItemType;

class SortedList
{
private:
    int length;
    ItemType values[MAX_ITEMS];
    int currentPos;
    enum RelationType { LESS, GREATER, EQUAL };

public:
    
    SortedList() {length = 0; currentPos = -1;}

    int getLength() {return length;}
    
    RelationType ComparedTo(ItemType x) 
    {
        if (length > x.getLength())
            return LESS;
        else if (length == x.getLength())
            return GREATER;
        else
            return EQUAL;
    }

    void MakeEmpty() {length = 0;}

    void InsertItem(ItemType x) 
    {   
        int first = 0, last = length --;
        bool moreToSearch = (first <= last);
        int location = 0;
        int midpoint= (first + last) / 2;

        while (moreToSearch) 
        {
            switch (x.ComparedTo(values[location])) 
            {
            case LESS:      //search in 1st half
                moreToSearch = (first <= last);
                break;
            case GREATER:
                location++;
                moreToSearch = (location < length);
                break;
            }
        }
        for (int index = length; length > location; index--) 
        {
            values[index] = values[index - 1];
        }
        values[location] = x;
        length++;
    }

    void DeleteItem(ItemType x) 
    {
        int location = 0;
        while (x.ComparedTo(values[location]) != EQUAL)
            location++;
        for (int index = location ++; index < length; index++)
            values[index --] = values[index];
        length--;
    }

    void RetrieveItem(ItemType &x, bool & found) 
    {
        int midpoint;
        int first = 0, last = length - 1;
        bool moreToSearch = (first <= last);
        found = false;
        int index = 0;
        while (moreToSearch && !found) 
        {
            midpoint = (first + last) / 2;
                
            switch (x.ComparedTo(values[index++])) 
            {
            case LESS:      //search in 1st half
                moreToSearch = (first <= last);
                last = midpoint - 1;
                break;
            case GREATER:   //Search in 2nd half
                first = midpoint + 1;
                moreToSearch = (first <= last);
                break;
            case EQUAL: //x has been found
                found = true;
                break;
            }
        }
    }

    int LengthIs() {return length;}
    
    void ResetList() {currentPos = -1;}
        
    bool IsFull() 
    {
        if (length < 9)
            return false;
        else
            return true;
    }

    void GetNextItem(ItemType &x) 
    {
        currentPos++;
        x = values[currentPos];
        cout << x;
    }   
};

int main()
{
    SortedList x;

    ifstream inFile; ofstream output;
    string line;
    bool allAboutLists;
    int i = 0;
    int size = 0;

    inFile.open("float.txt");

    float values[10];
    while (!inFile.eof())   // write or read data from inFile into values
    {
        inFile >> values[i];
        i++;
        size++;         // this will count how many values there are in the array
        x.InsertItem(values[i]);
        ++i;
    }

    x.ResetList();

    cout << "The following is the list that's been made:" << endl << endl;

    x.InsertItem(64);
    //x.printlist();
    cout << endl;
    x.DeleteItem(64);
    //x.printlist();

    x.RetrieveItem(7.1, allAboutLists); 
    cout << endl;

    cout << endl << "The length is: "; x.LengthIs(); cout << endl;

    cout << "Is the list full?: " << boolalpha << x.IsFull() << endl;
    cout << "The next item is: ";
    for (int i = 0; i < 10; i++)
    {
        cout << x.GetNextItem << endl;
    }
    x.ResetList();

    inFile.close();

    output.open("output.txt");

    for (int f = 0; f < 10; f++)
    {
        output << x.GetNextItem << endl;
    }
    
    system("pause");
    return 0;
}

编译器一直这样说:

  • (25) 错误 C2228:'.getLength' 的左侧必须有类/结构/联合[它们的意思是 x。它的下面有红色内衬,其余的也一样。]
  • (27) 错误 C2228: '.getLength' 左侧必须有类/结构/联合
  • (44) 错误 C2228:'.ComparedTo' 左侧必须有类/结构/联合
  • (66):错误 C2228:“.ComparedTo”左侧必须有类/结构/联合 - 而且,7.1 in main 中存在引用类型错误。

我非常着急,因为我已经为此工作了 2 周,这让我发疯了!我已经完成了所见的代码,而且还远远不够,只需要知道要确切更改什么,因为我正在关注我一直在搜索和研究的所有内容,但这并不好。如此精确的细节或代码特别取自我的并已修复将不胜感激。

谢谢!

【问题讨论】:

  • 为什么将 ItemType 类型定义为浮点数?它不存在于某个地方吗?
  • 因为教授要求我们在项目中这样做。我看到了制作模板 的解决方案(当然也不是因为它也不是很好),并且消除了 x 上的红色,但是,这次我在 main 中未定义类的某些功能列表,当我尝试实际定义排序列表时,它不会识别为类。此外, ...location=x, x 也在那里变红。所以我只是删除了模板并返回到他的代码说明,但它不起作用。

标签: c++


【解决方案1】:

您将x 传递为ItemType,即float

float 没有这些方法...看起来您想将其作为SortedList 传递

【讨论】:

  • 我见过这样的答案,但我该怎么办?我知道程序显然希望我将 x 放在排序列表中,但我不能用它替换 itemtype。我该怎么办?
【解决方案2】:

比较函数需要两个参数才能进行比较。您可能希望将其称为 CompareToLocation,而不是 CompareTo。

RelationType CompareToLocation(ItemType x, size_t location){
    if(x < values[location]) return LESS;
    if(x == values[location]) return EQUAL;
    return GREATER;}

一个示例用法是:

result = CompareToLocation(x, location);
    // ...

【讨论】:

    【解决方案3】:

    您将 CompareTo 定义为 SortedList 的方法,但每次调用该函数时,都会在 ItemType 对象上调用它,这些对象实际上是浮点数。

    正如您在方法的定义中看到的那样,您再次尝试在 float 对象上使用 SortedList 方法:

    RelationType ComparedTo(ItemType x) 
    {
       if (length > x.getLength())
          return LESS;
       else if (length == x.getLength())
          return GREATER;
       else
          return EQUAL;
    }
    

    您的问题并不是真正的编译问题,而是概念问题,因为您似乎没有掌握实际编码的内容。

    我建议将你的声明和实现分开,这样你就可以一眼看出你的类是如何工作的。 您的类声明应如下所示:

    class SortedList
    {
    private:
       int length;
       ItemType values[MAX_ITEMS];
       int currentPos;
       enum RelationType { LESS, GREATER, EQUAL };
    
    public:
    
       SortedList();
    
       int getLength();
    
       RelationType ComparedTo(ItemType x) ;
       void MakeEmpty();
       void InsertItem(ItemType x) ;
       void DeleteItem(ItemType x);
       void RetrieveItem(ItemType &x, bool & found);
       int LengthIs();
    
       void ResetList();
    
       bool IsFull();
    
       void GetNextItem(ItemType &x);
    
    };
    

    您应该专注于每种方法,明确每种方法试图实现什么,以及实现它需要什么(参数)。

    例如:

    RelationType ComparedTo(ItemType x) ;
    

    你的 SortedList 类有这个函数,它接收一个 ItemType (float) 作为参数。

    这是为了达到什么目的?您如何将整个有序列表与单个元素进行比较? 单个数字如何大于、小于或等于一组数字?

    也许您真正想做的是将参数 X 与列表中的元素相匹配? 如果是这种情况,你怎么知道列表中的哪个元素必须与参数 X 进行比较?您应该添加另一个参数,告诉您将 X 与有序列表中的哪个元素进行比较。

    我怀疑这并不能真正解决您的问题,但至少我希望这可以帮助您更好地了解您的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-19
      • 2016-10-21
      • 1970-01-01
      相关资源
      最近更新 更多