C++将枚举传递给对象构造函数
假设我有以下内容:
Foo::Foo() {
value = 25; //default constructor...
}
Foo::Foo(Enum bar) {
value = (int)bar; //purpose is to allow an integer to take enum constant's integer value.
}
来自...
enum Enum
{
A = 25,
B = 50,
}
class Foo
{
public:
Foo();
Foo(Enum bar);
private:
int value;
}
然而,如果我执行以下操作:
Enum bar = A; //A = 25
Foo * foo = new Foo(A); //error: "undefined reference to Foo::Foo(Enum)"
这是在 Eclipse CDT 3.6 中。为什么会发生这种情况?我能做些什么来解决这个问题吗?
Say I have the following:
Foo::Foo() {
value = 25; //default constructor...
}
Foo::Foo(Enum bar) {
value = (int)bar; //purpose is to allow an integer to take enum constant's integer value.
}
from...
enum Enum
{
A = 25,
B = 50,
}
class Foo
{
public:
Foo();
Foo(Enum bar);
private:
int value;
}
Yet, if I do the following:
Enum bar = A; //A = 25
Foo * foo = new Foo(A); //error: "undefined reference to Foo::Foo(Enum)"
This is in Eclipse CDT 3.6. Why is this happening? Is there anything I can do about this to solve the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
修复了一些语法错误后(
Enum
定义中额外的,
,Enum
和Foo 之后缺少
定义),您的代码可以在;
gcc
中运行。检查此处:http://www.ideone.com/GZdNM我不太明白你在说什么正在尝试使用
enum
,但可以肯定的是,调用它Enum
可能不是一个好主意......After fixing a few syntax errors (extra
,
inEnum
definition, missing;
afterEnum
andFoo
definitions), your code was run-able ingcc
. Check here: http://www.ideone.com/GZdNMI don't really understand what you're trying to do with the
enum
, but for sure, calling itEnum
is probably not a great idea...听起来您好像忘记将 foo.C 链接到最终应用程序中。
It sounds like you forgot to link
foo.C
into your final application.