-1

This program allows for a user to add an items storage location and search for an item by name to find it later. I am getting a weird error when I try and run my code. Everything was working fine until I added in a search function for the vector. Any ideas on what is going wrong since I don't see a line item issue?

The red section of GDB error report says this:

/tmp/ccngGZSU.o: In function `bool __gnu_cxx::__ops::_Iter_equals_val<item const>::operator()<__gnu_cxx::__normal_iterator<item*, std::vector<item, std::allocator<item> > > >(__gnu_cxx::__normal_iterator<item*, std::vector<item, std::allocator<item> > >)':
main.cpp:(.text._ZN9__gnu_cxx5__ops16_Iter_equals_valIK4itemEclINS_17__normal_iteratorIPS2_St6vectorIS2_SaIS2_EEEEEEbT_[_ZN9__gnu_cxx5__ops16_Iter_equals_valIK4itemEclINS_17__normal_iteratorIPS2_St6vectorIS2_SaIS2_EEEEEEbT_]+0x2b): undefined reference to `operator==(item const&, item const&)'
collect2: error: ld returned 1 exit status

The code is only 150 lines long, so 199:17 doesn't exist as far as I know.

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

struct item {
  string name;

  string color;

  string size;

  string location;
};

void additem()

{
  int num;

  cout << "How any items do you wish to add? " << endl;
  cin >> num;

  vector<item> items(num);

  item hold;

  for (vector<item>::size_type i = 0; i < items.size(); i++) {
    cout << "What is the item name? " << endl;
    cin >> hold.name;

    cout << "What is the item color? " << endl;
    cin >> hold.color;

    cout << "What is the item size? " << endl;
    cin >> hold.size;

    cout << "What is the item location? " << endl;
    cin >> hold.location;

    items[i] = hold;
  }
}

void search()

{
  item s;

  typedef vector<item> itemsearch;
  typedef itemsearch::iterator itemiterator;

  itemsearch items;

  cout << "Enter the item name to search for. " << endl;
  cin >> s.name;
  cout << "Enter the item color to search for. " << endl;
  cin >> s.color;
  cout << "Enter the item size to search for. " << endl;
  cin >> s.size;

  itemiterator i;
  i = find(items.begin(), items.end(), s);

  if (i != items.end()) {
    cout << "Item Found!" << endl;
    cout << "The location is . . ." << endl;
    cout << s.location << endl;
  } else {
    cout << "Item Not Found" << endl;
  }
}

void menu()

{
  int menu;

  while (menu != 3) {
    cout << "Please select an option from the menu to begin." << endl;

    cout << "1. Add Item/s " << endl;
    cout << "2. Search for Item " << endl;
    cout << "3. Exit Program " << endl;

    cin >> menu;

    if (menu == 1)

    {
      cout << "You selected to add an item." << endl;
      additem();
    }

    if (menu == 2)

    {
      cout << "You selected to search for an item." << endl;
      search();
    }

    if (menu == 3)

    {
      cout << "Exiting Prgram." << endl;
      cout << ". . . . ." << endl;

      exit;
    }
  }
}

int main()

{
  cout << "Welcome to Brandon's Box" << endl;
  cout << "This program will allow you to add details about an item and where "
          "it is stored."
       << endl;

  menu();
}
cigien
  • 57,834
  • 11
  • 73
  • 112
  • Read more of the error message. Eventually you'll see a line of your own code, or a file-name, and line-number from your own code. Add that information to the question. – cigien Nov 28 '20 at 19:17
  • i was going to complain that you should include the error message, but i actually does not point to the problem in your code. Unfortuantely there are some corners of algorithm that can trigger really bad error messages. Fwiw, here is what gcc reports: https://godbolt.org/z/46ssPd – 463035818_is_not_an_ai Nov 28 '20 at 19:18
  • Unrelated but a bug: `if (menu = 3)` The = operator is assignment. Comparison is == – drescherjm Nov 28 '20 at 19:18
  • @cigien actually no. Or I missed it. See above link – 463035818_is_not_an_ai Nov 28 '20 at 19:19
  • @idclev463035818 Not exactly, it does say "required from here" and mentions a file, and line number. I edited my comment. – cigien Nov 28 '20 at 19:19
  • Interesting fun fact: Including headers effectively pastes the contents of the header into the including file making the file that ultimately is compiled much longer than the source. – user4581301 Nov 28 '20 at 19:28
  • It looks like `search()` always tries to search through an empty vector, so even if `operator==` was working correctly you'd always get "Item Not Found". A similar issue affects `additem()`. – Nathan Pierson Nov 28 '20 at 19:28
  • I checked the gcc report, but am still having trouble figuring out what the problem is. If I remove the algorithm include line can I still use the find function? – BrandonElwell77 Nov 28 '20 at 19:30
  • @NathanPierson the vector gets filled when additem() is ran. Does that mean I can't have search()? – BrandonElwell77 Nov 28 '20 at 19:32
  • Can you see the entire error message? If so, go ahead and paste that into the question. I'm confident there will be some indication of *where* in your code the error comes from, even if it's completely unclear *why*. – cigien Nov 28 '20 at 19:36
  • @cigien full error message added – BrandonElwell77 Nov 28 '20 at 19:44
  • Not just the full part of the "red section". I mean the entire error message. Is that line all you can see? – cigien Nov 28 '20 at 19:46
  • @BrandonElwell77 What "the vector"? There's `items`, which is local to the `additem` function and gets destroyed and created anew every time it's called. There's a _different_ vector `items` in `search` that is _also_ destroyed and created anew every time `search` is called. – Nathan Pierson Nov 28 '20 at 19:47
  • @cigien yes that is all I see on the error message. – BrandonElwell77 Nov 28 '20 at 19:49
  • @NathanPierson I have to adjust the code to use push.back but want to make sure I can search vector entries first. – BrandonElwell77 Nov 28 '20 at 19:50
  • Oh, ok. I was confused because you said "red section". So I assumed there are other sections that are not red. – cigien Nov 28 '20 at 19:50
  • @cigien no problem – BrandonElwell77 Nov 28 '20 at 19:52
  • Ok, now I'm confused as to where you got the line `199:17` from. I don't see that in the error message. Please paste the *entire* output that you get. – cigien Nov 28 '20 at 19:55
  • You've edited the question after receiving an answer, invalidating that answer. As it is now, I'd close this as a dup of [this one](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix). – 1201ProgramAlarm Nov 28 '20 at 20:04
  • @1201ProgramAlarm I edited that line out. Anyway that dupe wouldn't apply because the error message in the question is about something else. – cigien Nov 28 '20 at 20:06
  • @cigien My comment was directed at OP. The error message I see says "undefined reference to 'operator=='" (because the function is not implemented) and was not in the original question. – 1201ProgramAlarm Nov 28 '20 at 20:11
  • @1201ProgramAlarm Ah, I see. I edited that change out, because I don't want the answer to be invalidated. – cigien Nov 28 '20 at 20:14

1 Answers1

2

Error messages for the algorithm library are indeed terrible. I hope with concepts we get better error messages for the standard library in the future.

Let's look at the documentation of std::find. There it says that the signature of std::find is

template <class InputIterator, class T>
   InputIterator find (InputIterator first, InputIterator last, const T& val);

with first and last being input iterators and val being the value you want to search for. The important part in the documentation is that val must be a type supporting comparisons with the elements pointed by InputIterator using operator== . Your struct item does support that. Add the code below as a free function (after struct item definition) and the code will compile.

bool operator==(const item& first, const item& second) {
    // Implement-me as appropriate
    return false;
}

When you finish the implementation of bool operator==(const item& first, const item& second) then std::find will do what you want.


And as @drescherjm commented, you probably want to change menu = number in the ifs to menu == number.

darcamo
  • 3,294
  • 1
  • 16
  • 27
  • I added the bool operator line above the typedefs and get the same error when compiling: '/usr/include/c++/6/bits/predefined_ops.h:199:17: error: no match for ‘operator==’ (operand types are ‘item’ and ‘const item’)' Not sure what went wrong. Also I added the second = in each menu option – BrandonElwell77 Nov 28 '20 at 19:36
  • It must be a free function and thus outside search. Put it after `struct item` definition. – darcamo Nov 28 '20 at 19:38
  • Okay moved it below the struct itemm definition. This is the new error 'main.cpp:(.text._ZN9__gnu_cxx5__ops16_Iter_equals_valIK4itemEclINS_17__normal_iteratorIPS2_St6vectorIS2_SaIS2_EEEEEEbT_[_ZN9__gnu_cxx5__ops16_Iter_equals_valIK4itemEclINS_17__normal_iteratorIPS2_St6vectorIS2_SaIS2_EEEEEEbT_]+0x2b): undefined reference to `operator==(item const&, item const&)' collect2: error: ld returned 1 exit status' – BrandonElwell77 Nov 28 '20 at 19:40
  • Sorry I'm not great at troubleshooting these kind of compiler issues. – BrandonElwell77 Nov 28 '20 at 19:41
  • You must implement the function. You probably just added its signature. I have edited the answer. – darcamo Nov 28 '20 at 19:41
  • On the implementation do I nest the other functions search() and additem() inside? – BrandonElwell77 Nov 28 '20 at 19:51
  • Okay I tried setting the bool statement as the function name and now it compiles. Time to work on actually storing searchable info to the items vector. Thank you for the help! It is much appreciated. – BrandonElwell77 Nov 28 '20 at 19:56
  • @BrandonElwell77 I'm glad your problem is solved. Can you please edit the question to include the error message as well? – cigien Nov 28 '20 at 19:57