Weird "off-topic" compiler errors

Hey everyone.

I'm a (A-bit-more-than-beginner-but-not-really-good-too-)Java Developer learning C++, and I decided to program a really simple game with SFML in order to get comfortable with C++.

I wrote a few lines of codes, and tried running it for the first time - And I directly get a hundred of compiler errors. This isn't bad nor unusual in itself, but the thing bothering me, is that those errors seem to be completely "off-topic": I get Syntax-Errors where OBVIOUSLY everything is alright.

First, here are all the types of errors that I get:
- C4430: missing type specifier - int assumed. Note: C++ does not support default-int
I already read quite a few articles and forum-posts on this error, but unfortunately none of the problems seem to apply to me. I'm pretty damn sure, that I did NOT forget any typespecifiers, and nor did I forget to put Include Guards in my header files.


- C2976: too few type arguments
I'm using a std::vector in a few places, and, NO, I did NOT forget the template-argument. I just don't understand why I get this error.


- C2661: no overloaded function takes number parameters
Again - The compiler tells me that I don't put the right number of arguments in my functions, but unfortunately for me, I did.


- C2504: base class undefined
So, I get this error in some (If not all) of my base-classes. And no, I did not forget to include the superclass-header, nor did I forget to put the class-definition in the superclass-header.


- C2143: missing ';' before '*'
I got some functions returning a pointer to another one of my objects. I did include that object though, but the compiler does not seem to recognise it. A "real" syntax-error is completely impossible, in my opinion. Or maybe I'm just stupid.


- C2065: undeclared identifier
Apparently the compiler doesnt recognise the class I'm using. Even if I did include it... Again.


- C2061: identifier 'identifier' ("World" in my case)
NO, I did NOT mistype my classname. And I DID include it.


- C2059: syntax error : '>'
Getting this on my vector-declaration. No clue why, of course.


Those errors are driving me crazy, mainly because they make no freaking sense. If the compiler would tell me "Yeah you missed that include here, bro" it would be so much easier, but telling me "syntax-error" for something completely "else"? TL;DR I hate that compiler.

Finally I'll post a bit of code. I'll try to keep it short and readable, but since the errors are only in the headerfiles anyway that shouldnt be that much of a problem (Not posting all errors of course, just one file illustrating every problem. Since I got like at least 10x the same error.)

---- WORLD.H ----
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#ifndef World_H
#define World_H

#include "Entity.h"
#include "BallEntity.h"
#include <Box2D/Box2D.h>
#include <SFML\Graphics.hpp>
#include <wink\slot.hpp>
#include <wink\event_queue.hpp>
#include <map>
#include "Event.h"
#include "CollisionEvent.h"
#include "WallEntity.h"
#include "Assets.h"
#include <stdlib.h>


class World : public b2ContactListener {

private:
	std::vector<Entity*> entities;
	sf::RenderTarget * target;

	//std::map<unsigned short int, wink::signal<wink::slot<Event>>> eventSenders;

public:
	b2World physicsWorld;
	World();
	void deleteEntity(Entity * entityToRemove);
	void createBall();
	void moveEntity(float delta, sf::Vector2f velocity, Entity * entity);
	void update(sf::Time delta);
	void render();
	void BeginContact(b2Contact* contact);
	void EndContact(b2Contact* contact);

};

#endif 

Here, the errors are on the linestd::vector<Entity*> entities;
Errors here are: C2059, C2065, C2976, C2061(on multiple of the function declarations)
-----------------

--- ENTITY:H ---
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#ifndef Entity_H
#define Entity_H

#include "World.h"
#include "Body.h"
#include <SFML\Graphics.hpp>
#include <Box2D/Box2D.h>


/**
Abstract entity-class.
*/
class Entity {

public:

	/**
	Entity-Constructor. Only called by subclasses.
	@param RenderTarget Class used to draw Entities. (SFML specific).
	*/
	Entity(World * world, sf::RenderTarget* target);

	/**
	Destructor, inherited and defined by subclasses.
	*/
	virtual ~Entity() = 0;

	/**
	Returns the Body for this Entity.
	*/
	Body* getPhysicsBody();

	/**
	Returns the World in which the Entity is contained.
	*/
	World* getWorld();

	/**
	Creates the Body for this Entity.
	*/
	virtual Body* createBody() = 0;

	/**
	Method used to render the entity. Defined by subclasses.
	*/
	virtual void render() = 0;

	/**
	Method called every tick to update the entity. In most cases unsused.
	*/
	virtual void update(float delta) = 0;

	/**
	Get type of entity.
	*/
	virtual unsigned short int getType();

protected:
	sf::RenderTarget* target;
	Body * body;
	World * parent;

};

#endif 


Got C2061 errors with "World" identifier, and C2143 ";" before "*" errors. And of course C4430 errors a bit everywhere.

----------------

In those two classes nearly all errors are represented. As I said, I can't wrap my head around why it shows me those weird errors.


Would be really awesome if someone could help me (, before I destroy my computer in a burning fit of rage) =)

Kind regards


Without seeing the complete error messages (and the relevant code) , all of them, there is no way we can tell what you are doing wrong. Error messages have important information embedded within them to aid in locating and fixing the problems and they must be evaluated from the top down. Quite often one error can cause several other errors later in the chain.


Last edited on
1) only first error matters. Do not look at others until you fix this error first. Often next errors are there because of first. For example if you will made a mistake when writing type name in template parameters, you might get both "unknown name" (real error) and "not enough template parameters" (consequence of the first). If you mistype type in function return type, you will get "default int not supported" as consequence.

2) You have circular include. This is a problem and will cause many errors. Your dependence tree should be a tree. That means if header A includes header B then header B should not include header A. If you ever need that, you got your program design wrong. You can: separate common functionality in separate header (not your case); or forward declare class instead of including whole header (works only if you access that class only through pointers or references)
@jlb
Well, to be honest I think that the code I posted actually is the relevant code. Posting the error log is going to take me a while tho, because my VS is in German (Thats why I posted only the errors itself)

@MiiNiPaa

1) Thing is, I don't think that I mistyped anything.

2) But aren't my header guards preventing that loop from causing any problems? If I got it right, the compiler creates somewhat one, big, file with everything included in it - So my headerguards should be able to assure that everything only is included once.

Actually I don't know how to do it without that circular include, since my "World" Object holds an Array of all the entities I have, but each Entity also holds a reference to the world.
1) Thing is, I don't think that I mistyped anything.
Those were just examples of what could go wrong.

2) But aren't my header guards preventing that loop from causing any problems?
It prevents multiple inclusion problem. Circular include is still a problem.
Example of what happens:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//1.h (I will not post header guards, pretend they are here)
#include "2.h"

class foo 
{
   bar b;
/*rest of class definition*/};

//2.h 
#include "1.h"

class bar
{
   foo b;
/*rest of class definition*/};
After inclusion of 1.h somewhere (first iteration):
1
2
3
4
5
6
7
8
9
10
11
12
//Here is content of 2.h copy-pasted (what include do)
#include "1.h"

class bar
{
   foo b;
/*rest of class definition*/};

class foo 
{
   bar b;
/*rest of class definition*/};
second iteration:
1
2
3
4
5
6
7
8
9
10
11
//Header guards prevent second inclusion of 1.h

class bar
{
   foo b; //Error, foo is not defined before
/*rest of class definition*/};

class foo 
{
   bar b;
/*rest of class definition*/};
Cinzedark wrote:
Actually I don't know how to do it without that circular include, since my "World" Object holds an Array of all the entities I have, but each Entity also holds a reference to the world.
MiiNiPaa wrote:
forward declare class instead of including whole header (works only if you access that class only through pointers or references)
So if we return to simplified example I made, you can rewrite it as:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//1.h (I will not post header guards, pretend they are here)
#include "2.h"

class foo 
{
   bar b;
/*rest of class definition*/};

//2.h 
class foo; //Forward declaration. Note that we do not need include anymore

class bar
{
   foo* b; //or foo& depending on your needs
/*rest of class definition*/};
Last edited on
Thanks for the answer! Now I've another question though... If the class is only declared as class foo;, the class bar won't have any access to the member functions of foo, or will it? I mean, I still did not get that whole header concept right, but if you don't include that full declaration stuff, you can't use it, right?
If you separate class definition and class implementation, everything is possible:
1
2
3
4
5
6
7
8
9
10
//A.h
class B; //Do not need any info about B aside from fact that it exists

class A
{
public:
    B* b;
    void doOwnStuff();
    void doOtherStuff();
};
1
2
3
4
5
6
7
8
9
10
//B.h
class A; //Do not need any info about A aside from fact that it exists

class B
{
public:
    A* a;
    void doOwnStuff();
    void doOtherStuff();
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//A.cpp
#include <iostream> //For implementation of doOwnStuff

#include "A.h" //We need our own definition to implement it
#include "B.h" //We need B definition to use it

void A::doOwnStuff()
{
    std::cout << "A stuff\n";
}

void A::doOtherStuff()
{
    b->doOwnStuff();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//B.cpp
#include <iostream> //For implementation of doOwnStuff

#include "B.h" //We need our own definition to implement it
#include "A.h" //We need A definition to use it

void B::doOwnStuff()
{
    std::cout << "B stuff\n";
}

void B::doOtherStuff()
{
    a->doOwnStuff();
}
1
2
3
4
5
6
7
8
//main.cpp
#include "A.h" //We use A class

int main()
{
    A a;
    a.doOtherStuff();
}
B stuff
Just remember to compile A.cpp and B.cpp alongside your main.cpp
Wooooow, thank you so much, every single problem disappeared!

Unfortunately, now I got a whole lot of LNK2019 errors... I already had and solved that error, but at that time I was working with templates - Which I ain't right now... Any idea why I get those errors? Sorry for bothering you so much, but apparently I just ain't capable of solving it myself... =(
It is hard to tell exactly why this error is happening without code.
http://msdn.microsoft.com/en-us/library/799kze2z.aspx

I would bet on "Missing Function Body or Variable" case.
http://msdn.microsoft.com/en-us/library/x3k07566.aspx

Make sure that you are compiling all cpp files and not only main file. (They added to solution explorer and marked as building)
Managed to solve it =) Multiple errors caused that compiler-error, one of them being that I forgot to make my virtual function pure virtual.
Topic archived. No new replies allowed.