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

时间:2013-11-13 19:09:08

标签: c++ 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

4 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

您有一个免费功能histogram,您在其中尝试使用两个成员函数addInOrderbinarySearch。要使用它们,您需要具有CountedLocations的实例。

如果这些是某种辅助函数,它们不依赖于实际的CountedLocations实例,我会把它们变成这样的静态函数(你只需要改变标题):

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

然后你可以通过指定你的类的类型来调用这个函数:

CountedLocations::addInOrder(...);

答案 2 :(得分:0)

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

答案 3 :(得分:0)

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

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

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

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

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

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