0

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

4 Answers 4

2

The methods are member methods of CountedLocations... use something.extractLocator and something.binarySearch or make the histogram() to be also a member method of CountedLocations... (something is of type CountedLocations highly possibly will be countedLocs[position])

2
  • making an object like CountedLocations something; does work, but for this project I'm not allowed to edit the histogram.cpp so it is throwing me off quite a bit. Nov 13, 2013 at 19:27
  • then remove the two funny methods from the struct in the header file. It seems to me they were introduced there just to "CPlusPlus-ize a project with C bindings. If you want to achieve this bind-togetherness, for this situation is better to stick to namespaces, not structs. Nov 13, 2013 at 19:29
1

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(...);
0

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

0

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.

2
  • This does work, but for this project I'm not allowed to edit the histogram.cpp so it is throwing me off quite a bit. Nov 13, 2013 at 19:22
  • Then you should not declare those functions in any namespace. Just make functions in a h file and include them in your histogram.cpp.
    – fonZ
    Nov 13, 2013 at 20:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.