繁体   English   中英

C ++错误:未在范围中声明

[英]C++ Error: Was not declared in the scope

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

我收到两个错误:

错误:未在此范围内声明'binarySearch'

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

这是我的文件,我已经尝试了很多事情但没有用。 非常感谢帮助。

histogram.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;
}

countedLocs.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()也成为CountedLocations的成员方法...( 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));

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM