简体   繁体   English

C ++头函数

[英]C++ header functions

Sorry for asking a beginners question. 很抱歉问初学者问题。 I'm quiet familiar with Java, C# ... but I have to write an OpenGL Program in C++ and there is something I don't understand: 我对Java,C#非常熟悉,但是我必须用C ++编写OpenGL程序,但有些我不理解:

I have to parse a text file to get vertices and put them into a vector. 我必须解析一个文本文件以获得顶点并将其放入向量中。 So I wrote a method that receives my vector of Vertex objects. 因此,我写了一个方法来接收我的Vertex对象向量。 It works when I define the method before I call it 当我在调用它之前定义该方法时,它就可以工作

std::vector<AESParser::Vertex> vertices;

void parseVertices(std::vector<AESParser::Vertex> &verts){
 ...
}

...

parseVertices(vertices);

But fails when doing it the other way around. 但是以其他方式执行失败。 So I understand I have to declare it in the header file. 所以我知道我必须在头文件中声明它。 So I wrote something like this: 所以我写了这样的东西:

*** Header ***
class AESParser
{
    public:
        struct Vertex{
            float x;
            float y;
            float z;
        };
        AESParser();
        virtual ~AESParser();
        void read();
    protected:
    private:
        int readNumVertices(std::fstream &stream);
        int readNumFaces(std::fstream &stream);   
        void parseVertices(std::vector<Vertex> &verts);
        Vertex readVertex(std::fstream &stream);  
};

I know there are probably many more mistakes since I never did C++ but the main problem with this is, that I get the following error message: 我知道可能有更多错误,因为我从没做过C ++,但是主要的问题是,我收到以下错误消息:

undefined reference to `AESParser::parseVertices(std::vector<AESParser::Vertex, std::allocator<AESParser::Vertex> >&)'

So something seems to be wrong with the "parseVertices()" method and I don't see what. 因此,“ parseVertices()”方法似乎出现了问题,我看不到什么。 It works for the others like "readNumVertices()". 它适用于其他人,例如“ readNumVertices()”。

Thanks for your help! 谢谢你的帮助!

Your function belongs to the AESParser class, so you need to define it as such 您的函数属于AESParser类,因此您需要这样定义它

void AESParser::parseVertices(std::vector<AESParser::Vertex> &verts){
    ...
}

When you have the function definition written as 当函数定义写为

void parseVertices(std::vector<AESParser::Vertex> &verts)

Then it is a free function (doesn't belong to a class) that happens to have the same name, return the same type, and take the same arguments. 然后它是一个自由函数(不属于类),碰巧具有相同的名称,返回相同的类型并采用相同的参数。 But it is a different function. 但这是一个不同的功能。

Note in the first version, there is a AESParser:: scope appended to the front of the function name. 请注意,在第一个版本中,在函数名称的前面附加了一个AESParser::范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM