Jump to content
  • Advertisement
Sign in to follow this  
Preacher

Templates Baby

This topic is 4772 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

Hi im trying to define a member function of a class a template function heres the class definition:
namespace bw1{

class Application
{
	public:
        
		// class destructor
		~Application();
		template<class T> void RegisterWindowApp(T& className);
		template<class T> void RegisterConsoleApp(T& classname);
		
		// function pointer 
		typedef int (*EntryPoint)(int, char**);
		static EntryPoint Run;
		static Application* TheApplication;
		
	protected:
        
        // class constructor
		Application();
		
};//end class Application
}// end namespace bw1

and have defined them as:
template<class T> void Application::RegisterConsoleApp(T& className)
{
    Application::Run = &ConsoleApplication::Run;
    TheApplication = new className;
}

i get the errors: * incomplete type `ConsoleApplication' used in nested name specifier *`className' is not a type was wondering if anyone could tell me what goin wrong as ConsoleApplication is defined within the bw1 namespace.

Share this post


Link to post
Share on other sites
Advertisement
yep the functions are in the application.cpp file which has using namespace bw1 at the start i have the classes forward declared as below in same file:

using namespace bw1;

class WindowApplication;
class ConsoleApplication;

Share this post


Link to post
Share on other sites
If you have a using directive it is not the same as being inside the namespace. A using directive merely makes the elements of the namespace visible in the namespace the using directive is made in. To truly be part of a namespace an symbol needs to be declared inside the namespace b1 { } brackets.

Also, you do know that declaring template definitions in a source file generally cause problems, right?

Share this post


Link to post
Share on other sites
ive read something about it alright, so what i should do is define the template functions in the header file.

and instead of using:

using namesoace bw1

use:

namespace bw1{

//class def

}

Share this post


Link to post
Share on other sites
Quote:
Original post by Preacher
i get the errors:

* incomplete type `ConsoleApplication' used in nested name specifier
*`className' is not a type

was wondering if anyone could tell me what goin wrong as ConsoleApplication is defined within the bw1 namespace.


Try:

namespace bw1
{
template<class T> void Application::RegisterConsoleApp(T& className)
{
Application::Run = &ConsoleApplication::Run;
TheApplication = new T();
}
} // namespace bw1



This will put the function definition into the same namespace as its declaration.

Where and how is ConsoleApplication defined ? Is it derived from Application ?
How is ConsoleApplication::Run defined ?


The second error -- "`className' is not a type" -- means, that you have to use a type in conjunction with new, not the name of a variable. So you can do "new T();" to create an instance of that type.

Share this post


Link to post
Share on other sites
In general template definitions go into headers, though some people define them in seperate files (usually named .inl though other extensions are common) that the header includes at the end.

But yeah, the template member function definition should go inside the same namespace as the template class' definition.

Share this post


Link to post
Share on other sites
Thats the type error sorted out thanks nmi to answe your questions

Application is the base class
WindowApplication extends Application
ConsoleApplication extends Application

heres the complete definiton for Application which is where run is defined. the template func are now within the class definition with the correct object
creation syntax. Im still getting the error
* application.h In member function
`void bw1::Application::RegisterWindowApp(T&)'


// Class automatically generated by Dev-C++ New Class wizard

#ifndef Application_H
#define Application_H

//#include "ConsoleApplication.h"
//#include "WindowApplication.h"

namespace bw1{

class ConsoleApplication;
class WindowApplication;

/**
* The Application Layer to interface with the native OS
*
*/

class Application
{
public:

// class destructor
~Application();
template<class T> void RegisterWindowApp(T& className)
{

Application::Run = &ConsoleApplication::Run;
TheApplication = new T();

}

template<class T> void RegisterConsoleApp(T& classname)
{

Application::Run = &WindowApplication::Run;
TheApplication = new T();

}

// function pointer
typedef int (*EntryPoint)(int, char**);
static EntryPoint Run;
static Application* TheApplication;

protected:

// class constructor
Application();

};//end class Application
}// end namespace bw1
#endif // APPLICATION_H



and heres ConsoleApplication


/**
* Encapsulates a console application
*
*
*/


#ifndef ConsoleApplication_h
#define ConsoleApplication_h

#include "Application.h"

namespace bw1{

class ConsoleApplication : public Application
{
public:

ConsoleApplication();
virtual ~ConsoleApplication();
static int Run (int iQuantity, char** apcArgument);

};// end class ConsoleApplication
}//end namespace bw1

#endif //ConsoleApplication_h




Share this post


Link to post
Share on other sites
sorry that exact error is

28 incomplete type `bw1::ConsoleApplication' used in nested name specifier

and its in the application.h file

Share this post


Link to post
Share on other sites
It's complaining about this line:

Application::Run = &ConsoleApplication::Run;

Because at that point the only thing the compiler has seen regarding ConsoleApplication is:

class ConsoleApplication;

And doesn't know anything about the insides of the class yet (like what ConsoleApplication::Run is).

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!