470,594 Members | 1,345 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,594 developers. It's quick & easy.

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::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)
Oct 5 '09 #1
10 14813
542 512MB
Can`t see anything wrong with that snippet ---probably need to post more code.
Oct 5 '09 #2
#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;

cout << "Enter choice: ";
cin >> choice;
switch (choice)
case '1':
cout << "User ID: ";
cin >> user;
cout << "Password: ";
cin >> password;
PWServer::CheckPW(user, password);
Oct 5 '09 #3
542 512MB
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.?
Oct 5 '09 #4
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
Oct 5 '09 #5
Here is the code for PWServer:
class PWServer
// 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
Oct 5 '09 #6
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...
Oct 5 '09 #7
How would I do that? This is my first progamming class with C++
Oct 5 '09 #8
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.
Oct 5 '09 #9
9,065 Expert Mod 8TB
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);
  3.     /* Code using pws */
  5.     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.

Expand|Select|Wrap|Line Numbers
  1.     PWServer pws(infile,outfile,max);
  3.     /* Code using pws */
Oct 5 '09 #10
That was my bad Randy. Thanks for straightening it out Banfa.
Oct 5 '09 #11

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by christopher vogt | last post: by
27 posts views Thread by Jason Shohet | last post: by
26 posts views Thread by Martin JÝrgensen | last post: by
5 posts views Thread by Frederick Gotham | last post: by
46 posts views Thread by Steven T. Hatton | last post: by
5 posts views Thread by mohammaditraders | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.