c++库重构编译器错误

c++ library refrencing compiler error

本文关键字:错误 编译器 重构 c++      更新时间:2023-10-16

这是一个简单的问题,但我似乎找不到问题

    #include <iostream>
namespace utils {
    class IntList {
      public:
        IntList();                         // constructor; initialize the list to be empty
        void AddToEnd(int k);              // add k to the end of the list
        void Print(ostream &output); // print the list to output
      private:
        static const int SIZE = 10;      // initial size of the array
        int *Items;                      // Items will point to the dynamically allocated array
        int numItems;                    // number of items currently in the list
        int arraySize;                   // the current size of the array
    };
}

在这里,我在头文件中定义了一个类

但是它抛出了一个编译器错误,说它找不到对ostream 的引用

stl中的类位于命名空间std.中

因此,除非您正在执行using namespace std,否则必须在它们前面加上std::。在您的情况下,您应该编写std::ostream

您缺少ostream前面的std::

您可以:

  1. 在类定义之前使用整个名称空间:using namespace std;

  2. 标记您将使用std::ostream:using namespace std::ostream;

  3. 或者在任何需要使用的地方编写std::ostream

您也可以在调用ostream 之前添加using namespace std