using 'this' as parameter

Pages: 12
Sep 29, 2012 at 11:55am
Hi,
I'm having a quite confusing error.

I have class called Level. In this class I construct objects of the class Block. The constructor of Block looks like this:
Block(Level* level, int x, int y);

So in my level I have the following code:
Block b = new Block(this, 0,0);

And that's where I get the following error:
Argument of type 'Level *' is incompatible with parameter of type 'Level *'

I find this confusing because the error says that the argument and the paramter are of the same type. So why are they incompatible?

Thanks for the help
Sep 29, 2012 at 12:12pm
Show the relevant code.
Sep 29, 2012 at 12:22pm
ok, here is some more code (although what I posted is the most important part)

Block.h
1
2
3
4
5
6
7
8
9
10
11
class Block {
protected:
   int x;
   int y;

   Level* level;

public:
   Block(Level* level, int x, int y);

}


Block.cpp
1
2
3
4
5
6
Block::Block(Level* level, int x, int y) {
   this->level = level;

   this->x = x;
   this->y = y;
}


Level.cpp
1
2
3
4
5
6
7
8
9
10
bool Level::generateLevel(int amountOfBeasts) {

   for (int i = 0; i < WIDTH; i++) {
      for (int j = 0; j < HEIGHT; j++) {
         Block b = new Block(this, i,j); // this is the line where I get the error
         grid[i][j] = b;

      }
   }
}
Sep 29, 2012 at 12:46pm
Either
 
Block *b = new Block(this, i,j);
or
 
Block b(this, i,j);
Sep 29, 2012 at 1:11pm
Couldyou maybe give some more explenation about why this should help?

And the error now changed to:
no instance of constructor "Block::Block" matches the argument list
Sep 29, 2012 at 1:33pm
As @helios pointed out this statement

Block b = new Block(this, i,j);

is invalid.

The type of the expression new Block(this, i,j); is Block * but you are trying to initialize an object oif type Block
Sep 29, 2012 at 2:55pm
Ok, thanks, now I got that, what can I do about this new error? :s
Sep 29, 2012 at 3:21pm
Please post the entire compiler output, rather than just one line.
Sep 29, 2012 at 4:19pm
ok, but it's quite long (because of other problems):

1>  SoftBlock.cpp
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
1>c:\users\matthias\documents\visual studio 2010\projects\beast\beast\block.h(15): error C2061: syntax error : identifier 'Level'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(29): error C2628: 'Block' followed by 'unsigned' is illegal (did you forget a ';'?)
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(29): error C2628: 'Block' followed by 'int' is illegal (did you forget a ';'?)
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(29): error C2347: '__w64' : can not be used with type '__w64 Block'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(29): error C2371: 'size_t' : redefinition; different basic types
1>          c:\users\matthias\documents\visual studio 2010\projects\beast\beast\predefined c++ types (compiler internal)(19) : see declaration of 'size_t'
1>c:\users\matthias\documents\visual studio 2010\projects\beast\beast\level.h(16): error C2143: syntax error : missing ';' before '*'
1>c:\users\matthias\documents\visual studio 2010\projects\beast\beast\level.h(16): 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\level.h(16): 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(3): error C2011: 'Block' : 'class' type redefinition
1>          c:\users\matthias\documents\visual studio 2010\projects\beast\beast\block.h(3) : see declaration of 'Block'

Sep 29, 2012 at 4:19pm
1>  Level.cpp
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
1>c:\users\matthias\documents\visual studio 2010\projects\beast\beast\block.h(15): error C2061: syntax error : identifier 'Level'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(29): error C2628: 'Block' followed by 'unsigned' is illegal (did you forget a ';'?)
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(29): error C2628: 'Block' followed by 'int' is illegal (did you forget a ';'?)
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(29): error C2347: '__w64' : can not be used with type '__w64 Block'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(29): error C2371: 'size_t' : redefinition; different basic types
1>          c:\users\matthias\documents\visual studio 2010\projects\beast\beast\predefined c++ types (compiler internal)(19) : see declaration of 'size_t'
1>c:\users\matthias\documents\visual studio 2010\projects\beast\beast\level.h(16): error C2143: syntax error : missing ';' before '*'
1>c:\users\matthias\documents\visual studio 2010\projects\beast\beast\level.h(16): 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\level.h(16): 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(3): error C2011: 'Block' : 'class' type redefinition
1>          c:\users\matthias\documents\visual studio 2010\projects\beast\beast\block.h(3) : see declaration of 'Block'
1>  Block.cpp
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
1>c:\users\matthias\documents\visual studio 2010\projects\beast\beast\block.h(15): error C2061: syntax error : identifier 'Level'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(29): error C2628: 'Block' followed by 'unsigned' is illegal (did you forget a ';'?)
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(29): error C2628: 'Block' followed by 'int' is illegal (did you forget a ';'?)
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(29): error C2347: '__w64' : can not be used with type '__w64 Block'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(29): error C2371: 'size_t' : redefinition; different basic types
1>          c:\users\matthias\documents\visual studio 2010\projects\beast\beast\predefined c++ types (compiler internal)(19) : see declaration of 'size_t'
1>c:\users\matthias\documents\visual studio 2010\projects\beast\beast\level.h(16): error C2143: syntax error : missing ';' before '*'
1>c:\users\matthias\documents\visual studio 2010\projects\beast\beast\level.h(16): 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\level.h(16): 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(3): error C2011: 'Block' : 'class' type redefinition
1>          c:\users\matthias\documents\visual studio 2010\projects\beast\beast\block.h(3) : see declaration of 'Block'
1>  Generating Code...
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:04.06
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Sep 29, 2012 at 4:55pm
I don't see the error you mentioned.
Sep 29, 2012 at 5:44pm
Now that you mention it, no :s
It's just that I'm working in visual studio, and there appears a red line under the word "this", and when hovering it shows this is error :s
Sep 29, 2012 at 5:56pm
Maybe if you try fixing as many of the errors as you can, it will make things clearer.
Sep 29, 2012 at 6:01pm
I don't see a semicolon after your class declaration. Is it there?
Sep 29, 2012 at 6:16pm
Do you need a semicolon if the class declaration is the only thing in the header file?
I thought that it was only necessary if it was in the .cpp file and the implementation came after it.
Sep 29, 2012 at 6:36pm
Since #include directives are simply replaced with the contents of the specified file, there's almost always something after the class declaration. And even when there isn't, the semicolon is still required.
Sep 29, 2012 at 6:38pm
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(29): error C2628: 'Block' followed by 'unsigned' is illegal (did you forget a ';'?)
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(29): error C2628: 'Block' followed by 'int' is illegal (did you forget a ';'?)


Doesn't VS highlight syntax errors with a red felt tip pen?
Sep 30, 2012 at 9:01am
So I've placed the semicolons.

@Zephilinox
I would assume that visual studio would do that, but apparently not :s The only red there is is as I described in my previous posts.
Sep 30, 2012 at 1:05pm
I'm trying to figure out what the cause is of all these errors, but they are very confusing to me. Can anyone give some help, or tips?

Any help is very appreciated
Sep 30, 2012 at 1:48pm
These mostly appear to be syntax errors. That's a bit like spelling or grammatical errors in ordinary language - but the compiler is less forgiving.

Without seeing the source code, it's hard to meaningfully comment, except to say that sometimes a huge number of messages might be triggered from a single coding error, so it's generally a good plan to try to fix the first few errors and compile again to see what remains.
Pages: 12