0

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:

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

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. It works for the others like "readNumVertices()".

Thanks for your help!

Tobias Reich
  • 4,952
  • 3
  • 47
  • 90
  • Please show your compile line. If you don't use a compiler by hand, tell us your IDE, project settings/organization. The "undefined reference" isn't a compiler, but a linker error, which means that compilation worked fine, but your function wasn't found at the end. – Zeta Dec 09 '14 at 12:36
  • At a guess your implementation file needs to have the class name in front of the function: so in your .cpp file have void AESParser::parseVertices(std::vector... ){ ... – David Woo Dec 09 '14 at 12:39

1 Answers1

4

Your function belongs to the AESParser class, so you need to define it as such

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.

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • Gnarf, that seems to be the solution. So I have to write this even though it already is in the same class? (Sorry, didn't have to do this in Java). – Tobias Reich Dec 09 '14 at 12:39
  • 1
    In c++ you can have various methods of different classes in one .cpp file, while in java you embody all your class methods inside the class itself, which is also the name of the file. Quite a significant diffenerence. – Gio Dec 09 '14 at 12:41
  • 3
    @TobiasReich: This definition is in a namespace, not inside the class definition, so you need to qualify the name to indicate that it refers to the class member. You can also define member functions inside the class definition, similar to Java, in which case the name doesn't need qualification. – Mike Seymour Dec 09 '14 at 12:41
  • So instead of writing "using namespace std;" I should write "using namespace AESParser"? But then I get "AESParser is not a namespace-name" as error. – Tobias Reich Dec 09 '14 at 12:48
  • 1
    @TobiasReich Here's [an example](http://coliru.stacked-crooked.com/a/fe826297d64ce1ff) showing the difference between a free function and a method. Java and C# do not have free functions, which is why you're seeing a difference. – bames53 Dec 09 '14 at 12:50
  • @TobiasReich No, you don't need to mess with namespaces. namespaces and classes are different things. – bames53 Dec 09 '14 at 12:51
  • Okay, thanks a lot. I guess I have to read a bit more before I try all this. For now it works. Thanks to all of you! :) – Tobias Reich Dec 09 '14 at 12:53
  • 1
    @TobiasReich Well if you're looking for books [here's](http://stackoverflow.com/a/388282/365496) a good list of them. The first one I would suggest for someone with experience in other languages is _A Tour of C++_. – bames53 Dec 09 '14 at 13:22
  • I actually started reading it a few days ago but it's huge. Will take me a bit! Thanks! :) – Tobias Reich Dec 09 '14 at 14:26