I have my own class inside the file "Particles.h" and the class's implementation is inside "Particles.cpp"
I want the file "Load.h" to recognize my classes inside there, so I've added the line
#include "Particles.h"
and the file doesn't recognize it and in the past everything was OK (I haven't made any changes inside that class).
What should I do?
It sounds like your include path - the list of directories that the compiler scans in order to locate files that you #include - is set incorrectly. Which compiler are you using?
Well, if you listed your error codes, it might help. Off the top of my head, do you have something in Particles.h to make sure that the file is only included once? There are two methods of doing this. The first is to use #pragma once, but I think that might be Microsoft specific. The second is to use a #define.
Example:
#ifndef PARTICLES_H
#define PARTICLES_H
class CParticleWrapper
{
...
};
#endif
Also, unless you're deriving from a class in Particles.h or using an instance of a class instead of a pointer, you can use a forward declaration of the class and skip including the header file in a header file, which will save you compile time.
#ifndef LOAD_H
#define LOAD_H
class CParticleWrapper;
class CLoader
{
CParticleWrapper * m_pParticle;
public:
CLoader(CParticleWrapper * pParticle);
...
};
#endif
Then, in the Load.cpp, you would include the particle.h file.
make sure the file "Particles.cpp" has also included "Particles.h" to start with and the files are in the same folder and they are all part of the same project. it will help if you also share the error message that you are getting from your compiler.
Dev C++,It uses GCC,
The line is:
Stone *stone[48];
and it says: "expected constructor, destructor, or type conversion before '*' token ".
It sounds like you need to include the definition of the Stone class, but it would be impossible to say without more details. Can you narrow down the error by removing unrelated code and post that?
Related
I am instructed to code a Blackjack project for my college CMSC class. I have made all required source files but there is an error that I cannot figure out. I am using the terminal with a Makefile to compile my program.
When I compile my program I get this error in the terminal along with other warnings (I am not concerned about the warnings).
In file included from Blackjack.h:19:0,
from Proj2.cpp:12:
Player.h:17:3: error: ‘Hand’ does not name a type
Hand hand;
^
In file included from Blackjack.h:19:0,
from Blackjack.cpp:1:
Player.h:17:3: error: ‘Hand’ does not name a type
Hand hand;
Here is my source code in a Github repository.
https://github.com/Terrablezach/Blackjack
Can anyone tell me why the class 'Hand' does not name a type? I have included it in my header files where it needs to be included and I don't understand why it does not recognize it as a class.
Thank you in advance for your help.
You haven't included Hand.h in Player.h, so the definition of Hand is not available there.
Looking at the project brief you can change code, correct errors and include orders, however it states you cannot change the function declarations (this will be to limit the possible number of variations in code functionality I would suspect).
The function declaration is simply this line: Player(char *newName, int newFunds)
Looking at your code you are potentially going to run into problems with circular inclusion in your headers.
What you could do is wrap each header in a small piece of logic to prevent the same file from being included multiple times, for example add the lines
#pragma once
// the #pragma effectively does the same as the #ifndef/#define lines,
// its the equivalent of belt and braces if you use both
#ifndef HAND_H
#define HAND_H
//normal hand.h code in here
#endif
that way no matter how many times you call on the hand.h file you cannot end up with a multiply defined/included header. As a matter of rote I do that with all my header files whilst doing rapid development.
In regards specifically to the error Player.h:17:3: error: ‘Hand’ does not name a type
Hand hand; I suspect the previous comment in regards to the include order is correct, however I have no linux environment to hand, but will get back to you later tonight/tomorrow:)
The order of the #include declarations is incorrect.
Player class is dependant upon the declaration of Hand class. So in Blackjack.h the #include for Hand.h must go before the #include for Player.h
#ifndef BLACKJACK_H
#define BLACKJACK_H
#include <vector>
#include "Hand.h" // must be before Player.h include
#include "Player.h"
Alternatively, a forward declaration can be used in Player.h.
class Hand; // forward declaration of class Hand
class Player {
public:
Player();
Player(char *newName, int newFunds);
...
...
Hi I have what is hopefully a quick question. As part of homework I've been asked to write my own template vector class (most of the code is there it just needs to be expanded upon). While I understand HOW it works and WHAT it does I have no idea WHERE to put the code or reference it as I have never seen it in context within a program.
Do I create a new cpp file with all this information in it, or do I just add it in above my main method? If I create a new file (either cpp or h) how do I reference it, with just a #include like normal?
This might seem fairly simple but I've tried creating a new .h file and then including it in my main program but I always get scope definition errors.
Most compilers require you to put all the template code in a header file, rather than a source. This is due to the way template expansion works. You just include that header in whichever files need to use your vector class.
Some things to watch out for when creating a header:
Prevent multiple inclusion. If your compiler supports #pragma once you can put that at the top, otherwise use the #ifndef MY_HEADER_H ....... pattern.
Don't forget to put a semi-colon on the end of your class!!!!
Never put using namespace whatever; in the outer scope of a header (it's okay to use it within block scope such as namespace something { ... } or a function).
Be careful of name conflicts with std::vector if you are calling your class vector - make sure nobody has imported the std namespace prior to including your header.
One point you need to keep in mind is that you should place template declaration and definition together in the header file because of the compilation model of templates.
You can create a header file for the templated vector class and include this header file when you would like to use it in other .h or .cpp files.
You can also put them together inside main, but the previous option is better for you to maintain your code.
I'm using a helper class to log messages in the android ndk in an easy way. It works like that:
LOGE("ClassTag", "Message");
Since I don't want to write the tag manually every time I want to log something, I define a TAG constant for every class definition:
#define TAG "Class1Tag"
And then I can just log by doing:
LOGE(TAG, "Message");
The problem comes up when a class with the defined TAG constant includes another class which has the same TAG constant declared. Then the following compilation error pops up:
error: "TAG" redefined
How can I take rid of the redefinitions without having to use a different identifier for every #define?
It sounds like you are defining the TAG value in the header file. For this type of thing to work properly, you should only define it in the implementation file. Because the implementation file is not included in the other files, there will be no redefinition.
One implication of this is that logging statements can only occur in the implementation file.
Define them in the respective .cpp files instead of in the headers.
Or use a private, static, const std::string or array of char, which would let you use logging statements in the header while being invisible to other classes.
If you define the same identifier in several different header files, you are not likely to get the behavior you want. In a given implementation file, the value for the identifier is going to be the last one defined, not necessarily the one associated with the class. The "last one defined" will be the one in the last header file included in the implementation file, e.g.
a.h:
#define TAG "ClassA"
b.h:
#define TAG "ClassB"
a.cpp:
#include "a.h"
#include "b.h"
For this example, uses of TAG in a.cpp will have the value "ClassB".
Basically, you never want to re-define identifiers in your header files. If you define an identifier in an implementation file that is the same as one in another implementation file, that can work because it won't be visible when compiling the other implementation files. But the compiler is complaining for a reason, and you should heed what it is telling you to avoid confusion.
EDIT: I know your compiler is flagging it as an error; the upshot of what I said is don't relax your compiler complaints to accept what you have, because that will probably result in confusion.
why not use #under TAG before #define TAG "ClassTag"
Several questions has been asked related to this error, but each one of them practically relates to the object or type in question not declared before usage. For example:
class A
{
public:
A_Object a_obj;
};
Getting the error A_Object was not declared in this scope means A_object is not declared anywhere within the file.
NOTE: This is my understanding of the error.
Now I have a file called Account.h as shown below:
#ifndef ACCOUNT_H_
#define ACCOUNT_H_
class Account
{
//fields and methods
};
#endif /* ACCOUNT_H_ */
I also have a second file called Address.h as shown below:
#ifndef ADDRESS_H_
#define ADDRESS_H_
#include "Account.h"
typedef Account account_type;//Error here
class Address
{
//Fields and methods
};
#endif /* ADDRESS_H_ */
When I try to compile this file I get the error Account was not declared in this scope.
Any Ideas why?
Does Account.h actually also include Address.h? Such a circular reference seems the most likely situation.
Do you have a matching #endif at the end of both include files?
First point, your understanding about A_object is incorrect, the error means that A_object was not declared prior to it's first use, not that it wasn't declared anywhere.
Second point, the code you posted is incorrect, because you are missing #endif from both files. But assuming that was the only missing code then you would not get the error you describe. Post the real code that has the error.
I've seen this error when Address.h includes Account.h, which includes OtherFile.h, which includes Address.h. Is it possible you have a circular dependency? It may be hard to find.
This might be a case where a more core understanding of how the c/c++ compiler works would be in order. Include blocks, forward declarations, includes etc. All of these concepts did not make sense to me until I understood the basics of how compiler works. While I realize that this is somewhat of an oversimplification of compiler theory / logic, bear with me.
One of the first steps that a c++ compiler performs is a pre-processing (pre-compiler) step where it takes all of the files that are required, and combines them into one big flat file. In "C" languages, these pre-compiler operations a denoted using the hash (#) symbol. All an "#include" is doing, is directing the pre-compiler to bring this file into the entire "flat-file". If you have a cyclic include, your pre-compiler will get into an infinite loop and blow up, or say something super generic and useful like "the symbol has already been defined".
Include blocks, forward declarations, and all of the neat things that you are taught in c++ books that say "just do it, trust me" generally are helping you to avoid these type of compiling problems.
I was reading some code from the Doom 3 SDK ( in a VS solution ) when I found a header like this:
#ifndef __PLAYERICON_H__
#define __PLAYERICON_H__
class idPlayerIcon {
public:
idPlayerIcon();
~idPlayerIcon();
...... // omitted
public:
playerIconType_t iconType;
renderEntity_t renderEnt;
qhandle_t iconHandle;
};
#endif /* !_PLAYERICON_H_ */
The header has no forward class declaration nor #includes so, in my experience it should lead to an error like: Undeclared Identifier or Syntax error, cause renderEntity_t and qhandle_t are not "seen".
So how can this compile correctly?
Thank you in advance for the answers.
Because every time it is included, the needed entities are forward declared/included right before it, so everything is defined at the point of inclusion. As you correctly say, it will not work any other way.
I guess they include other headers before including this one.
Since this is a header file it's likely there is an order to their includes elsewhere (where this file is used perhaps?). As long as renderEntity_t and qhandle_t make it into the symbol table prior to this file being included it doesn't matter.
Is there something called stdafx.h? Most VS projects have these.
It would simply be a header that includes all the needed files for your application to reduce compile time from including headers.
So it would contain something like this:
#ifndef _STDAFX_H_
#define _STDAFX_H_
#include "playerIconAndOtherVariables.h"
#include "thatFileYouListed.h"
#endif