Sign in to follow this  
Frizbe

Classes

Recommended Posts

Frizbe    122
I am having trouble setting up my classes for some reason. this is my class as it stands now: class code { public: void SDL_Init(); }; When I compile this I get this error: error C2011: 'code' : 'class' type redefinition I am using Visual Studio 2005. I am fairly sure I have everything in the correct order, but keep getting this error. Thanks for any help.

Share this post


Link to post
Share on other sites
AIDev    190
Change the name of the class from 'code' to something else. 'Code' is a pretty generic name, and it looks like it was previously defined, perhaps in one of your header files.

Share this post


Link to post
Share on other sites
fpsgamer    856
Did you make sure to use include guards?

inside each header file:

#ifndef __HEADER_1_INCLUDED__ // use a unique constant here for each header file
#define __HEADER_1_INCLUDED__

// put header file content here

#endif // __HEADER_1_INCLUDED__

more details here:

http://www.gamedev.net/reference/programming/features/orgfiles/

Share this post


Link to post
Share on other sites
Frizbe    122
Quote:
Original post by AIDev
Change the name of the class from 'code' to something else. 'Code' is a pretty generic name, and it looks like it was previously defined, perhaps in one of your header files.

I have tried a few different names and nothing changes. I am still trying to just get the classes set up before i try anything else now.

Quote:
Original post by fpsgamer
Did you make sure to use include guards?

inside each header file:

#ifndef __HEADER_1_INCLUDED__ // use a unique constant here for each header file
#define __HEADER_1_INCLUDED__

// put header file content here

#endif // __HEADER_1_INCLUDED__

more details here:

http://www.gamedev.net/reference/programming/features/orgfiles/


I have no idea what you are talking about. Kinda sucks I have looked at some stuff online and in my C++ for Dummies books and everything I have read says I have it set up right.

And this error is coming up on code that worked 6 months ago. Which is why I belive it is a setting in VS, but alas I can't figure out which setting it is.

Share this post


Link to post
Share on other sites
fpsgamer    856
Quote:
Original post by Frizbe
I have no idea what you are talking about. Kinda sucks I have looked at some stuff online and in my C++ for Dummies books and everything I have read says I have it set up right.


At this point we can't say for sure what is wrong without seeing your code. But I still suspect it is the include guard problem.

As a general practice, ALL header files should have include guards anyways. The fact that your C++ text neglected to mention that is a bit worrisome.

Share this post


Link to post
Share on other sites
Frizbe    122
Quote:
Original post by fpsgamer
Quote:
Original post by Frizbe
I have no idea what you are talking about. Kinda sucks I have looked at some stuff online and in my C++ for Dummies books and everything I have read says I have it set up right.


At this point we can't say for sure what is wrong without seeing your code. But I still suspect it is the include guard problem.

As a general practice, ALL header files should have include guards anyways. The fact that your C++ text neglected to mention that is a bit worrisome.


This is my entire header as it stands now because I keep getting this error:

#include <iostream>
#include <string.h>
#include "SDL/SDL.h"
using namespace std;

class code
{
public:
void SDL_Init();
};

Share this post


Link to post
Share on other sites
fpsgamer    856
Quote:
Original post by Frizbe
This is my entire header as it stands now because I keep getting this error:

#include <iostream>
#include <string.h>
#include "SDL/SDL.h"
using namespace std;

class code
{
public:
void SDL_Init();
};


It would be helpful if we could also see your source files.

Share this post


Link to post
Share on other sites
Frizbe    122
Quote:
Original post by fpsgamer
Quote:
Original post by Frizbe
This is my entire header as it stands now because I keep getting this error:

#include <iostream>
#include <string.h>
#include "SDL/SDL.h"
using namespace std;

class code
{
public:
void SDL_Init();
};


It would be helpful if we could also see your source files.

As in IOStream, String.h and SDL.h?
Sorry for the beginner-esque questions I am still trying to a handle on the terminology.

Share this post


Link to post
Share on other sites
Quote:
Original post by Frizbe
Quote:
Original post by fpsgamer
Quote:
Original post by Frizbe
I have no idea what you are talking about. Kinda sucks I have looked at some stuff online and in my C++ for Dummies books and everything I have read says I have it set up right.


At this point we can't say for sure what is wrong without seeing your code. But I still suspect it is the include guard problem.

As a general practice, ALL header files should have include guards anyways. The fact that your C++ text neglected to mention that is a bit worrisome.


This is my entire header as it stands now because I keep getting this error:

#include <iostream>
#include <string.h>
#include "SDL/SDL.h"
using namespace std;

class code
{
public:
void SDL_Init();
};


Like previous posters have said, you're missing include guards. Obligatory link for more information.

EDIT: Whoops, you've already been linked. But the point is, you still need them. The point of include guards is to prevent your compiler from "reading" your header file more than once. C++ doesn't allow multiple definitions of the same named thing, so if you #include your header files in more than one place without the appropiate guards, you'll hit the redefinition error.

EDIT2: I should stop skimming so quickly. fpsgamer has explained exactly what you need to do in this post

EDIT3: And do read the article! It's important :P

Share this post


Link to post
Share on other sites
fpsgamer    856
Quote:
Original post by Frizbe
Quote:
Original post by fpsgamer
Quote:
Original post by Frizbe
This is my entire header as it stands now because I keep getting this error:

#include <iostream>
#include <string.h>
#include "SDL/SDL.h"
using namespace std;

class code
{
public:
void SDL_Init();
};


It would be helpful if we could also see your source files.

As in IOStream, String.h and SDL.h?
Sorry for the beginner-esque questions I am still trying to a handle on the terminology.


Header files: Your files that end in .h
Source files: Your files that end in .c or .cpp

Share this post


Link to post
Share on other sites
Frizbe    122
Quote:
Original post by fpsgamer

Header files: Your files that end in .h
Source files: Your files that end in .c or .cpp

Ah ok,
header(as before AKA: "Radius_header.h"):

#include <iostream>
#include <string.h>
#include "SDL/SDL.h"
using namespace std;

class code
{
public:
void SDL_Init();
};


Implementation:

#include "Radius_header.h"
using namespace std;


Source:
#include "Radius_header.h
using namespace std;

int main(int argc, char *argv[])
{

return 0;
}


[Edited by - Frizbe on August 4, 2007 11:00:03 PM]

Share this post


Link to post
Share on other sites
Frizbe    122
Alright I got it working with #pragma once, its been awhile since I used SDl last and for got about that part. Oops.

Thanks for the help fpsgamer and load_bitmap_file.
Although this has left me wondering if the #pragma once and #ifndef/#define/#endif work in the same way or is one better than the other to use?

Share this post


Link to post
Share on other sites
fpsgamer    856
Quote:
Original post by Frizbe
Alright I got it working with #pragma once, its been awhile since I used SDl last and for got about that part. Oops.


"#pragma once" has nothing to do with SDL in particular, it is a concept necessary for coding with multiple files in C and C++. You should really read the article I posted earlier.

Quote:
Original post by Frizbe
Thanks for the help fpsgamer and load_bitmap_file.
Although this has left me wondering if the #pragma once and #ifndef/#define/#endif work in the same way or is one better than the other to use?


"#pragma once" performs the same job as include guards except that "#pragma once" is non-standard. That is to say it is not supported by all compilers.

Check Wikipedia for both topics ("#pragma once" and include guards), its covered pretty thoroughly there.

Its up to you to decide which is better. If you feel like your project will never use a different compiler then the "#pragma once" directive is fine.

Share this post


Link to post
Share on other sites
JohnBolton    1372
Three suggestions:
  1. SDL_Init is the name of an SDL function. Even though you can have a member function named SDL_Init, I recommend that you don't. It will be less confusing.
  2. Putting "using namespace std;" in a header file is a bad practice. Whatever convenience you might get out of doing that is not worth the problems you may have in the future.
  3. Only include header files that are needed by the file doing the including. In your case, your header file needs no other header files, so it should not be including any.


Here is a good article on organizing code. It is recommended reading for all beginners: Organizing Code Files in C and C++

Edit: haha I see that this article has already been linked to twice in this thread. I guess that means that you should really read it.

Share this post


Link to post
Share on other sites
Frizbe    122
Quote:
Original post by JohnBolton
Three suggestions:
  1. SDL_Init is the name of an SDL function. Even though you can have a member function named SDL_Init, I recommend that you don't. It will be less confusing.
  2. Putting "using namespace std;" in a header file is a bad practice. Whatever convenience you might get out of doing that is not worth the problems you may have in the future.
  3. Only include header files that are needed by the file doing the including. In your case, your header file needs no other header files, so it should not be including any.


Here is a good article on organizing code. It is recommended reading for all beginners: Organizing Code Files in C and C++

Edit: haha I see that this article has already been linked to twice in this thread. I guess that means that you should really read it.

I have read the article now.
Could you explain what problems I would run into putting using namespace std in my header file?

Share this post


Link to post
Share on other sites
JohnBolton    1372
Quote:
Original post by Frizbe
Could you explain what problems I would run into putting using namespace std in my header file?


Basically, "using namespace xxx;" in a header file will apply to every file that includes it and every file that is included after it. It's bound to cause a name clash at some point. Even if it doesn't, it is poor programming because you are creating restrictions for no good reason.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this