【问题标题】:Object pointer parameter cannot use accessor method from the same class as the object对象指针参数不能使用与对象相同类的访问器方法
【发布时间】:2012-02-02 16:58:57
【问题描述】:

所以我有这个:

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);
}

所以不知何故,我无法获取参数指针以使用访问器方法(参见 (1) 标记的行)。有人可以给我一个快速修复吗?我搜索了一种方法来做到这一点,但似乎没有人拥有参数是指向对象的指针的访问器。

【问题讨论】:

  • 我可以告诉您来自具有不同 new 语法的语言。也许是java? :)

标签: c++ syntax-error


【解决方案1】:

这里有很多问题。让我尝试为您解决其中的一些问题。

您声明:

     unsigned int _uid; //user identifier

但你的吸气剂是:

    int getUserID();

这有截断的风险。应该是:

    unsigned int getUserID() const;

const,因为您没有修改 class 的实例。

这将被定义:

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

接下来,幻象class的这个方法:

void UserDB::adduser(A* newUser)

你说:

我搜索了一种方法来做到这一点,但似乎没有人拥有参数是指向对象的指针的访问器。

那是因为在这样的class 中不需要这样做。您应该更喜欢在此处通过引用传递。 const-reference 会更好,但我们不要改变太多...

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

那里没有语法错误!对于指针,首先您需要检查它不是NULL,然后使用-> 运算符而不是.。参考文献更清晰。

然后,到...

int main(){

    A tempObj;

这会在堆栈上构造一个A 的实例,调用默认构造函数。你不需要这个,所以只需删除它。

    UserDB B;

这会在堆栈上构造一个B,再次调用默认构造函数。我打算不管它。

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

好的。就这一次。

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

你不能这样做! tempObjA,而不是 A*。相反,只需在调用第二个构造函数的堆栈上创建一个A

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

现在开始...

    B = new UserDB();

同样,你不能这样做。 BUserDB,而不是 UserDB*。删除这一行就行了,因为你已经在堆栈上创建了,所以不需要它!

这条线几乎是对的……

    B.addUser(tempObj);

...但是 C++ 区分大小写!使用:

    B.adduser(tempObj);

你的代码应该可以编译了。

read a book or two的时间到了!

【讨论】:

  • 谢谢,前几天我已经修复了所有这些错误,不过还是谢谢你,你的回答很详细。
【解决方案2】:

if(newUser.getUserID == 0) 更改为if(newUser->getUserID() == 0)
B.addUser(tempObj);B->addUser(&tempObj);

您还需要将 tempObjB 对象声明为指针:

UserDB* B
A* tempObj;

当然,如果您不需要使用指针,则不建议使用指针,因为您必须处理内存管理问题。这是没有指针的方法:

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);
}

为了提高性能,最好传递newUser参数by reference,这样在传递给addUser方法时就不会被复制。并将其设为 const 以确保其值不会在 addUser 方法中意外修改:

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

【讨论】:

  • 谢谢,它不再给我语法错误了。我希望它有效。
  • 有没有办法让B 只保留UserDB 类型?
【解决方案3】:

我猜tempObj应该是这里的指针:A tempObj = new A();,所以它应该是这样的:

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

if(newUser.getUserID == 0) 应该是if(newUser->getUserID() == 0),因为您正在处理指针,而getUserID 是一种方法,而不是成员变量。

希望这会有所帮助。

【讨论】:

  • 它告诉我我不能删除 B。“表达式必须有指针或句柄类型”
  • 您是否将B 声明为指向UserDB 的指针?它应该是这样的:UserDB* B = new UserDB(); /* some code */ delete B;
  • 我不打算让它成为一个指针。给它分配new 是一个错误。
  • 我认为delete A 你的意思是delete tempObj,但是当我写到我收到expression: _CrtIsValidHeapPointer(pUserData) 的错误
  • 是的,谢谢这是错字。对于这个堆损坏:确保你不要尝试两次删除同一个对象。
【解决方案4】:

newUser 是一个指针,因此您需要(*newUser).newUser-> 才能访问它所指向的内容。

要调用函数,您需要使用函数调用运算符()

然后你得到

newUser->getUserID()

【讨论】:

  • 谢谢,我试过(*newUser).,但没用。但是第二个做到了。
猜你喜欢
  • 2021-04-25
  • 1970-01-01
  • 1970-01-01
  • 2023-02-09
  • 1970-01-01
  • 2023-03-07
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多