Error when inheriting from a template class with multiple parameters - c++

Goal: To inherit from a class with 2 template parameters.
Error
Error error C2143: syntax error : missing ',' before '<'
The Code
template< typename Type >
class AssetManager : public IsDerivedFromBase< Type, Asset* >
{
// Class specific code here
};
Things you need to know: Asset is a class that just wraps a char* with a getter/setter and IsDerivedFromBase will be used to basically test wether or not Type is a derivative of Asset. These sets of classes are isolated in there own small scale visual Studio 2012 project and will be integrated into the main project once all the functionality has been tested thoroughly.
Some Edits based on Comments
Thank you for the help so far, I really appreciate it. Here are some more specifics:
IsDerivedFromBase.h
#ifndef ISDERIVEDFROMBASE_H
#define ISDERIVEDFROMBASE_H
#include< iostream > // For access to NULL
namespace Fenrir
{
template< typename Type, typename Base >
class IsDerivedFromBase
{
private:
static void Constraints( Type* _inheritedType )
{
Base* temp = NULL;
// Should throw compiler error if
// this assignment is not possible.
temp = _inheritedType;
}
protected:
IsDerivedFromBase( ) { void( *test )( Type* ) = Constraints; }
};
}
#endif
Note: This class was based off of a class I read about in an article. I have since found a better way to achieve the desired result; however, I wish to understand where this error is stemming from.
AssetManager.h"
#ifndef ASSETMANAGER_H
#define ASSETMANAGER_H
#include "IsDerivedFromBase.h"
namespace Fenrir
{
template< typename Type >
class AssetManager : public IsDerivedFromBase< Type, Asset* >
{
// Class specific code
};
}
#endif
Keeping class specific code to a minimum to keep this post as neat as possible, if more info is needed just let me know and I can add it in :).

That error message is common when the compiler encounters an identifier that it did not expect, so a first guess would be that IsDerivedFromBase is not known to the compiler at the time (maybe you did not include the appropriate header?). Alternatively, if IsDerivedFromBase is not a template, the compiler would also expect a , (or ;) after it.

Solved my issue which was interestingly silly. So due to a comment (from Jesse Good) I took a quick look at my includes. Since this was a small "whip it up quick" kind of project I didn't really pay much attention to them. I don't know exactly where the error was occuring, but I found out that AssetManager did not know about IsDerivedFromBase so I set up the following code block to just solve that issue without having to write a bunch of
#include statements everywhere!
// Contact point houses all of the include files
// for this project to keep everything in one place.
#ifndef CONTACTPOINT_H
#define CONTACTPOINT_H
#include "Asset.h"
#include "Sprite.h"
#include "IsDerivedFromBase.h"
#include "GenericManager.h"
#include "AssetManager.h"
#endif
Now I just include this in every header and all is well. I've been programming C++ for a little over a year now and never ran into this, a good lesson to learn for anyone new programming.
Thanks for your help everyone : )

Related

Inherit from Nested class inside inherited class

I have the kinda weird problem which is already described in nested class inherits from nested class, but i start to get a little bit desperate by now.
I have 4 classes in 2 different files (i don't think that really matters), I'll also cut out the non-important stuff from the classes for a better overview
Request.h (With class Request and BaseCtx)
#ifndef REQUEST_H
#define REQUEST_H
#include <string>
#include <map>
class Request
{
public:
Request() {};
Request(Request &orig) {};
class BaseCtx {};
~Request();
/* ... More methods and functions */
private:
/* ... More methods and functions */
};
#endif
Then there is my inherited class in p_TuStRequest.h (TuStRequest and TuStCtx)
#ifndef P_TU_ST_REQUEST_H
#define P_TU_ST_REQUEST_H
/*#include "Request.h"*/
class TuStRequest : public Request {
public:
TuStRequest() {};
TuStRequest(TuStRequest &orig);
TuStRequest(std::string requestData) {};
class TuStCtx : public Request::BaseCtx {
std::string teid;
};
private:
};
#endif
And my compiler (gcc version 4.2.4 (Gentoo 4.2.4-r01.16 p1.1) tells me the following:
p_TuStRequest.h:14: error: expected class-name before '{' token Makefile:431: recipe for target 'p_TuStController.o' failed
For the ones which are not that familiar with compiler versions, vers. 4.2.4 is a little bit before c++11 support (so basically C++98)
Is this some kind if problem because the compiler (which I can't upgrade) is that old and does not support this or did I do something wrong fundamentally?
If this really is a compiler problem, is there a way around it? If i remove the nested class in TuStRequest everything builds fine, but i would need a second structure to prevent the usage of voidpointers (I really don't want to use voidpointers for the context)
PS: Yeah i know, some stuff is still missing (Copy Constructor and so on..) but that shouldn't be the issue here.

Undeclared identifier error c++, What did I do wrong i have no clue?

I am getting this error when ever i run this project
6 error C2065: 'Engine_in':undeclared identifier
I really dont know what i have done wrong. Usually i can figure it out and know what i did wrong but the books i have dont go into depth on seperate file classes. I honestly do not know where the error is coming from. I have googled it but everyones problems are specific, so that is why i am resorting to asking you to solve my problems. I appologize in advance for me not knowing much.
I have this class 'Engine_debug.cpp'
//Engine Debugger
#include<iostream>
#include "Engine_debug.h"
#include "Engine_in.h"
using namespace std;
Engine_debug::Engine_debug()
{
Engine_in input;
}
Then i have this header 'engine_debug.h'
#ifndef Engine_debug_H
#define Engine_debug_H
class Engine_debug
{
public:
Engine_debug();
protected:
private:
}
#endif
I also have this class 'Engine_in.cpp'
//Engine input
#include<iostream>
#include<string>
#include "Engine_in.h"
using namespace std;
Engine_in::Engine_in()
{
}
string askYN(string question, int format)
{...working code}
And one more, the other header 'Engine_in.h'
#ifndef Engine_in_H
#define Engine_in_H
class Engine_in
{
public:
Engine_in();
std::string askYN(std::string question, int format = 0);
protected:
private:
};
#endif
If anyone knows what i did wrong and would like to explain to me, please do, thanks.
If it isn't a typo, you forgot to write class name while defining the member function.
string Engine_in::askYN(string question, int format)
// ^^^^^^^^^^ Missed during member function definition
Not sure if that causes the kind of error message the compiler is complaining about.
There is also a missing ; at the end of Engine_debug class definition. Credits Jesse.

VC++ compiler doesnt see any of my namespacec or classes

I'm working on a project in c++ and I stuck with no idea what is wrong. I've writen 4 classes and everything looked fine during work (under visual studio 2010). VS 'saw' all the definition, i could use auto-fill and sudgestions, but when I tried to compile the project it sudennly went blind. It's like I didnt include headers or something (which I did). The strange thing is there is no problem with working with those classes on VS (i can ctrl+space for hint, list of attributes and methods and all that stuff) but when i try to compile it says "ClassName" is not a type.
Quick sample of problem below:
ProButton.cpp:
#include "ProButton.h"
using namespace pGUI;
ProButton::ProButton( ... )
: ProControl( ... )
{
...
}
ProButton.h:
#ifndef __PRO_BUTTON__
#define __PRO_BUTTON__
#include <string>
#include "ProControl.h"
namespace pGUI
{
class ProButton :
public pGUI::ProControl
{
public:
//attributes
...
public:
//methods
...
};
}
#endif
but compiler says:
Error 291 error C2653: 'ProButton' : is not a class or namespace name
for this line in ProButton.cpp: ProButton::ProButton( ... )
It also says:
Error 23 error C2039: 'ProControl' : is not a member of 'pGUI'
Error 24 error C2504: 'ProControl' : base class undefined
and all similar errors for whole project. I have no idea what is wrong. Looks like my VS broke :D
Of course those (...) means there is code there, just not that important for now. I can upload all solution somewhere fi it will help.
edit//
About namespaces, all header files (classes declaration) are defined in namespace with:
namespace pGUI{
class ProClass
{
};
}
all definitions for these classes (in ProClass.cpp) are using:
using namespace pGUI;
at the beginning.
I think the problem is with order of including files.
Im not sure how this is supposed to be done. So far i have 4 classes that:
class ProGUI:
has a pointer to ProContainer
includes: ProContainer and ProControl
class ProContainer:
has pointers to: ProGUI and ProControl
class ProControl:
has a pointer to ProContainer
includes ProButton
is a base class for ProButton
class ProButton:
is a sub-class of ProControl
Those classes also uses irrlicht library and I'm not sure where to include it.
I had it included in my main file just before #include "ProGUI.h". This is also the only include in main. ProGUI.h .
//EDIT 2 -> solved
It was a problem with includes. I needed to rethink my inclusion order and add some forward declarations. Anyway that all seemed strange and took me a lot of time to figure i out. Thx for help. :)
It seems that you are using following statement:
using namespace pGUI;
Just before the class declaration:
class ProControl
{
};
Instead of using following approach:
namespace pGUI
{
class ProControl
{
};
}
The using namespace, as it says uses a namespace. You need to explicitly put something a namespace using namespace keyword followed by braces!
using namespace pGUI informs the compiler that it should look in the pGUI namespace to resolve existing names.
To declare or implement something in a namespace you need to be more specific. with either:
namespace pGUI
{
ProButton::ProButton( ... ) : ProControl( ... )
{
...
}
}
or:
pGUI::ProButton::ProButton( ... ) : ProControl( ... )
{
....
}
Personally, I consider any use of using namespace to be a lazy programmer hack that completely defeats the point of namespaces. But I digress. :)

C++ - "Unspecialised class template" error with shared_ptr

I have a class Room and it holds a vector of shared_ptrs to Option objects like so:
private:
vector<shared_ptr<Option> > options;
But for some reason when I build, I get the following errors:
'shared_ptr' : unspecialized class template can't be used as a template argument for template parameter '_Ty', expected a real type
'std::tr1::shared_ptr' : use of class template requires template argument list
Strangely, I also have a vector of shared_ptrs, exact same syntax but there's no problem with that one.
There's also a bunch of places that bring up the error "'Option': undeclared identifier", which brings me to think it might be a problem with the Option class, but it seems to be fine. Here's the code for Option:
Option.h:
#pragma once
#include "Room.h"
#include <memory>
using namespace std;
class Option
{
protected:
int id;
char* text;
public:
Option(void);
Option(int, char*);
virtual ~Option(void);
char* getText();
int getID();
};
Option.cpp:
#include "Option.h"
#include "Room.h"
#include <memory>
using namespace std;
Option::Option(void)
{
}
Option::Option(int newID, char* newText){
id = newID;
text = newText;
}
Option::~Option(void)
{
}
char* Option::getText(){
return text;
}
int Option::getID(){
return id;
}
There is a bit of conjecture in this answer since you haven't posted the code for the Room class. I'm assuming this code
private:
vector<shared_ptr<Option> > options;
is in Room.h. Your Option.h file includes Room.h, hence the Room class gets declared before the Option class. So Option is an incomplete type when the Room class' destructor is compiled and the shared_ptr implementation tries to delete the Option object.
From the code above, I don't see why Option.h needs to include Room.h, in fact, it should be the other way around. If it does indeed need to include the file, you should be able to work around the problem by explicitly declaring Room::~Room() out-of-line in Room.cpp.
EDIT:
Turns out ~shared_ptr<T> does not require T to be a complete type. However, shared_ptr<T>( T* ) and shared_ptr<T>::reset( T* ) do, and the problem may be because some operation on the vector is invoking a call to one of these (more likely the former).
vector<shared_ptr<Option >>
You almost did that right :)
vector<shared_ptr<Option> >
It's the two > characters that, when touching, cause the strange errors you see. It is being interpreted as the >> operator.
BTW, thank you for posting your code exactly as it is rather than typing it back in and possibly hiding the mistake.

Expected class name before ',' or ';'

I am getting an "Expected class-name before , or ; and I dont quite get how to fix it.
Here is where the error is:
class FXHost : public CPLAT::CP_Application, public CPLAT::CP_M_Listener
{
The file is FXHost.h and CPLAT:: is obviously a namespace where CP_Application and CP_M_Listener are.
I dont see why this would be wrong. This code ran fine in Metrowerks (without the CPLAT::) but in XCode CPLAT is needed due to the way the code was written by the previous developer.
Can anyone shed some light on this error?
UPDATE: Here is a sample of the CP_Application class
template <class DOC_POLICY, class PRINT_POLICY, class UNDO_POLICY>
class CP_EXPORT CP_Application : public CP_Application_Imp
{
public:
typedef DOC_POLICY DocPolicyType;
typedef PRINT_POLICY PrintPolicyType;
typedef UNDO_POLICY UndoPolicyType;
CP_Application();
virtual ~CP_Application() throw();
It looks like the compiler hasn't seen the class declaration for the two parent classes. The first thing I would check are your include directives. Are you sure you're including header which defines the classes CP_Application and CP_M_Listener?