c++调用虚函数的问题

标签 c++

好吧,多态性和虚函数太新了,我什至在调用我的第一个函数时都遇到了问题。我认为问题出在调用本身,因为这会返回错误;

banking.cpp(289): error C2664: 'Checking::Balance' : cannot convert parameter 1 from 'Checking (__cdecl *)(void)' to 'Checking &'

我很确定这意味着我向函数传递了错误的东西。我知道我不能只为虚函数传递类实例的名称,它必须通过 ref 或作为指针传递。我已经尝试了这两种方法并得到了相同的错误。

我省略了复制和粘贴整个内容,因为它是一个银行项目,并且有大量不相关的内容用于输入验证。父类是“Account”,“Checking”是从它派生的子类。 “checkingObject”是我尝试调用的“Checking”类实例的名称。

main.cpp 中的函数调用:

Checking::Balance(&checkingObject);

在Account.h(父类)中声明:

virtual void Balance();

在Checking.h(子类)中重新声明:

void Balance(Checking &);

在Checking.cpp中定义(balance是类的私有(private)成员):

void Checking::Balance(Checking &)
{
cout << balance;

}

我一定是遗漏了什么,但我只是想传递对象 checkingObject 作为函数的引用。我真的不明白错误消息“无法从'Checking (__cdecl *)(void)'”转换参数1的部分。我对 ClassName & 的理解是它会接受从该类派生的任何对象。

编辑:为了清晰起见,包括更多代码。

在 banking.cpp(主文件)

Account accountObject();
    Checking checkingObject();
    Savings savingsObject();
int main()
{       
regScreen();
servicesScreen();   

return 0;
}

void checkingScreen()
{
system("cls");
int choice;

do
{
    string countString;     
    centerString("$$$$ Checking $$$$");
    cout << endl;       
    centerString("Account  Activites");
    cout << endl;       
    centerString("==================");
    cout << endl;       
    centerString("1) ----Deposit----");
    cout << endl;
    centerString("2) ----Withdraw---");
    cout << endl;
    centerString("3) ----Transfer---");
    cout << endl;
    centerString("4) ----Balance----");
    cout << endl;
    centerString("5) Personal  Check");
    cout << endl;
    centerString("6) ----Interest---");
    cout << endl;
    centerString("7) ---Statement---");
    cout << endl;
    centerString("8) -----Exit------");
    cout << endl;
    centerString("Enter selection: ");

    // Validate user input for menu choice
    while(!(cin >> choice) || (choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 5 && choice != 6 && choice != 7 && choice != 8))
    {
        cout << "Error: Please re-enter a correct choice: ";
        cin.clear();
        fflush(stdin);
    }

    //Switch for user choices
    switch(choice)
    {
    case 1:
        {
            string userInput;
            double dollars;
            centerString("*** Deposit ***");
            cout << endl;
            centerString("Amount to deposit: ");
            cin >> userInput;
            dollars = validCurrency(userInput);             
        }
    case 2:
        {

        }
    case 3:
        {

        }
    case 4:
        {
            checkingObject.Balance();               
        }
    }
} while (choice != 8);
}

帐户标题

#ifndef ACCOUNT_H
#define ACCOUNT_H

#include<string>
using namespace std;

class Account
{
private:


string nameString;
    string titleString;
    string SSNString;
    string IDString;
public:
    //Constructor
    Account();

    // Mutators
    void setName(string), setTitle(string), setSSN(string), setID(string);
    virtual void Deposit(double);
    virtual void Withdraw(double);
    virtual void Transfer(string, double);
    virtual void Balance();
    virtual void Check();
    virtual void Interest();

    // Accessors
    virtual void Statement() const;
};
#endif  

检查标题

#ifndef CHECKING_H
#define CHECKING_H
#include"Account.h"

class Checking : public Account
{
private:
    double balance;
    double rate;

public:
    Checking(); // Default Constructor
    Checking(double, double);
    void Deposit(double);   
    void Balance();

};

#endif

最佳答案

有几个问题,首先这不是覆盖虚拟方法:

void Balance(Checking &);

这是定义一个新的方法,将隐藏父方法,派生的虚方法需要有相同的签名。接下来这个电话:

Checking::Balance(&checkingObject);

在某些方面很奇怪,应该是:

someInstanceOfChecking.Balance(checkingObject);

&checkingObject 将是指向checkingObject 的指针。当方法采用引用时,它会在幕后完成工作,您无需执行任何操作,只需传递您正在引用的对象的实例即可。

更新:

根据您新发布的代码,这似乎是 Checking::Balance 的合理实现:

void Checking::Balance()
{
    std::cout << balance;
}

虽然我不清楚Account::Balance 是做什么的。另外,我想我看到了你更大的问题,你的全局定义不正确,这就是你想要的:

Account accountObject;
Checking checkingObject;
Savings savingsObject;

您的代码中包含的是函数声明,例如:

Checking checkingObject();

声明一个名为 checkingObject 的函数,它返回一个 Checking 对象。

关于c++调用虚函数的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16619843/

相关文章:

c++ - C++ 栈中的数组

C++帮助。我在做这件事时遇到了问题

c++ - 如何将音频字节转换为样本

javascript - 使用 Emscripten 从 JavaScript 异步调用 C++ 函数

c++ - 如何使用 AllocaInst 创建 LLVM 数组类型?

c++ - Valgrind Massif工具部队快照

c++ - 为什么 unique-ptr 不检查基类是否可虚拟破坏?

c++ - 如何复制特征矩阵

c++ - 如何在 while(1) 内部工作时安全退出 C++ 控制台应用程序

c++ - 如何在使用 OpenCV 遍历像素时添加 2 个 Mat 像素值?