LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-21-2004, 05:13 PM   #1
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
C++ and templates


Ok, I am in the proccess of learning C++. I am trying to write my own List class. But whenever I try to compile, I get this error:

template.cpp:32: syntax error before `::' token

This is my first time trying to use templates, so I don't know whats wrong.

Here is the code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;

template <typename n3dListNodeElemType>
class n3dListNode
{
	public:
		n3dListNodeElemType *pData;
		n3dListNode<n3dListNodeElemType> *Last, *Next;
};

template <typename n3dListElemType>
class n3dList
{
	public:
		n3dList() { Nodes.pData=NULL; Nodes.Last=NULL; Nodes.Next=NULL; }
		int Add(n3dListNode<n3dListElemType> &Val);
		int Append(n3dListNode<n3dListElemType> &Val);
		int Prepend(n3dListNode<n3dListElemType> &Val);
		int Remove(unsigned long);
		int Remove(n3dListNode<n3dListElemType> &Val);
		int Insert(n3dListNode<n3dListElemType> &Val, unsigned long);
		int Clear();
		int Sort();

	private:
		n3dListNode<n3dListElemType> Nodes;
};

int n3dList::Insert(n3dListNode<n3dListElemType> &Val, unsigned long ulIndex)
{
	unsigned long ulI;
}

int main(int argc, char **argv)
{
	n3dList< int > MyList;

	printf("Hello world!!!\n");	
}
I also have some more questions. A template is basically like a user define right? So it is posible to go like this right?:

Code:
template <typename MyType>

void MyStrCopy(MyType &Str)
{
    //Copy string here
}
Of course this needs to be the member of some class, or does it? Can I call it like:

MyStrCopy(<string>MyStr);
?

Help! Explainations are good. Right now as far as I understand a template is a sort of #define, am I wrong?

Thanks for your time!
 
Old 06-21-2004, 06:19 PM   #2
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 48
Templates aren't like a define, there a lot more generic.

The reason for the syntax error is you need to provide the templat definition with the functions out side the class.

Code:
tempate <typename n3dListElemType>
int n3dList::Insert(n3dListNode<n3dListElemType> &Val, unsigned long ulIndex)
{
        unsigned long ulI;
}
The reason is for more complicated template definitions, but this is what your missing.

The thing with the string is not what templates are for. The list template is well defined in the STL, because its a good thing to use for templates, get to specific, and your using the wrong approach.

To learn more about templates this tutorial might be a good start.
 
Old 06-21-2004, 08:53 PM   #3
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Original Poster
Rep: Reputation: 32
Thanks for the help... although I still don't understand. That tutorial looks good, I'll give it a try. Thanks again.
 
Old 06-22-2004, 05:32 AM   #4
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
Code:
template <typename MyType>
void MyStrCopy(MyType &Str)
{
    //Copy string here
}
In this example, I think you need to clarify what you want the template for ...

You can have template classes (like your list) and template functions (a little like a macro but with all the virtues of a 'real' function).

It seems that you are defining a template function to copy a string - what you should think about is a copy function that copies any object that can be traversed, like your list class (this also exists already but ignore that for now).

So your string class could provide a function 'begin' which gives access the first character, and a function 'end' which somehow indicates the last character has been reached, and your copy be performed on each character in between the two points.

Moving on to your list class, why not have that provide the same 'begin' and 'end' functions, then you can use your same copy function to copy lists. Maybe an array class would be a good idea and if you give that 'begin' and 'end' functions then ... etc.

'begin' and 'end' functionas are not the only option either - if your container class always go from 0 to length(), then you could use that functionality.

(Lots of these features are already covered by the standard library, often referred to as the standard template library, or STL, but you will see all that later - I am just trying to point you to some examples and I am sure you pick it up pretty quickly)

wrt instantiation, you basically think about what information is available to the compiler - you have to provide the rest:
Code:
template<typename S>
S copy(const S& s)
{
  S new_s;
  // i would be something called an 'iterator' here
  // this code is not real, just to illustrate
  for (i = s.start(); i!=s.end(); i++) new_s[i] = s[i];
  return new_s;
}
template<typename To, typename From>
To change(const From& f)
{
  return (To)f;
}
int main()
{
  string s;
  string another_s = copy(s); 
  /* here the compiler can tell that copy<string> will be invoked because it
      the type of s is given */

  rope r = change<rope>(s);
  /* now we need to tell the compiler what version of change to instantiate.
     the 'From' type is implicit but the 'To' type must be explicitly stated */
}
HTH
 
Old 06-23-2004, 01:15 PM   #5
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Original Poster
Rep: Reputation: 32
That wasn't a real example (the sting copy thing), and I wouldn't ever make a function like that. I just used it to explain what I was thinking about templates.

Thanks guys! After that tutorial and some trial and error, I think I am well on the way to programming with templates! =)
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Impress Templates logosys Linux - Software 1 09-13-2005 10:33 AM
Need help with templates ........ max_rsr Programming 4 04-22-2005 12:53 PM
problem with templates please help..... max_rsr Linux - Newbie 1 04-13-2005 06:51 PM
c++ templates ashirazi Programming 5 07-30-2004 05:17 AM
Templates MGLindsey Linux - Networking 0 07-02-2002 11:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration