Seriously!

Go Back   Seriously! Forums > Serious Sam Series > Serious Sam Classic > Serious Sam Classic Workshop > Serious Sam Classic SDK

Serious Sam Classic SDK Discuss coding ideas & problems or show tutorials and code snippets.

Reply
 
Thread Tools Search this Thread Rate Thread Display Modes
Old 01-21-2002, 02:03 PM   #1   Add To Ignore List  
Fluffy Teletubby
 
Iirion Claus's Avatar
 
Joined: Apr 2001
Location: Israel
 
Iirion Claus is offline
Default Off Topic: Using template classes on C++

Yeps, it's Iirion's off topic coding questions again!!

Does someone know how to build a template class with C++? To be more specific, I'm trying to build a general linked list class, but constantly get error messages, and couldn't find any good web resources on this subject... Does someone has any experiance with it? (And patience to answer a couple of emails )
  Reply With Quote
Old 01-21-2002, 02:49 PM   #2   Add To Ignore List  
Your ad here.
 
Entroper's Avatar
 
Joined: Mar 2001
 
Entroper is offline
Default

Sure, fire away. I suffered through most of the usual template errors when I learned, so I'll probably be able to spot what's up. Just gimme what MSVC tells you and maybe a snippet or two.
__________________
Entroper
https://www.twitch.tv/entroperzero
https://www.youtube.com/channel/UCUI...Fx5PjTSXHRf2yg
Virus and antivirus free since 1999.

If at first you don't succeed, you fail.
  Reply With Quote
Old 01-21-2002, 04:51 PM   #3   Add To Ignore List  
Zip
Werebull
 
Joined: Apr 2001
 
Zip is offline
Default

Same as E, fire away
  Reply With Quote
Old 01-21-2002, 05:45 PM   #4   Add To Ignore List  
Spy Killah
 
Rodzilla's Avatar
 
Seriously! Reaper
Joined: May 2001
 
Rodzilla is offline
Default

Here's an example of a templated function. Classes work the same way:

Code:
template <class itemType>

void Swap(itemType& lhs, itemType& rhs)
{
    itemType temp = rhs;
    rhs = lhs;
    lhs = temp;
}
When you make a templated class however, do NOT include the implementation file in the project. If you do, you'll get link errors.
__________________
Rodzilla
-Seriously! Administrator Emeritus
  Reply With Quote
Old 01-21-2002, 06:40 PM   #5   Add To Ignore List  
untitled.
 
Hollywood's Avatar
 
Joined: Aug 2001
Location: Australia
 
Hollywood is offline Send a message via MSN to Hollywood
Default Re: Off Topic: Using template classes on C++

Quote:
Originally posted by Iirion Claus
I'm trying to build a general linked list class
just use stl

#include <list>

or .h which ever it is

but templates and stl will increase your executable size. I heard of a 20 meg EXE cuz they templated EVERYTHING, crazy and insane!!!! and sloppy coders!
__________________
Tres.
  Reply With Quote
Old 01-21-2002, 08:21 PM   #6   Add To Ignore List  
Your ad here.
 
Entroper's Avatar
 
Joined: Mar 2001
 
Entroper is offline
Default

Templates can actually decrease your executable size when you don't run wild with them. When you create templated classes and functions, the code isn't compiled until it's used. So if you need a linked list for two different data types, and you don't use every function, it will compile less code than writing two separate linked lists.
__________________
Entroper
https://www.twitch.tv/entroperzero
https://www.youtube.com/channel/UCUI...Fx5PjTSXHRf2yg
Virus and antivirus free since 1999.

If at first you don't succeed, you fail.
  Reply With Quote
Old 01-22-2002, 05:36 AM   #7   Add To Ignore List  
Fluffy Teletubby
 
Iirion Claus's Avatar
 
Joined: Apr 2001
Location: Israel
 
Iirion Claus is offline
Default

Okay, the most basic example. I'm trying to learn here, y'know:

CArray.h:
Code:
#include <stdlib.h>

template <class T>
class CArray30
{
private:
	T* arr;

public:
	CArray30();
	~CArray30();

	T* Get(int i);
};
CArray.cpp:
Code:
#include "CArray.h"

template <class T>
CArray30<T>::CArray30()
{
	arr= new arr[30];
}

template <class T>
CArray30<T>::~CArray30()
{
	delete(arr);
}

template <class T>
T* CArray30<T>::Get(int i)
{
	return arr+i;
}
main.cpp:
Code:
#include "CArray.h"
#include <stdio.h>

void main(void)
{
	CArray30<int> our();

	printf("%d\n", our.Get(3));
}

When trying to compile, I get the following error:
left of '.Get' must have class/struct/union type
But "our" is a class, it's my template class! So I have no idea what I did wrong (when removing paranthesis after the our declaration I get linking errors, so that's not it...).
  Reply With Quote
Old 01-24-2002, 07:36 AM   #8   Add To Ignore List  
Fluffy Teletubby
 
Iirion Claus's Avatar
 
Joined: Apr 2001
Location: Israel
 
Iirion Claus is offline
Default

Entroper? Zippo? Someone??
  Reply With Quote
Old 01-25-2002, 09:10 AM   #9   Add To Ignore List  
****** Games
 
PainBringer's Avatar
 
Joined: Apr 2001
Location: The Netherlands
 
PainBringer is offline
Default

Hi Irion,

I've twiddled your code around somewhat :

Code:
#include <stdlib.h>
#include <stdio.h>

template <class T> class CArray30
{
public:
	CArray30();
	~CArray30();

	T *Get(int i);
private:
	T arr[30];
};

template <class T>
CArray30<T>::CArray30()
{
};

template <class T>
CArray30<T>::~CArray30()
{
};

template <class T>
T *CArray30<T>::Get(int i)
{
	return arr+i;
}

void main(void)
{
	CArray30<int> *our = new CArray30<int>();
	printf("%d\n", (*our).Get(3));
	delete our;
}
It is not entirely clear to me what you want to do, because if
you want to make an array of ints or any other T you might
come up with, you may as well use <vector> or even <list>
in the STL libs, but for the sake of argument : this snippet works.

Notice the difference in instantialization (= new CArray()) etc. as
well as the in-class declaration and allocation of the 30 T's
I've not put in any initialization, but the mem used for arr[] is
allocated at instantiation, containing rubbish values.

You could rewrite that ofcourse to a (T*)malloc(sizeof(T)*30); thing
and even make that variable by making it CArrayVariable(int iSize)
and using iSize in the malloc, etc. but the main thing wrong with
your own code was the arr = new arr[30]; statement.

That just didn't make sense. It compiled because, as you said, it is
only compiled and checked when used and you didn't use the
constructor (properly). Unless ofcourse this is some method of
array declaration I've never seen before, which is entirely possible.

The (*our)-> thing instead of our. adressing method also made
a big difference since you directly use the reference to the class,
not the class itself. I'm not too clear about the exact reason behind
this (still a little fuzzy on templates as well), so bear with me

HTH!

-- PainBringer

Last edited by PainBringer; 01-25-2002 at 09:23 AM.
  Reply With Quote
Old 01-25-2002, 11:53 AM   #10   Add To Ignore List  
Fluffy Teletubby
 
Iirion Claus's Avatar
 
Joined: Apr 2001
Location: Israel
 
Iirion Claus is offline
Default

I don't get it... It works fine when in a single file, but when splitting for headers and .cpp files I get linking errors for all of the class' functions!
  Reply With Quote
Old 01-27-2002, 04:36 PM   #11   Add To Ignore List  
Fluffy Teletubby
 
Iirion Claus's Avatar
 
Joined: Apr 2001
Location: Israel
 
Iirion Claus is offline
Default

-= bump =-
  Reply With Quote
Old 01-27-2002, 05:10 PM   #12   Add To Ignore List  
Anime Freak
 
Bwian's Avatar
 
Joined: Mar 2001
Location: Wandering Japan
 
Bwian is offline Send a message via AIM to Bwian
Default

Perhaps you've got problems with multiple #include's?

In your example both main.cpp and CArray.cpp are #include'ing CArray.h.

I'd also suggest making the class less restrictive in terms of the array size, and just determine that upon creation of member objects.
__________________
Bwian
Master of the Hiten Mitsurugi Ryu!
"I'm not playing games, I'm doing R&D!"
  Reply With Quote
Old 01-27-2002, 08:10 PM   #13   Add To Ignore List  
Your ad here.
 
Entroper's Avatar
 
Joined: Mar 2001
 
Entroper is offline
Default

Iirion, referring back to your initial example:

I think what you want in the constructor is to say new T[30], not new arr[30]. 'arr' is the variable name, and the new operator expects a type, not a variable. As far as the linking errors go, this is one of those quirks of C++. When using templates, the implementation has to be with the class definition in the .h file. Separate compilation won't work. The reason for this goes back to the fact that the code is only compiled as it's needed, so you can't 'compile' the .cpp file into its own module, which is what MSVC will try to do if you separate it into a .h and .cpp. There are ways around this, but I don't know any of them offhand. It's easier to place your implementation in the .h file when you use templates and just not worry about it.
__________________
Entroper
https://www.twitch.tv/entroperzero
https://www.youtube.com/channel/UCUI...Fx5PjTSXHRf2yg
Virus and antivirus free since 1999.

If at first you don't succeed, you fail.
  Reply With Quote
Old 01-28-2002, 05:40 AM   #14   Add To Ignore List  
****** Games
 
PainBringer's Avatar
 
Joined: Apr 2001
Location: The Netherlands
 
PainBringer is offline
Default

Getting link-errors? That really doesn't make sense. An include is nothing more than 'paste the stuff from file_something.h here before compiling'

Only thing that could mean is that you don't include in the right order OR you include stuff recursively.

Ofcourse as Entroper also says : templates have their implementation and declaration in the same file usually. I think this glitch is a new one (nothing to do with the one you got at the start of the thread), but to be sure you don't get recursive stuff when including (redefinition type errors) I always do this :

Code:
#ifndef __ARRAY30_H__
#define __ARRAY30_H__

// MSVC++ SPECIFIC
#pragma once
// end MSVC++ SPECIFIC

< ... Array 30 declaration stuff here ... >

#endif
Good practice to prevent redefinition mistakes
HTH

(i love these new smileys, just the thing to get you going on a monday morning)

Last edited by PainBringer; 01-28-2002 at 05:46 AM.
  Reply With Quote
Old 02-01-2002, 10:03 AM   #15   Add To Ignore List  
Gnaar
 
Joined: Sep 2001
 
Vlummi is offline
Default

hi irion,

there currently is NO c++ compiler that is able to let you have the implementation in a separate .cpp - file.

you MUST have the implementation INLINE, in your .h - file.

the reason is the following:

c++ templates are a compile time mechanism; there is NO runtime overhead. if you instantiate a template, e.g. something like "list<int> myIntList;", the compiler takes the code from the list.h - file (which you of course have to #include), and "replaces" all occurences of "T" (with std::list<T>) with "int", because you wanted to instantiate an "int" list when you said "list<int> myIntList;"

the c++ standard says that it must be possible to declare a template function or class as "export", which would make it possible to have the implementation in a .cpp file.
but, as i said, i don't know of any compiler that is able to do that.

think about how you would do that as a compiler builder - to instantiate code out of .obj files. would be pretty difficult.

i hope i expressed myself clearly. if not, just ask again and again, i'm sure i can help you
  Reply With Quote
Old 02-02-2002, 12:59 AM   #16   Add To Ignore List  
Fluffy Teletubby
 
Iirion Claus's Avatar
 
Joined: Apr 2001
Location: Israel
 
Iirion Claus is offline
Lightbulb

Ahhh, finally, a clear solution Thanks for your help.
  Reply With Quote
Old 02-02-2002, 12:17 PM   #17   Add To Ignore List  
Your ad here.
 
Entroper's Avatar
 
Joined: Mar 2001
 
Entroper is offline
Default

Quote:
Originally posted by PainBringer
Getting link-errors? That really doesn't make sense. An include is nothing more than 'paste the stuff from file_something.h here before compiling'

Only thing that could mean is that you don't include in the right order OR you include stuff recursively.
The problem is that you can't compile a template. When you make a non-templated class, you specify all of the data types, and it can compile all of the code since it knows how much space will be needed for the data and such. Templates by definition don't specify the data type. You can't compile code for them because you don't know how much memory to allocate for undefined data types. That's why you put the implementation inside the header file. When you use the templated class, like CArray30<int>, you've just specified the data type, so it compiles the code for that data type. If you use the class with multiple data types (which you probably will, or else there's no point making it a template ), it compiles multiple versions of your template code.
__________________
Entroper
https://www.twitch.tv/entroperzero
https://www.youtube.com/channel/UCUI...Fx5PjTSXHRf2yg
Virus and antivirus free since 1999.

If at first you don't succeed, you fail.
  Reply With Quote
Old 02-04-2002, 06:20 AM   #18   Add To Ignore List  
****** Games
 
PainBringer's Avatar
 
Joined: Apr 2001
Location: The Netherlands
 
PainBringer is offline
Smile

It's always great to see that all the bits and pieces of knowledge finally mesh together into a solution

You gotta love these boards.
  Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help me think of a topic! MADMAN360 Off Topic Forum 4 01-06-2004 10:21 PM
Classes (.ECL) That Cannot Be Added To The Editor's Virtual Tree? Michael Harris Serious Sam Classic SDK 17 03-24-2003 09:30 AM
PHP Classes Divx Off Topic Forum 7 09-29-2002 04:58 AM
adding custom classes to serious sam remybar Serious Sam Classic SDK 6 02-08-2002 07:44 AM


All times are GMT -4. The time now is 11:01 AM.