派生类问题 [英] Derived classes problem

查看:98
本文介绍了派生类问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有一个包含多个类的程序,现在我必须从1个基类创建3个派生类。我有基类Account和3个派生类:CurrentAccount,SavingAccount和ISAAccount。类Account连接到Employee类,我在其中声明一个指针

Hello everyone,

I have a program with several classes, and now I have to create 3 derived classes from 1 base class. I have the base class Account and 3 derived classes: CurrentAccount, SavingAccount and ISAAccount. The class Account is connected to the class Employee, where I am declaring a pointer

展开 | 选择 | Wrap | 行号

推荐答案

什么数据类型是账户?它应该是账户** - 是吗?
What data type is theAccounts? It should be Account** - is it?


他在第一篇文章中包含了Ganon,这是一个账户**。


@OP:你的#include看起来没错,假设所有的派生类都在单独的文件中。否则,你会把它包含在内,这是一件坏事。浪费空间,导致一些讨厌的运行时间爆炸等...
He included that in his first post, Ganon, it''s an Account **.

@OP: Your #includes look OK assuming all your derived classes are in separate files. Otherwise, you''re getting it multiply included, which is a bad thing. Waste of space, leads to some nasty runtime blowups, etc...


哦,我发现了问题。 (很抱歉没有仔细阅读你的帖子O_O)。


当你创建对象时,无论有没有新运算符,你都不包括除非您为构造函数提供参数,否则在对象名称之后的括号()。否则,编译器会认为您正在声明一个新函数(无论出于何种原因)。默认情况下,没有指定返回类型的函数返回一个int(我相信),因为你使用new,它使它成为一个int *,然后它尝试分配给一个Account *变量。

基本上,你需要写:

Oh, I caught the problem. (And sorry for not reading your post more carefully O_O).

When you are creating objects, either with or without the ''new'' operator, you don''t include the parentheses () after the object name unless you are giving arguments to the constructor. Otherwise, the compiler thinks you are declaring a new function (for whatever reason). By default, functions without specified return types return an int (I believe), and since you were using new, it made it into an int*, which it then tried to assign to an Account* variable.

Basically, you need to write:

展开 | 选择 | Wrap | 行号


这篇关于派生类问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆