Thread: Searching Array of objects

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    38

    Searching Array of objects

    Doing a simple class with names and ID numbers. In main I created a object array named roster and in main I am trying to search the array to print out the name or the id if the other is entered. However When I compile the I get an error

    no match for 'operator==' in 'roster[i].Student::getId == search'

    both member variables are strings and are private.

    attempting the following

    Code:
     if (roster[i].getId == search)
    Thanks for any input.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Is roster[i].getId a variable or a member function? If it is a member function then you forgot the ()

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    38
    Doh, it's a member function. With that () back in (i have been testing various versions)
    I get this error now.

    no match for 'operator==' in '(((Student*)(&roster)) + (+(((unsigned int)i) * 8u)))->Student::setData() == search'

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You need to post the smallest compilable code fragment that illustrates the problem.

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    38
    I am always afraid to put too much on here for fear of plagiarism, or being accused of cheating.
    I changed the if(roster[i].name == search) name is a member variable. And changed members to public. it worked for what I needed. I don't think that is smart coding but I am not sure how else to get the search to work when the array is in main and the members private.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If the members are private, then main should not have anything to do with them. You could provide member functions to access these variables, though, so called get functions.
    Aside from that, without any code, I'm not sure how much more we can help you.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    38
    It works now.( see following posts). It still has the set function not as streamlined or accurate as it should be though (and a constructor that does nothing...) thanks.

    H file
    Code:
    #ifndef lab0_h
    #define lab0_h
    #include<string>
    #include <iostream>
    
    using namespace std;
    
    class Student
    {
             string name,userid;  
            
          public:
               Student();  
          void setData();      
          string getId();
          string getName();
         
          
          
    };
    #endif
    imp file
    Code:
    #include "lab0.h"
    using namespace std;
    Student::Student ()
    {      
            name;
            userid;        
    }  
    void Student::setData()
    {    
         cout<< "Please enter Student name:";
         getline(cin, name);   
                 
         cout<< "Please enter Student ID:";
         cin>> userid; 
         cin.ignore();    
    }
    string Student::getId()
    {
       return userid ;
    }
    string Student::getName()
    {
          return name;
    }
    Code:
    #include <iostream>
    #include "lab0.h"
    using namespace std;
    
    int main()
    {
        string search;
        Student roster[3];      
              
        for (int i=0 ;i<3;i++)
        {
            roster[i].setData();       
        }    
    
        while ( search != "exit")
        {           
            cout << "enter an ID or Name to search for type exit to quit: ";   
            getline(cin, search);  
            bool found = false;	       
            for (int j=0;j<3;j++)        
            {      
                         
                if (roster[j].getName() == search) 
                    {
                        cout << roster[j].getId() << endl;  
                        found = true;              			   
                    }
                
                else if (roster[j].getId() == search)
                    {
                   cout<< roster[j].getName()<< endl;  
                     found = true;                 			   
                    }                              
            }
                if (search == "exit" )
                    break;          
                if (!found)
                    cout << "no match "<< endl;                                                
        }	   
      
        system("PAUSE");
        return 0;
    }
    Last edited by fadlan12; 08-23-2010 at 06:10 PM. Reason: Updated get functions

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Set functions should not acquire data from cin and get functions should not print out data through cout. That is your problem.
    Set function will take parameters for the data to set and get function will return the data.
    It's your main code's logic to get the input for a student from where you need it (say cin) and pass it along to the student object.
    Doing anything else just makes it inflexible.

    Then you can make your members private and search through the array by using the get functions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    OP, I'm surprised that your compiler had no problems with this, but functions with a return type of void do not return anything. These are errors:

    Code:
    void Student::getId()
    {
       return userid ;
    }
    void Student::getName()
    {
          return name;
    }
    So change the return type to the types of your members, for getId() and getName(), respectively.

    Your constructor also has statements which have no effect.

    If you fix these things, you can make your data members private again, and it should work. I see no other problems.

  10. #10
    Registered User
    Join Date
    Apr 2010
    Posts
    38
    Sorry, I was trying to update the code i posted without another post and did not change that. It does work even though the constructor does not really do anything (string auto initializes to empty right?) . Thanks for all he tips, I stil have a set function that uses cin however.

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    (string auto initializes to empty right?)
    Yes, but I said what you wrote does nothing, and I am correct about that. If you don't want your class constructor to give its members meaningful values, then write nothing. In other code, a regular declaration will work. Don't try to fit a regular declaration of a class member in your constructor.

    Sorry, I was trying to update the code i posted without another post and did not change that
    If you are still having problems, (you're being ambiguous so I don't know) just post what you have again. For the future, make sure what you post is what you have, otherwise, our help is useless.

  12. #12
    Registered User
    Join Date
    Apr 2010
    Posts
    38
    Yea what I meant is when something was pointed out I was trying to update my original code post and prevent cluttering up the thread. Thanks for your advice.

    I will look at something else for the constructor, for this pre lab it was asked to have a constructor: "It should set the instance's data
    members to suitable initial values."

  13. #13
    Registered User Cadbury's Avatar
    Join Date
    Aug 2010
    Location
    The Netherlands
    Posts
    3
    Ran your code just fine..

  14. #14
    Registered User
    Join Date
    Apr 2010
    Posts
    38
    Correct as it stands it's working, it was not working prior to Elysia pointing out that I was not using set and get functions correctly. I will update the comments in the code post.

  15. #15
    Registered User Cadbury's Avatar
    Join Date
    Aug 2010
    Location
    The Netherlands
    Posts
    3
    Oh my bad

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. array of objects?
    By *~*~*~* in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2003, 05:57 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM