Archive for December 30th, 2006
- In: c2143 | Code
- 29 Comments
Ever since I started programming with C++, I have been pulling my hair out trying to decrypt what the compiler errors mean.
Coming from the Java camp, I just hate how C++ compilers work and how stupid they are! After spending 3 hours trying to know why this piece of code won’t compile, I found out the answer after searching for a LONG time.
The code:
#pragma once
#include “Edge.h”
#include “IDGenerator.h”
class Face {
public:
Edge *edge;
int id;
Face(){
id = IDGenerator::getFaceID();
}
void setEdge(Edge *e) {
edge = e;
}
int getID() {
return id;
}
Edge *getEdge() {
return edge;
}
};
The compile error I was getting was:
error C2143: syntax error : missing ';' before '*'
it was choking at the “Edge *edge” line. To cut a long story short, if you are going to use a pointer to another class, don’t include the file as above, just use forward declaration by adding:
class Edge;
at the top of
class Face {
so that the final version looks like:
#pragma once
#include “IDGenerator.h”
class Edge;
class Face {
public:
Edge *edge;
int id;
Face(){
id = IDGenerator::getFaceID();
}
void setEdge(Edge *e) {
edge = e;
}
int getID() {
return id;
}
Edge *getEdge() {
return edge;
}
};
for a better explaination:
http://www-subatech.in2p3.fr/~photons/subatech/soft/carnac/CPP-INC-1.shtml
Finally, a blog!
Posted December 30, 2006
on:I finally have my own blog!
I have been intending to create one for a long time. I still don’t quite know what will I post here, but I will find something 🙂