C ++初学者的编码错误:&“未声明的标识符"? [英] C++ beginner's coding mistake: "Undeclared identifier"?

查看:60
本文介绍了C ++初学者的编码错误:&“未声明的标识符"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将在项目的一小部分中使用C ++.我一定是在编写错误的代码,但是我对C ++的了解是什么,我无法解决这个问题……

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...

请参见下面的AbstractContactListener.h和.mm文件.问题出在isFixtureCollidingWithFixtureOfType(...)方法中,我无法访问_contact向量.我在这里怎么可能做错了?

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?

标题:

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:"关键字之后声明了它.

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.

推荐答案

您忘记添加函数的范围.试试:

You forget to add the scope of the function. Try:

void AbstractContactListener::isFixtureCollidingWithFixtureOfType(b2Fixture fix, int type){

为什么错误将您指向那个陌生的地方?编译器会看到您的函数定义,并认为这是一个自由函数,因为没有任何指示相反的东西,并试图按原样对其进行处理.它失败,因为它试图在全局范围内查找变量.这甚至可以变得更有趣(请阅读:更加令人困惑):该函数未使用类成员的图像.它将简单地解析并编译为自由函数.一旦尝试在该类型的对象上调用它,您将收到链接器错误.

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.

此外,我看不到在 AbstractContactListener 中使用的 id 类型的声明,但这可能只是因为代码示例不完整.

Also, I cannot see a declaration of the type id which is used in AbstractContactListener but that might just be because the code sample is incomplete.

这篇关于C ++初学者的编码错误:&amp;“未声明的标识符"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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