Connecting Tech Pros Worldwide Help | Site Map

Cannot call member function without object

Newbie
 
Join Date: Oct 2009
Posts: 17
#1: Oct 5 '09
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::CheckPW(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::CheckPW (uid, pwd)
Familiar Sight
 
Join Date: Jan 2007
Posts: 188
#2: Oct 5 '09

re: Cannot call member function without object


Can`t see anything wrong with that snippet ---probably need to post more code.
Newbie
 
Join Date: Oct 2009
Posts: 17
#3: Oct 5 '09

re: Cannot call member function without object


#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::CheckPW(user, password);
Familiar Sight
 
Join Date: Jan 2007
Posts: 188
#4: Oct 5 '09

re: Cannot call member function without object


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.?
Newbie
 
Join Date: Oct 2009
Posts: 17
#5: Oct 5 '09

re: Cannot call member function without object


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
Newbie
 
Join Date: Oct 2009
Posts: 17
#6: Oct 5 '09

re: Cannot call member function without object


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::ostream& out1 = std::cout);
// public during white-box testing, then privatized
Member
 
Join Date: Jan 2008
Posts: 38
#7: Oct 5 '09

re: Cannot call member function without object


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...
Newbie
 
Join Date: Oct 2009
Posts: 17
#8: Oct 5 '09

re: Cannot call member function without object


How would I do that? This is my first progamming class with C++
Member
 
Join Date: Jan 2008
Posts: 38
#9: Oct 5 '09

re: Cannot call member function without object


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.
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,148
#10: Oct 5 '09

re: Cannot call member function without object


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

Expand|Select|Wrap|Line Numbers
  1.     PWServer *pws = new PWServer(infile,outfile,max);
  2.  
  3.     /* Code using pws */
  4.  
  5.     delete pws;
  6.  
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.

Expand|Select|Wrap|Line Numbers
  1.     PWServer pws(infile,outfile,max);
  2.  
  3.     /* Code using pws */
  4.  
Member
 
Join Date: Jan 2008
Posts: 38
#11: Oct 5 '09

re: Cannot call member function without object


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