C++ 错误:未在作用域中声明

C++ Error: Was not declared in the scope

本文关键字:作用域 声明 错误 C++      更新时间:2023-10-16

嘿伙计们,我正在做一个项目,在我碰到这堵墙之前,我做得很好。

我收到两个错误:

错误:"二进制搜索"未在此范围内声明

错误:"addInOrder"未在此范围内声明

这是我的文件,我已经尝试了很多事情,但无济于事。帮助将不胜感激。

直方图.cpp

#include "histogram.h"
#include "countedLocs.h"
//#include "vectorUtils.h"
#include <string>
#include <vector>
using namespace std;
void histogram (istream& input, ostream& output)
{
  // Step 1 - set up the data
  vector<CountedLocations> countedLocs;
  // Step 2 - read and count the requested locators
  string logEntry;
  getline (input, logEntry);
  while (input)
    {
      string request = extractTheRequest(logEntry);
      if (isAGet(request))
    {
      string locator = extractLocator(request);
      int position = binarySearch (countedLocs,
                       CountedLocations(locator, 0));
      /** Hint - when looking CountedLocations up in any kind
          of container, we really don't care if the counts match up
          or not, just so long as the URLs are the same. ***/
      if (position >= 0)
        {
          // We found this locator already in the array.
          // Increment its count
          ++countedLocs[position].count;
        }
      else
        {
          // This is a new locator. Add it.
          CountedLocations newLocation (locator, 1);
          addInOrder (countedLocs, newLocation);
        }
    }
      getline (input, logEntry);
    }
  // Step 3 - write the output report
  for (int i = 0; i < countedLocs.size(); ++i)
    output << countedLocs[i] << endl;
}

伯爵位.cpp

#include "countedLocs.h"
#include <iostream>
#include <vector>
using namespace std;

int CountedLocations::binarySearch(const vector<CountedLocations> list, CountedLocations searchItem)
{
   //Code was here
}
int CountedLocations::addInOrder (std::vector<CountedLocations>& vectr, CountedLocations value)
{
   //Code was here
}

countedLocs.h

#ifndef COUNTEDLOCATIONS
#define COUNTEDLOCATIONS
#include <iostream>
#include <string>
#include <vector>

struct CountedLocations
{
    std::string url;
    int count;
    CountedLocations (){
     url = "";
     count = 0;
    }
    CountedLocations(std::string a, int b){
     url = a;
     count = b;
    }
    int addInOrder (std::vector<CountedLocations>& vectr, CountedLocations value);
    int binarySearch (const std::vector<CountedLocations> list, CountedLocations searchItem);
};
inline
std::ostream& operator<< (std::ostream &out, CountedLocations& cL)
{
    //out << "URL: " << cL.url << " count: " << cL.count << std::endl;
    out << """ << cL.url << ""," << cL.count;
    return out;
}
#endif

这些方法是 CountedLocations 的成员方法...使用something.extractLocatorsomething.binarySearch或使histogram()也成为计数位置的成员方法...(something属于CountedLocations类型,很可能是countedLocs[position]

你有一个自由函数histogram,你在其中尝试使用两个成员函数,addInOrderbinarySearch 。为了使用它们,您需要有一个 CountedLocations 的实例。

如果这些是某种不依赖于实际CountedLocations实例的辅助函数,我会将它们转换为这样的静态函数(您只需要更改标头):

static int addInOrder (std::vector<CountedLocations>& vectr, CountedLocations value);

然后,您可以通过指定类的类型来调用此函数:

CountedLocations::addInOrder(...);

您正在尝试调用没有该类型对象的结构的成员方法。奇怪。

您需要查看命名空间是什么。

你声明了一个类 CountedLocations,到目前为止一切顺利。但是,您尝试在 CountedLocations 命名空间之外使用成员函数,这显然永远不会起作用。

int position = binarySearch (countedLocs,
                   CountedLocations(locator, 0));

二进制搜索是 CountedLocations 命名空间的成员函数。如果要调用该函数,则必须创建一个包含对该成员函数的引用的对象。

CountedLocation myObject;
int position = myObject.binarySearch (countedLocs, CountedLocations(locator, 0));

我不知道这是否能解决你的问题,但在你尝试解决问题之前,你应该知道这一点。