0

我在学习 C++ 方面相当新。我很困惑,任何帮助将不胜感激。我在课堂上得到了(main.cpp)和(ADTbag.h),并被告知要完成课堂实施(“ADTbag.cpp”)。老师有语言问题,很难从教授那里学到任何东西。我的问题是两个(main.cpp)和(ADTbag.h)要合并吗?很困惑:我只需要一个关于如何处理这种痛苦的虚拟解释。下面是课堂上给出的代码:

#include "ADTbag.h"

#include<string>

#include<iostream>

using namespace std;


void displayBag(ArrayBag<string>& bag)
{
cout << "The bag contains "<< bag.getCurrentSize() << " items:\n";

vector<string> bagItems=bag.toVector();

int numberOfEntries=(int)bagItems.size();

for (int i=0; i<numberOfEntries; i++)
{
    cout <<bagItems[i] << " ";
}
cout << endl;
}

void main() {

ArrayBag<string> bag;

cout << "Testing the array-based bag:" <<endl;

if (bag.isEmpty())

cout << "The initial bag is empty.\n";

displayBag(bag);

string items[]={"one", "two", "three", "four", "five", "one", "two"};

cout << "Add 7 items to the bag:" <<endl;

for (int i=0; i<7; i++)
   {

    bag.add(items[i]);
}
displayBag(bag);
cout << "The string \"one\" occurs " << bag.getFrequencyOf("one") << " times.\n";

cout << "The string \"two\" occurs " << bag.getFrequencyOf("two") << " times.\n";

cout << "The string \"three\" occurs " << bag.getFrequencyOf("three") << " time.\n";

cout << "The string \"seven\" occurs " << bag.getFrequencyOf("seven") << " time.\n";

cout<< "Remove string \"one\" ...\n";

bag.remove("one");

displayBag(bag);

cout<< "Remove string \"two\" ...\n";

bag.remove("two");

displayBag(bag);

if (bag.contains("three"))

    cout << "The bag contains the string \"three\".\n";
else 
    cout << "The bag does NOT contain the string three.\n";

if (bag.contains("seven"))

    cout << "The bag contains the string \"seven\".\n";
else 
    cout << "The bag does NOT contain the string \"seven\".\n";

cout << "Delete all items fron the bag. \n";

bag.clear();

displayBag(bag);

}

另外,当我将它放入编译器开始时,“#include ADTbag.h”带有红色下划线,给出错误“无法打开源文件“bag.h”驱动程序如下:

#ifndef _ARRAY_BAG
#define _ARRAY_BAG

#include <vector>

const int MAX_LIST=20;  // Maximum capacity of the bag

using namespace std;

template <class ItemType>
class ArrayBag
{
  public:

    ArrayBag();  //default constructor. Create an empty list.

    bool isEmpty() const;  // test if the bag is empty

    int getCurrentSize() const; // get the number of items in the bag

    bool add(const ItemType& newEntry);
    //Insert the newEntry into a bag that is not full.  Place it right after the last item in the array.

    bool remove(const ItemType& anEntry);
    //Remove an item from the bag that matches the newEntry

    int getFrequencyOf (const ItemType& anEntry) const;
    // To count the number of times a given object occurs in a bag.

    bool contains(const ItemType& anEntry) const;
    // To test if the item "anEntry" is in the bag.

    void clear(); // Remove all items from the bag.

    vector <ItemType> toVector() const; 
    // Get the entries that are in a bag and return them within a vector.

  private:

     ItemType items[MAX_LIST];  // Array of bag items

     int size; // current count of bag items
};
#endif
4

1 回答 1

0

您忘记添加 ADTbag.h 的源代码(据我了解,您的源代码都相同)。但基本理论很简单:

  1. 将所有三个文件(main.cpp、ADTbag.h 和 ADTbag.cpp)放在一个目录中,
  2. 在 ADTbag.cpp 中添加一行#include "ADTbag.h",并在 ADTbag.cpp 中添加来自 (ADTbag.h) 的所有函数的实现。

所以你的 ADTbag.cpp 会是这样的:

#include "ADTbag.h"

ArrayBag::ArrayBag()
{
  size = 0; // initialize empty list
}

bool ArrayBag::isEmpty() const
{
  return (size == 0); // return true if list is empty
}

int ArrayBag::getCurrentSize() const
{
  return size; // return size of the list
}

// ... 
于 2013-09-25T11:31:37.343 回答