Thread: For some reason my "find" function is not working in my if statement.

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    9

    For some reason my "find" function is not working in my if statement.

    I am trying to implement my database, and I'm working on the "addtable" function, but for some reason, the "find" method didn't work(which is my first "if statement" in the code), and I have gotten this error:

    " ‘void Database::addTable(std::string, Table)’:
    Database.cpp:18:13: error: ‘class std::vector<Table>’ has no member named ‘find’
    if (tables.find(name) != tables.end())"

    So I modified it, and it still won't work, can anyone help me understand the problem? Thanks.

    Code:
    #include "Database.h"
    #include <iostream>
    #include <algorithm>
    
    
    Database::Database(){
    
    
    }
    
    
    Database::Database(vector<Table> tables){
        
    }
    
    
    void Database::addTable(string name, Table t){
        //if (tables.find(name) != tables.end()) --Had this, which gave me the error above.
    
    //So I changed to this:
        if (find(tables.begin(), tables.end(), name) != tables.end())
            throw InvalidOperation("Table " + name + " is in the database already");
        tables[name] = t;
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    vector doesn't have a find method, you're probably mistaking it to std::find in <algorithm>, and this what you use in the end as I can see.

    Another problem is that you've misunderstood how vectors are used. A vector's indices must be integers, what you may want to use instead is a map.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Jan 2017
    Posts
    9
    Ah I see. We were asked to use vectors, and not maps. So I don't know, what changes to apply to get the code above working, should i instead iterate through the vector?

  4. #4
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    The reason a map was suggested is because it looks like you want to access the data by a string and that's most efficiently accomplished by using a map.

    If you have to use vectors, you can probably get away with using a vector of pairs or tuples.

  5. #5
    Registered User
    Join Date
    Mar 2016
    Posts
    203
    OP: you can have a bool variable that keeps track of whether or not a match has been found:
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    struct Table
    {
        std::string m_name;
        Table(const std::string& name): m_name(name){}
    };
    struct Database
    {
        std::vector<Table> tables;
        void addTable(const std::string& name);
    };
    void Database::addTable(const std::string& name)
    {
        bool match = false;
        for (const auto& elem : tables)
        {
            if(elem.m_name == name)
            {
                match = true;
                break;
            }
        }
        if(match == true)
        {
           // std::throw InvalidOperation("Table " + name + " is in the database already");
        }
        else
        {
            tables.push_back(Table(name));
        }
    }
    Another point in your code:
    Code:
     void Database::addTable(string name, Table t)
    given what you're trying to do using the overload Table ctor should be sufficient and you don't need the second argument Table t

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    So I modified it, and it still won't work,
    What do you mean by "still won't work"?

    If you're getting some kind of error message please post the complete error message (all of them) exactly as reported by your development environment. Otherwise please thoroughly explain the problem.

    By the way have you overloaded the proper comparison operator in your class so std::sort knows how to compare your class?

    Jim

  7. #7
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    By the way have you overloaded the proper comparison operator in your class so std::sort knows how to compare your class?
    Oh man, it's things like this that I think motivated the push for Concepts, sane compiler errors for new C++ programmers instead of the compiler failing to find a proper definition for the operation and then basically puking the entire call chain to the console.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. my malloc is stuck on "8" for some reason... why?
    By c_seeker in forum C Programming
    Replies: 4
    Last Post: 01-08-2014, 03:19 AM
  2. Replies: 2
    Last Post: 12-23-2012, 04:52 AM
  3. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  4. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM
  5. can't find error function (s/a "erf") in borland c++ libraries
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 11-05-2001, 12:04 PM

Tags for this Thread