简体   繁体   English

C ++调用虚拟函数时遇到麻烦

[英]c++ trouble calling a virtual function

OK, so new to polymorphism and virtual functions, and I'm having issues even calling my first function. 好的,对多态性和虚函数来说还很陌生,即使调用第一个函数也遇到问题。 I believe the issue is in the call itself, because this returns the error; 我认为问题出在电话本身,因为这会返回错误;

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

Which I'm pretty sure means I'm passing the wrong thing to the function. 我很确定,这意味着我将错误的东西传递给了函数。 I know that I can not just pass the name of the instance of a class for virtual functions, that it must be by ref or as a pointer. 我知道我不能只为虚拟函数传递类实例的名称,它必须通过ref或作为指针。 I have tried both methods and get the same error. 我尝试了两种方法,并得到相同的错误。

I am omitting copy and pasting the entire thing, as it is a banking project and there is a ton if unrelated stuff for input validation. 我省略了复制并粘贴了整个内容,因为这是一个银行项目,并且有大量(如果不相关)用于输入验证的内容。 The parent class is 'Account' and 'Checking' is the child derived from it. 父类是“帐户”,“支票”是从其派生的子类。 'checkingObject' is the name of the instance of the class 'Checking' that I am trying to call on. “ checkingObject”是我尝试调用的“ Checking”类实例的名称。

Function call in main .cpp: 主.cpp中的函数调用:

Checking::Balance(&checkingObject);

Declared in Account.h (Parent class): 在Account.h中声明(父类):

virtual void Balance();

Re-Declared in Checking.h (child class): 在Checking.h(子类)中重新声明:

void Balance(Checking &);

Defined in Checking.cpp (balance is a private member of the class): 在Checking.cpp中定义(balance是该类的私有成员):

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

}

I must be missing something, but I'm just trying to pass the the object checkingObject as a reference to the function. 我必须缺少一些东西,但是我只是想将对象checkingObject传递给该函数。 I really don't understand the part of the error message "cannot convert parameter 1 from 'Checking (__cdecl *)(void)' ". 我真的不理解错误消息的一部分“无法从'Checking(__cdecl *)(void)'转换参数1”。 My understanding of ClassName & is that it would take any object derived from the class. 我对ClassName&的理解是,它将接受从该类派生的任何对象。

EDIT: Including more code for clarity. 编辑:为了清晰起见,包括更多代码。

In banking.cpp (Main file) 在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);
}

Account header 帐户标题

#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  

Checking Header 检查标题

#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

There are few issues, first this is not overriding the virtual method: 有几个问题,首先这不是覆盖虚拟方法:

void Balance(Checking &);

This is defining a new method that will hide the parent method, derived virtual methods need to have the same signature. 这是定义一个新方法,它将隐藏父方法,派生的虚拟方法需要具有相同的签名。 Next this call: 接下来的电话:

Checking::Balance(&checkingObject);

is odd in a few ways, it should be: 在某些方面很奇怪,应该是:

someInstanceOfChecking.Balance(checkingObject);

This &checkingObject will be a pointer to a checkingObject . &checkingObject将是指向checkingObject的指针。 When a method takes a reference it does the work behind the scenes you do not have to do anything but pass an instance of the object you are taking a reference to. 当方法引用时,它在幕后进行工作,您无需执行任何操作,只需传递引用对象的实例即可。

Update: 更新:

Based on your newly posted code this seems like a reasonable implementation of Checking::Balance : 根据您新发布的代码,这似乎是Checking::Balance的合理实现:

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

Although it is not clear to me what Account::Balance does. 尽管我不清楚Account::Balance作用。 Also, I think I see your larger issue, your globals are not defined properly, this is what you want: 另外,我想我看到了更大的问题,您的全局变量定义不正确,这就是您想要的:

Account accountObject;
Checking checkingObject;
Savings savingsObject;

What you have in your code is declaration of functions, for example, this: 您的代码中包含的是函数的声明,例如:

Checking checkingObject();

declares a function called checkingObject that returns a Checking object. 声明一个称为checkingObject的函数,该函数返回一个Checking对象。

Without seeing more code, it's hard to be completely certain; 没有看到更多的代码,很难完全确定。 but I believe that instead of this: 但我相信,除了这个:

Checking::Balance(&checkingObject);

what you want is this: 您想要的是:

checkingObject.Balance();

and instead of this: 而不是这样:

void Balance(Checking &)

this: 这个:

void Balance()

OK, first, your call is taking a pointer to checkingObject (that is, a Checking * ). 好的,首先,您的调用使用了一个指向checkingObject的指针(即Checking * )。 That's not what you want, instead just pass checkingObject , instead of &checkingObject . 那不是您想要的,而是只传递checkingObject ,而不是&checkingObject

Second, you aren't calling a virtual function. 其次,您没有调用虚函数。 To properly overload a virtual function, the child class function must have the same signature (all parameters and return values must be the same) as in the parent. 为了适当地重载虚拟函数,子类函数必须具有与父类相同的签名(所有参数和返回值必须相同)。 What you are doing is creating a new function with the same name and a different signature. 您正在做的是创建一个具有相同名称和不同签名的新功能。 Now since you call it using the class name, you'll get the one you want, but if you were to take an Account object and try to call Balance(checkingObject) on it, it would fail. 现在,由于使用类名调用了该类,因此您将获得所需的类,但是如果要使用一个Account对象并尝试在其上调用Balance(checkingObject) ,它将失败。

There are a number of issues here. 这里有很多问题。 Firstly: 首先:

void Balance(Checking &);

does not override the virtual void Balance(); 不重写virtual void Balance(); method you have declared in Account . 您在Account声明的方法。 The method signature should be the same. 方法签名应该相同。 Eg in your Checking class, it should look like: 例如,在您的Checking类中,它应如下所示:

void Balance();

The Checking.cpp then should read: 然后Checking.cpp应该显示为:

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

The call in main should then look like: 那么main中的调用应如下所示:

Checking account;    // Creates the checking account object
account.Balance();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM