简体   繁体   中英

C++ Error: Was not declared in the scope

Hey guys I'm working on a project and I was doing pretty well until I hit this wall..

I am getting two errors:

error: 'binarySearch' was not declared in this scope

error: 'addInOrder' was not declared in this scope

Here are my files, I've tried quite a few things with no avail. Help would be much appreciated.

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]

You have a free function histogram in which you are trying to use two member functions, addInOrder and binarySearch . In order to use them, you need to have an instance of CountedLocations .

If these are some kind of helper functions, which do not depend on the actual CountedLocations instance, I would turn them into static functions like this (you only need to change the header):

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

And then you can call this function by specifying the type of your class:

CountedLocations::addInOrder(...);

You are trying to call member methods of a struct without an object of that type. Strange.

You need to look at what a namespace is.

You declare a class CountedLocations, so far so good. But then you try to use the member functions outside the CountedLocations namespace which will obviously never work.

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

binarySearch is a member function of the CountedLocations namespace. If you want to call that function you have to create an object that contains a reference to that member function.

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

I dont know if that solves your problem, but you should know this before you even attempt to solve a problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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