Object pointer parameter cannot use accessor method from the same class as the object

StackOverflow https://stackoverflow.com/questions/9116480

  •  21-04-2021
  •  | 
  •  

Question

So I have this:

class A
{
private:
    unsigned int _uid; //user identifier
public:
    A();
    A(const char *, const char *, int, int, const char *, const char *, const char *);
    virtual ~A();
    int getUserID();
};

int A::getUserID(){
    return _uid;
}

//The class UserDB is not that important as this method is at this moment.
void UserDB::adduser(A* newUser){
    if(newUser.getUserID == 0) //Not working, not sure why... Gives syntax error. (1)
    //Something is done if the condition is true;
};

int main(){
    UserDB B
    A tempObj;
    //Assume the 7 following variables have been initialized correctly.
    char uloginName[9];
    char homeDirectory[33];
    int userID;
    int groupID;
    char password[17];
    char shell[17];
    char gecos[65];
    tempObj = new A(uloginName, password, userID, groupID, gecos, homeDirectory, shell);
    B = new UserDB();
    B.addUser(tempObj);
}

And so somehow I cannot get the parameter pointer to use the accessor method (refer at line marked by (1)). Can someone give me a quick fix? I searched a for a way to do it but no one seems to have accessors with parameters being pointers to an object.

No correct solution

OTHER TIPS

You've a number of issues here. Let me try to address some of them for you.

You declare:

     unsigned int _uid; //user identifier

But your getter for this is:

    int getUserID();

This risks truncation. It should be:

    unsigned int getUserID() const;

const because you're not modifying the instance of the class.

This would be defined:

unsigned int A::getUserID() const
{
    return _uid;
}

Next up, this method of the phantom class:

void UserDB::adduser(A* newUser)

You said:

I searched a for a way to do it but no one seems to have accessors with parameters being pointers to an object.

That is because there's no need for this in such a class. You should prefer to pass by reference here. const-reference would be better, but let's not change too much...

void UserDB::adduser(A& newUser)
{
    if (newUser.getUserID() == 0)
    {
        //Something is done if the condition is true;
    }
};

No syntax error there! For a pointer, first you'd need to check it's not NULL and then use the -> operator rather then the .. References are much cleaner.

Onwards, then, to...

int main(){

    A tempObj;

This constructs an instance of A on the stack, calling the default constructor. You don't need this, so just remove it.

    UserDB B;

This constructs a B on the stack, again calling the default constructor. I'm going to leave it alone.

    //Assume the 7 following variables have been initialized correctly.

Okay. Just this once.

    tempObj = new A(uloginName, password, userID, groupID, gecos, homeDirectory, shell);

You can't do that! tempObj was an A, not an A*. Instead just create an A on the stack calling the second constructor:

    A tempObj(uloginName, password, userID, groupID, gecos, homeDirectory, shell);

Now onto...

    B = new UserDB();

Again, you can't do this. B is a UserDB, not a UserDB*. Just delete this line, it's not needed as you've already created one on the stack!

This line is almost right...

    B.addUser(tempObj);

...but C++ is case-sensitive! Use:

    B.adduser(tempObj);

And your code should compile.

Time to read a book or two!

Change if(newUser.getUserID == 0) to if(newUser->getUserID() == 0)
and B.addUser(tempObj); to B->addUser(&tempObj);

You would also need to declare tempObj and B objects as pointer:

UserDB* B
A* tempObj;

Of course it's not recommended to use pointers if you don't need to because of memory management issues you have to deal with. This is the way to go without pointers:

void UserDB::addUser(A newUser)
{
    if(newUser.getUserID() == 0)
    {
        //Something is done if the condition is true;
    } 
};

int main(){    
    //Assume the 7 following variables have been initialized correctly.
    char uloginName[9];
    char homeDirectory[33];
    int userID;
    int groupID;
    char password[17];
    char shell[17];
    char gecos[65];
    UserDB B;
    A tempObj(uloginName, password, userID, groupID, gecos, homeDirectory, shell);
    B.addUser(tempObj);
}

To improve performance, it's better to pass newUser parameter by reference so that it doesn't get copied when passed to addUser method. And make it const to make sure that its value doesn't accidentally get modified inside the addUser method:

void UserDB::addUser(const A &newUser)
{
    if(newUser.getUserID() == 0)
    {
        //Something is done if the condition is true;
    } 
};

I guess that tempObj is supposed to be pointer here: A tempObj = new A();, so it should look like this:

int main()
{
    // ...
    A* tempObj = new A(uloginName, password, userID, groupID, gecos, homeDirectory, shell);
    UserDB B;
    B.addUser(tempObj);
    // ...
    delete tempObj;
    return 0;
}

And if(newUser.getUserID == 0) is supposed to be if(newUser->getUserID() == 0) since you are dealing with pointer and getUserID is a method, not a member variable.

Hope this helps.

newUser is a pointer, so you need either (*newUser). or newUser-> to access what it points to.

To call a function you need to use the function call operator ().

Then you get

newUser->getUserID()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top