Cannot call member function without object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • randysimes
    New Member
    • Oct 2009
    • 54

    Cannot call member function without object

    I am trying to pass uid and pwd to the function CheckPW of the class PWServer.
    I receive the error pwclient.cpp:41 : error: cannot call member function âint PWServer::Check PW(const char*, char*)â without object
    Where am I going wrong?

    Here is the code in int main():
    cout << "User ID: ";
    cin >> uid;
    cout << "Password: ";
    cin >> pwd;
    PWServer::Check PW (uid, pwd)
  • whodgson
    Contributor
    • Jan 2007
    • 542

    #2
    Can`t see anything wrong with that snippet ---probably need to post more code.

    Comment

    • randysimes
      New Member
      • Oct 2009
      • 54

      #3
      #include <xstring.h>
      #include <pwserver.h>
      #include <iostream>
      #include <istream>
      using namespace std;
      using std::cin;
      using std::cout;
      using std::endl;

      char choice;
      char* user;
      char* password;

      int main ()
      {
      cout << "Make selections using the following menu:" << endl;
      cout << "Check Password (uid, upw) 1" << endl;
      cout << "Change Password (uid, upw, npw) 2" << endl;
      cout << "Create New User (uid, upw) 3" << endl;
      cout << "Delete User (uid) 4" << endl;
      cout << "Dump () d" << endl;
      cout << "Display Output File f" << endl;
      cout << "Display main (this) menu m" << endl;
      cout << "Quit q" << endl;

      do
      {
      cout << "Enter choice: ";
      cin >> choice;
      switch (choice)
      {
      case '1':
      cout << "User ID: ";
      cin >> user;
      cout << "Password: ";
      cin >> password;
      PWServer::Check PW(user, password);

      Comment

      • whodgson
        Contributor
        • Jan 2007
        • 542

        #4
        So the PWserver class is declared in pwserver.h--- is this correct? If so where is the PWServer class definition which includes its methods and data.?

        Comment

        • randysimes
          New Member
          • Oct 2009
          • 54

          #5
          Yes, PWServer is declared in pwserver.h.
          PWServer methods and data for my first problem
          int CheckPW (const char* uid, char* upw);
          takes the user id (uid) and password (pwd) and checks its validity and returns 1 if valid and 0 if not valid

          Comment

          • randysimes
            New Member
            • Oct 2009
            • 54

            #6
            Here is the code for PWServer:
            class PWServer
            {
            public:
            // constructors
            PWServer (const char* infile, const char* outfile, unsigned int max);
            ~PWServer ();

            // password services

            int CheckPW (const char* uid, char* upw);
            // return 1 iff the signature matches the user id

            int ChangePW (const char* uid, char* upw, char* newpw);
            // return 1 iff user signature is successfully updated

            int CreateUser (const char* uid, char* upw);
            // return 1 iff new user,signature is successfully inserted and file updated

            int DeleteUser (const char* uid);
            // return 1 iff user is sucessfully deleted and file updated

            void Dump(std::ostre am& out1 = std::cout);
            // public during white-box testing, then privatized

            Comment

            • JonathanS
              New Member
              • Jan 2008
              • 37

              #7
              What if you instantiate a PWServer using new?

              Or I think you could change your PWServer class to make the CheckPW() function "static," but that may not be a good design...

              Comment

              • randysimes
                New Member
                • Oct 2009
                • 54

                #8
                How would I do that? This is my first progamming class with C++

                Comment

                • JonathanS
                  New Member
                  • Jan 2008
                  • 37

                  #9
                  PWServer pws = new PWServer(infile ,outfile,max);
                  you will need to substitute your appropriate values for infile, outfile, and max depending on how you've defined them.

                  if(pws.CheckPW( uid,upw) == 1) -or whatever is appropriate
                  again you'll need to fill in the appropriate values for uid and upw depending on how you defined them. You'll need to put it into some kind of conditional statement to check if the value is 1 or not.

                  Comment

                  • Banfa
                    Recognized Expert Expert
                    • Feb 2006
                    • 9067

                    #10
                    randysimes, the basic problem is that you have declared the class PWServer but you never instantiated an object of that class.

                    It is important to understand the difference between a class definition and an instantiation because you can not call class methods via the class name, you have to call them via an object of the class, unless the methods are static class methods.

                    JonathanS has suggested 1 method of instantiating a class using the new operator. If you use this method remember to delete the object when you have finished with it or you will get a memory leak. Also he has his syntax slightly wrong it should be

                    Code:
                        PWServer *pws = new PWServer(infile,outfile,max);
                    
                        /* Code using pws */
                    
                        delete pws;
                    Another method would be to just declare the object on the stack, the compiler will handle deleting the object but it will only exist for the lifetime of the function it is declared in.

                    Code:
                        PWServer pws(infile,outfile,max);
                    
                        /* Code using pws */

                    Comment

                    • JonathanS
                      New Member
                      • Jan 2008
                      • 37

                      #11
                      That was my bad Randy. Thanks for straightening it out Banfa.

                      Comment

                      Working...