Jump to content
  • Advertisement
Sign in to follow this  
Ultimate_Fusion

c++ Class problem

This topic is 4234 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to make a class and have had alot of errors. I am using c++ and visual studio. Test.cpp #include "StdAfx.h" #include "SDL.h" #include "Test.h" #define SPRITE_SIZE 94 class Test { //int x,y; public: Test(); //default contructor // contructor ~Test (); // deconstructor call //Test Functions Test(int d,int p){ /* set sprite position */ SDL_Rect rcSprite; rcSprite.x = 150; rcSprite.y = 150; /* set animation frame */ SDL_Rect rcSrc; rcSrc.x = 0; rcSrc.y = 0; rcSrc.w = SPRITE_SIZE; rcSrc.h = SPRITE_SIZE; } void CollisionWithWalls () { SDL_Rect rcSprite; /* collide with edges of screen */ if (rcSprite.x <= 0) rcSprite.x = 0; if (rcSprite.x >= SCREEN_WIDTH - SPRITE_SIZE) rcSprite.x = SCREEN_WIDTH - SPRITE_SIZE; if (rcSprite.y <= 0) rcSprite.y = 0; if (rcSprite.y >= SCREEN_HEIGHT - SPRITE_SIZE) rcSprite.y = SCREEN_HEIGHT - SPRITE_SIZE; } ); Test.h #pragma once class Test { public: Test(); ~Test(); Test(int d,int p); void CollisionWithWalls(); }; **Main main{ Test a = new a(20,190); } errors cpp(9) : error C2011: 'Test' : 'class' type redefinition test.h(5) : see declaration of 'Test' cpp(53) : error C2079: 'adam' uses undefined class 'Test' cpp(53) : error C2061: syntax error : identifier 'a' cpp(9) : error C2011: 'test' : 'class' type redefinition Test.h(5) : see declaration of 'test' 1>Generating Code... " 1>demiurgic - 5 error(s), 0 warning(s) ========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

Share this post


Link to post
Share on other sites
Advertisement
Your test.cpp file should look like this :

#include "StdAfx.h"
#include "SDL.h"
#include "Test.h"


#define SPRITE_SIZE 94



Test::Test()
{
}


Test::~Test ()
{
}

Test::Test(int d,int p)
{
SDL_Rect rcSprite;
rcSprite.x = 150;
rcSprite.y = 150;
/* set animation frame */
SDL_Rect rcSrc;
rcSrc.x = 0;
rcSrc.y = 0;
rcSrc.w = SPRITE_SIZE;
rcSrc.h = SPRITE_SIZE;
}

void Test::CollisionWithWalls ()
{
SDL_Rect rcSprite;
/* collide with edges of screen */
if (rcSprite.x <= 0)
rcSprite.x = 0;
if (rcSprite.x >= SCREEN_WIDTH - SPRITE_SIZE)
rcSprite.x = SCREEN_WIDTH - SPRITE_SIZE;

if (rcSprite.y <= 0)
rcSprite.y = 0;
if (rcSprite.y >= SCREEN_HEIGHT - SPRITE_SIZE)
rcSprite.y = SCREEN_HEIGHT - SPRITE_SIZE;
}







You don´t need the whole class declaration again.

edit: The function Test(int d,int p) is another constructor by the way. Don´t know if this is intended but I thought I should mention it anyway.


edit2: :D And now that I fully read it

this

//Main.cpp
main{
Test a = new a(20,190);
}




should be either this

//Main.cpp
int main(void)
{
Test *a = new Test(20,190);
}




or this

//Main.cpp
int main(void)
{
Test a = Test(20,190);
}


Share this post


Link to post
Share on other sites
Hi

Im guessing you are new to C++ and gamedev (since you should use the source code tags when putting in code to make it easier to read). It also seems that you dont understand the compilers messages. in Test.cpp you dont need the class block around the code, since you already declared the Test class in Test.h then it is already defined when the complier gets to the cpp - you have rightly included Test.h in Test.cpp. Instead of class "Test{ ... all your code...};" in your cpp you have to make the methods be in the test classes scope. Dont worry too much about what that means right now all you need to know is that before each method of the implementation of the Test class in Test.cpp you just write "Test::" to tell the compiler that the method belongs in the Test class. so your consturctor should be Test::Test() in test.cpp, the same for the rest of the functions. The other problem i spotted was in your code for mian. Here you have "Test a = new a(20,190);" which should be "Test* a = new Test(20,190);" if you want to be using a pointer to an instance of the Test class which it looks like you are trying to do.

Edit: good job Fynn you beat me to it

Share this post


Link to post
Share on other sites
thanks that got rig of alot of errors.
but I still have 2 which I dont understand.

//I had to change this
test a = new a(1,2);
//to this -
test a(1,2);

why?

and the big question is
//this line is in the main
SDL_BlitSurface(sprite, NULL, screen, &rcSprite);

rcSprite is in declared in test.cpp.
so it doesn't recogise it.

I tried a.rcSprite and Test.rcSprite and loads of other variations but no luck.

I would be very greatfull if some-one knows the answer to this head scratcher!!

error C2664: 'SDL_UpperBlit' : cannot convert parameter 4 from 'SDL_Rect' to 'SDL_Rect *'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
warning C4832: token '.' is illegal after UDT 'Test'

Share this post


Link to post
Share on other sites
Quote:
Original post by Ultimate_Fusion
//I had to change this
test a = new a(1,2);
//to this -
test a(1,2);

why?
The short answer is that 'new' returns the address of an object or array of objects that has been allocated on the heap. In C/C++ you use pointers to store addresses, therefore the assignee must be a pointer. It is not, so the line generates a compile-time error. (Also, 'a' is not a type, as noted below.)

The second line (which is valid) simply creates an object of type 'test' on the stack.

If you're new to C++ the above may not make much sense to you. C and C++ are both fairly complicated languages, and it can take time to come to grips with them. It kind of looks to me as though you're 'diving into the deep end', as it were, by tackling SDL and graphics programming before developing an understanding of the fundamentals of the language. This is certainly one way to learn, but you might also try writing some simpler programs to explore some of these concepts in a narrower context.

[Edit: Fixed errors.]

[Edited by - jyk on November 15, 2006 5:25:15 PM]

Share this post


Link to post
Share on other sites
test a = new a(1,2) will not work for 2 reasons

1. a is not a pointer (new returns pointers to things)
2. one the right side of the = [ the new a(1,2) ] a is not a type. (new wants a type)

To fix the problem, assuming you want to allocate on the heap is to change it slightly to

test* a = new test(1,2);

You can then access rcSprite with a->rcSprite for the pointer version or a.rcSprite for the stack version

The reason why test a(1,2) worked is because you are making an object called a on the stack... Similar to making an intteger or float object

int a(2);

or the new version

int* a = new int(5);

Hope this makes things a little clearer


Share this post


Link to post
Share on other sites
Quote:
Original post by Ultimate_Fusion
//I had to change this
test a = new a(1,2);
//to this -
test a(1,2);

why?


Because this isn't Java.

(If you're *not* from a Java background, why would you *expect to need* 'new' here?)

Quote:

and the big question is
//this line is in the main
SDL_BlitSurface(sprite, NULL, screen, &rcSprite);

rcSprite is in declared in test.cpp.
so it doesn't recogise it.


It shouldn't be in the main, then.

As it *was*, 'rcSprite' was a *local* variable in some Test member functions. There's simply no way you can get at such a thing "from outside".

Even if you make rcSprite a member of Test, you need to indicate which Test object you're getting the rcSprite from, and it has to be publically available. E.g.


struct Test {
SDL_Rect rcSprite;
// more stuff
};

// Then later on, as long as the Test declaration is visible
int main() {
Test t;
// more stuff
SDL_BlitSurface(sprite, NULL, screen, &(t.rcSprite));
// i.e. "address of the rcSprite member of the t Test object"
}


Quote:
I would be very greatfull if some-one knows the answer to this head scratcher!!

error C2664: 'SDL_UpperBlit' : cannot convert parameter 4 from 'SDL_Rect' to 'SDL_Rect *'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called


I should hope this much is obvious, but pointers are not the same kind of thing as the thing they point to. The SDL_UpperBlit() function expects parameter 4 to be a pointer to an SDL_Rect. You can't give it an SDL_Rect there, because SDL_Rects aren't pointers at all, let alone pointers to SDL_Rects. The compiler says, quite literally, that it can't do a conversion here, because the types aren't similar enough (and because you didn't provide a way of doing this conversion implicitly - but you *should not* attempt that).

The SDL library functions are written to be compatible with C, as well as C++. In C there is no pass-by-reference available, so commonly you pass pointers to things in place of the actual things. That way, the implementation of the function can dereference the pointer to get at the caller's actual object (and not a copy of it), and possibly change it (i.e. the caller will "see" changes to the object).

Long story short, you need to prefix the thing you're passing with '&' (the address-of operator). The expression '&x' evaluates to "a value of type pointer-to-(type of x), which points to x".

Quote:

warning C4832: token '.' is illegal after UDT 'Test'


'Test' is a class, not an object. Again, this isn't Java. We don't use '.' to get at static class members (never mind that you currently don't have any); we use '::'.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!