using 'this' as parameter

Pages: 12
I know they are syntax errors, but the syntax seems right :s
For example:

1>c:\users\matthias\documents\visual studio 2010\projects\beast\beast\block.h(12): error C2143: syntax error : missing ';' before '*'
1>c:\users\matthias\documents\visual studio 2010\projects\beast\beast\block.h(12): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\matthias\documents\visual studio 2010\projects\beast\beast\block.h(12): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int


These are all about line 12 and line 12 is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "global.h"

class Block {
protected:
	bool moveable;
	bool absorbs;
	char image;

	int x;
	int y;

	Level *level;

public:
	bool push(int dX, int dY);
	void setPosition(int x, int y);

	char getImage();

};
I get a different error, but the reason in my case is that "Level" is unknown. (and I have a different compiler).

I assume "Level" is declared in "global.h"? If not, maybe you need to put something like #include "level.h"   at the top of this code.
Global.h looks like this:
1
2
3
4
5
6
7
#pragma once

#include "Level.h"
#include "Block.h"
#include "SoftBlock.h"
#include "SolidBlock.h"
#include "TextBasedView.h" 


So yes, it's included, or isn't this the right way to do this?

IN MY OPINION ...

My friend the "this" in
Block *b = new Block(this, 0,0);
points at the object which calls the constructor "Block(this, 0,0)", e.g. the "*b" object, which is an object of type "Block"

But in the constructor
Block(Level* level, int x, int y);
the previous "this", goes as parameter in the pointer "Level* level", which is type "Level"

As you understand, except the syntax errors you had (and you have to correct them - maybe you all ready did ...), you have logical errors and maybe you have to re-write the classes again with other way !
You obviously programming a game, and you have to reconsider the code again !!!

Sorry for my English - I am Greek !!
If the "this" in
Block *b = new Block(this, 0,0);
points to the object *b, how then can I pass the Level object (which creats the block)?

I'm used to programming in Java and this is my first project (my target is to try to understand how C++ works). In Java "this" always refers to the object which is creating. Off course this can be diffrent in C++, but is there anything alike in C++?

About the syntax errors:
I still haven't fixed any of them because I don't understand where these are coming from.

I'm sorry that I'm asking all these (probably basic) questions. I'm trying to learn C++ as fast as possible.
It is very good that you are asking !!
For the syntax errors I said that you maybe all ready corrected them...

C++ is very difficult to learn immediately. In java there are pointers but you don't have to worry about them. Here in C++ pointers are very serious thing ...
I am studying C & C++ for 5 years & it is my first time that I try to help someone ...

I CAN HELP YOU IN THIS PART OF THE QUESTION

The previous answer I gave you, is why the compiler shows error (in MY OPINION & I am not sure about this... If someone more experienced can give another explanation, I will gladly accept it)

As I told before the "this" points at the object which calls the function (here you have the function: constructor "Block(this, 0,0)"). So it points at an object of type "Block".

You can not pass it to the pointer "Level* level", which is type "Level".
This is the technical thing I am discussing you you ....

THE 2nd PART OF THE QUESTION

For the 2nd part of your question, I think someone else maybe can help you better !
Unfortunately I can not find a way to pass the Level object in Block...
I can not understand how you setup the classes.
You created 2 classes which the 1st needs the 2nd & the 2nd needs the 1st .
This creates a confusing situation !!

For the 2nd part of your question, if I were you,
1. I would wait for someone more experienced to find a way to give you a better answer, or
2. I would try to setup the classes with another way !

Sorry that I can not help you completely in your problem (and I helped you partially).
C++ is indeed very confusing in the beginning :s

I will definitly rethink my class structures. This will probably allready fix a great deal of my problems.

I assume that I'm thinking to much java-ish. And c++ is definitly to diffrent, which causes problems (I think).

I have learned one thing:
I'm writing way to much code in one time :s This way I get a lot of errors and can't find the cause of them.

I thank everyone for helping me out. This was my first thread on this forum and I got a lot of respons which was very satisfying.

Greatings and till the next problem :p

Genzm
Farewell my friend !!
I think I found a solution:
I've created a new class called "LevelGenerator" which (off course) generates levels.
It has a method generate which kind of looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Level* LevelGenerator::generate() {
	Level* lvl = new Level();

	vector<vector<Block> > grid;
	/*
	*
	*  fill grid with blocks
        *         Block* b = new Block(lvl, locX, locY);
	*
	*/
	
	lvl->assignGrid(grid);

	return lvl;
}


This way I no longer need to use the 'this'. So rethinking my classes did do the trick :p
Thanks for that tip.

In the progress of trying to remove all those errors I got stuck on 1 which I think causes most of them.
I've made a new thread (because it's a diffrent problem) for it. Any help will off course be appreciated, and again I off course also appreciate all the help I've allready gotten.

greatings,
genzm
Topic archived. No new replies allowed.
Pages: 12