0

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

我收到两个错误:

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

错误:未在此范围内声明“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;
}

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

4 回答 4

1

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

于 2013-11-13T19:11:29.863 回答
1

您有一个自由函数histogram,您试图在其中使用两个成员函数,addInOrder并且binarySearch. 为了使用它们,您需要有一个CountedLocations.

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

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

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

CountedLocations::addInOrder(...);
于 2013-11-13T19:11:56.660 回答
0

您试图在没有该类型对象的情况下调用结构的成员方法。奇怪的。

于 2013-11-13T19:14:46.673 回答
0

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

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

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

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

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

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

于 2013-11-13T19:19:39.070 回答