Sign in to follow this  

main class connected to two classes which have the same super class in C++

This topic is 4205 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 have a bullets class and a enemies class which both are sprite objects and they also have their own specific methods. Eg bullets have damage property and enemies have a health property. C++ doesn't like me including these two classes as they both are sprite object. It thinks that enemy class redefines the sprite class. So what should I do? I included the definition of tboth classes into my main class otherwise they wont recognise the classes. #include "bullet.cc" #include "enemy.cc"

Share this post


Link to post
Share on other sites
Why are you using .cc's instead of regular .h and .cpp files?

I would guess that your problem stems from multiple files including your sprite file, so it gets defined multiple times, which can be solved by using
#ifndef
#define
include guards, or on win32 by putting "#pragma once" at the beginning of a file.

Share this post


Link to post
Share on other sites
The standard extensions for C++ files are '.h' fo header and '.cpp' for source files. You should use these whenever possible.

As for your problem, the other posters above me have mentioned everything I guess. Don't ever include source files (you need to link them instead - for eg. by adding them to your project if you're using an IDE), just their headers, and don't forget to use include guards in the header files.

Share this post


Link to post
Share on other sites
Quote:
Original post by Morrandir
The standard extensions for C++ files are '.h' fo header and '.cpp' for source files. You should use these whenever possible.

As for your problem, the other posters above me have mentioned everything I guess. Don't ever include source files (you need to link them instead - for eg. by adding them to your project if you're using an IDE), just their headers, and don't forget to use include guards in the header files.


The C++ standard makes no mention of .cpp and .h (except for when talking about C header files), and the standard library's headers are extension-less. Many C++ gurus even use .hpp instead of .h (.h is for C header files, you shouldn't also use it for C++ headers). .cpp is pretty standard on Windows, on some platforms .cc is the standard for C++ source files.

Share this post


Link to post
Share on other sites
You're right, I didn't mean 'standard' as 'part of the C++ standard', a much better word would've been 'commonly used'.

I guess it's rather pointless to argue about these things anyway, it's just the question of personal preference...

Strange that I haven't heard about '.cc' for C++ source files before, although I have programmed on different platforms before...

Share this post


Link to post
Share on other sites
It's really an organizational policy rather than a platform policy (that is dictated by the company/school/personal coding standards). Though it gets worse. On some platforms with case sensitive file names, .c can be a C file and .C can mean a C++ file.

Share this post


Link to post
Share on other sites
ah ok but am using linux. So whats the standard? I hate ms windows. I wouldn't develop on it unless I have to eg When am using VB.Net though I tend not to use VB. U see am using anjuta so I just went with what it's programmed to do.

Share this post


Link to post
Share on other sites
Quote:
Original post by amateur programmer
ah ok but am using linux. So whats the standard? ... U see am using anjuta so I just went with what it's programmed to do.

The problem is probably not related to your choice of extensions. It is probably related to how you are organizing your code. SiCrane provided a very helpful link above. You should read it if you haven't already. Here it is again: Organizing Code Files in C and C++

BTW, I've seen these extensions used:
  • source -- cpp, cc, cxx, C
  • headers -- h, hpp, H

Share this post


Link to post
Share on other sites

This topic is 4205 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.

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