C++初学者的编码错误:“未声明的标识符”?
我将在项目的一小部分中使用 C++。我一定是编码有问题,但我对 C++ 的了解就是这样,我无法解决这个问题...
请参阅下面的 AbstractContactListener.h 和 .mm 文件。问题出在 isFixtureCollidingWithFixtureOfType(...) 方法中,我无法访问 _contact 向量。我在这里可能做错了什么?
标题:
struct JRContact {
b2Fixture *fixtureA;
b2Fixture *fixtureB;
bool operator==(const JRContact& other) const
{
return (fixtureA == other.fixtureA) && (fixtureB == other.fixtureB);
}
};
class AbstractContactListener : public b2ContactListener {
id contactHandler;
public:
std::vector<JRContact>_contacts;
AbstractContactListener(id handler);
~AbstractContactListener();
void isFixtureCollidingWithFixtureOfType(b2Fixture fix, int type);
virtual void BeginContact(b2Contact* contact);
virtual void EndContact(b2Contact* contact);
};
实施:
AbstractContactListener::AbstractContactListener(id handler) : _contacts() {
contactHandler = handler;
}
AbstractContactListener::~AbstractContactListener() {
}
void isFixtureCollidingWithFixtureOfType(b2Fixture fix, int type){
std::vector<JRContact>::iterator ct;
// Next line is faulty... can't call _contacts.begin()
// xCode says: "Use of undeclared identifier _contacts"
ct = _contacts.begin();
}
void AbstractContactListener::BeginContact(b2Contact* contact) {
// ...
}
void AbstractContactListener::EndContact(b2Contact* contact) {
// ...
}
未声明?唔。我以为我是在标题中声明它,就在“public:”关键字之后。
我在这里可能做错了什么? 多谢! J。
I'm to use C++ for a very small part of my project. I must be coding something wrong, but my knowledge of C++ is what it is and I can't get around this...
See both the AbstractContactListener.h and .mm files below. The problem is in isFixtureCollidingWithFixtureOfType(...) method, I can't access the _contact vector. What could I be doing wrong here?
header:
struct JRContact {
b2Fixture *fixtureA;
b2Fixture *fixtureB;
bool operator==(const JRContact& other) const
{
return (fixtureA == other.fixtureA) && (fixtureB == other.fixtureB);
}
};
class AbstractContactListener : public b2ContactListener {
id contactHandler;
public:
std::vector<JRContact>_contacts;
AbstractContactListener(id handler);
~AbstractContactListener();
void isFixtureCollidingWithFixtureOfType(b2Fixture fix, int type);
virtual void BeginContact(b2Contact* contact);
virtual void EndContact(b2Contact* contact);
};
Implementation:
AbstractContactListener::AbstractContactListener(id handler) : _contacts() {
contactHandler = handler;
}
AbstractContactListener::~AbstractContactListener() {
}
void isFixtureCollidingWithFixtureOfType(b2Fixture fix, int type){
std::vector<JRContact>::iterator ct;
// Next line is faulty... can't call _contacts.begin()
// xCode says: "Use of undeclared identifier _contacts"
ct = _contacts.begin();
}
void AbstractContactListener::BeginContact(b2Contact* contact) {
// ...
}
void AbstractContactListener::EndContact(b2Contact* contact) {
// ...
}
Undeclared? Hmm. I thought I was declaring it in the header, right after the "public:" keyword.
What could I be doing wrong here?
thanks a lot!
J.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您忘记添加函数的范围。尝试:
为什么错误将您指向那个奇怪的地方?编译器看到您的函数定义并认为这是一个自由函数,因为没有任何其他指示并尝试按原样处理它。它失败了,因为它尝试在全局范围内查找变量。这可能会变得更有趣(阅读:更令人困惑):该函数不使用类成员的图像。它将被简单地解析并编译为自由函数。一旦您尝试在该类型的对象上调用它,您就会收到链接器错误。
另外,我看不到
AbstractContactListener
中使用的id
类型的声明,但这可能只是因为代码示例不完整。You forget to add the scope of the function. Try:
Why is the error pointing you to that strange place? The compiler sees your function definition and thinks that this is a free function, as there is nothing that indicates otherwise and tries to handle it as such. It fails, because it tries to find the variable in the global scope. This can get even funnier (read: more confusing): Image that this function does not use a class member. It will be simply parsed and compiled as a free function. As soon as your try to call it on an object of that type you will get a linker error.
Also, I cannot see a declaration of the type
id
which is used inAbstractContactListener
but that might just be because the code sample is incomplete.您忘记了班级名称
You forgot the class name from
void AbstractContactListener::isFixtureCollidingWithFixtureOfType(b2Fixture fix, int type)
实现中。
:)
void AbstractContactListener::isFixtureCollidingWithFixtureOfType(b2Fixture fix, int type)
In the implementation.
:)