虚拟抽象函数 [英] virtual abstract functions
问题描述
嗨
如果有以下课程
A级
{
public:
A();
virtual~A();
虚拟字符串someFunction()const = 0;
}
B级:公共A
{}
int main(void )
{
A * aa =新B();
}
我得到这个编译错误
main.cpp:在函数`int main(int,char **)'':
main.cpp:10:错误:无法分配类型为'A'的对象
main.cpp:10:错误:因为以下虚函数是
摘要:
Bh:30:错误:虚拟std :: string B :: someFunction()const
有没有人知道这是什么,因为如果我删除
$ b中的''const'' $ b它运行的Base类但我不会将Base类抽象出来!
提前致谢
* placid:
如果有以下课程
A级
公开:
A();
虚拟〜 A();
虚拟字符串someFunction()const = 0;
}
B类:public A
{}
int main(void)
{A / aa = new B();
}
我得到这个编译错误
不,你没有。
如果您尝试编译以上内容,您会收到一些其他错误消息。
这里''一个你实际得到的例子,使用g ++ 3.4.2:
koko.cpp:6:错误:`string''没有命名类型
koko.cpp:6:错误:额外的分号
koko.cpp:13:错误:新类型可能无法在返回类型中定义
koko.cpp: 13:错误:声明'main'的两个或多个数据类型'
koko.cpp:13:错误:无关的`int''忽略
koko.cpp: 13:错误:`main''必须返回`int''
koko.cpp:13:错误:返回'main'的类型改为'int''
koko.cpp:函数`int main(...)'':
koko.cpp:14 :警告:未使用的变量''aa''
这是另一个例子,当''#include< string>''和''使用命名空间std;''
添加在顶部:
koko.cpp:16:错误:新类型可能未在返回类型中定义
koko.cpp:16:错误:声明'main'的两个或多个数据类型'
koko.cpp:16:错误:无关'int''忽略
koko.cpp:16:错误:`main''必须返回`int''
koko.cpp:16:错误:返回类型为`main''改为`int''
koko.cpp:在函数`int main(...)'':
koko.cpp:17:错误:无法分配类型为'B''的对象
koko.cpp:17:错误:因为以下虚函数是抽象的:
koko.cpp:9:错误:virtual std :: string A :: someFunction() const
ko ko.cpp:17:警告:未使用的变量''aa''
现在为上面的代码你_claim_的错误信息,但是
实际上对于你没有显示的代码:
main.cpp:在函数`int main(int,char **)'':
main.cpp: 10:错误:无法分配类型为'A'的对象
main.cpp:10:错误:因为以下虚函数是抽象的:
Bh:30:错误:虚拟标准:: string B :: someFunction()const
研究错误信息。
它说什么?
这是你的实际代码中的一个问题。
有谁知道这是什么,因为如果我删除''const' '
基础课它的工作原理
不,它没有。
但我不会将Base类抽象出来!
为什么?
-
答:因为它弄乱了订单人们通常是r e文本。
问:为什么这么糟糕?
A:热门发布。
问:什么是最烦人的usenet和电子邮件中的东西?
placid写道:嗨
A级
{
公开:
A();
虚拟~A();
虚拟字符串someFunction()const = 0;
}
类B:public A
{}
int main(void)
{
A * aa = new B();
}
我得到这个编译错误
main.cpp:在函数`int main (int,char **)'':
main.cpp:10:错误:无法分配类型为'A'的对象
main.cpp:10:错误:因为以下虚函数是
抽象:
Bh:30:错误:虚拟std :: string B :: someFunction()const
有谁知道这是什么,因为如果我删除'' const''在基类中它可以工作但是我不要将Base类抽象化!
提前感谢
B类必须实现someFunction才能使类可实例化:
B级:公共A
{
public:
virtual std :: string someFunction()const
{
返回"" ;;
}
};
另请注意分配B对象的正确语法:
int main()
{
A * aa =新B;
...
}
Greg
"格雷格" < GR **** @ pacbell.net>在消息中写道
news:11 ********************* @ g44g2000cwa.googlegro ups.complacid写道:
int main(void)
{A / aa = new B();
}
还要注意分配B对象的正确语法:
int main()
{A / aa = new B;
.. 。
}
A * aa =新B();
也是正确的。见标准第5.3.4 / 15节。使用()表示
对象的值已初始化。
-
John Carson
Hi
if a have the following classes
class A
{
public:
A();
virtual ~A();
virtual string someFunction() const = 0;
}
class B:public A
{}
int main(void)
{
A* aa = new B();
}
I get this compiler error
main.cpp: In function `int main(int, char**)'':
main.cpp:10: error: cannot allocate an object of type `A''
main.cpp:10: error: because the following virtual functions are
abstract:
B.h:30: error: virtual std::string B::someFunction() const
does anyone know whats this abou, because if i remove the ''const'' in
the Base class it works but i wont the Base class to be abstract !
Thanks in advance
* placid:
if a have the following classes
class A
{
public:
A();
virtual ~A();
virtual string someFunction() const = 0;
}
class B:public A
{}
int main(void)
{
A* aa = new B();
}
I get this compiler error
No you don''t.
If you try to compile the above you get some other error messages.
Here''s an example of what you actually get, using g++ 3.4.2:
koko.cpp:6: error: `string'' does not name a type
koko.cpp:6: error: extra semicolon
koko.cpp:13: error: new types may not be defined in a return type
koko.cpp:13: error: two or more data types in declaration of `main''
koko.cpp:13: error: extraneous `int'' ignored
koko.cpp:13: error: `main'' must return `int''
koko.cpp:13: error: return type for `main'' changed to `int''
koko.cpp: In function `int main(...)'':
koko.cpp:14: warning: unused variable ''aa''
Here''s another example, when ''#include <string>'' and ''using namespace std;''
are added on the top:
koko.cpp:16: error: new types may not be defined in a return type
koko.cpp:16: error: two or more data types in declaration of `main''
koko.cpp:16: error: extraneous `int'' ignored
koko.cpp:16: error: `main'' must return `int''
koko.cpp:16: error: return type for `main'' changed to `int''
koko.cpp: In function `int main(...)'':
koko.cpp:17: error: cannot allocate an object of type `B''
koko.cpp:17: error: because the following virtual functions are abstract:
koko.cpp:9: error: virtual std::string A::someFunction() const
koko.cpp:17: warning: unused variable ''aa''
Now for the error message you _claim_ for the above code, but which is
actually for code that you haven''t shown:
main.cpp: In function `int main(int, char**)'':
main.cpp:10: error: cannot allocate an object of type `A''
main.cpp:10: error: because the following virtual functions are
abstract:
B.h:30: error: virtual std::string B::someFunction() const
Study the error message.
What does it say?
That''s one thing that''s wrong in your actual code.
does anyone know whats this abou, because if i remove the ''const'' in
the Base class it works
No it does not.
but i wont the Base class to be abstract !
Why?
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
placid wrote:Hi
if a have the following classes
class A
{
public:
A();
virtual ~A();
virtual string someFunction() const = 0;
}
class B:public A
{}
int main(void)
{
A* aa = new B();
}
I get this compiler error
main.cpp: In function `int main(int, char**)'':
main.cpp:10: error: cannot allocate an object of type `A''
main.cpp:10: error: because the following virtual functions are
abstract:
B.h:30: error: virtual std::string B::someFunction() const
does anyone know whats this abou, because if i remove the ''const'' in
the Base class it works but i wont the Base class to be abstract !
Thanks in advance
Class B must implement someFunction for the class to be instantiable:
class B : public A
{
public:
virtual std::string someFunction() const
{
return "";
}
};
Note also the proper syntax to allocate a B object:
int main()
{
A* aa = new B;
...
}
Greg
"Greg" <gr****@pacbell.net> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.complacid wrote:
int main(void)
{
A* aa = new B();
}
Note also the proper syntax to allocate a B object:
int main()
{
A* aa = new B;
...
}
A* aa = new B();
is also correct. See section 5.3.4/15 of the standard. The use of () means
that the object is value initialized.
--
John Carson
这篇关于虚拟抽象函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!