Solved

templates

Posted on 2004-08-08
5
356 Views
Last Modified: 2010-04-01
For the following template I am getting as error
error C2954: template definitions cannot nest

I am not able to get it.

template <class T> int
 FlexArray<T>::put( T data )
 {
      if ( N == Max ) grow(); // Create more space if necessary.
      Data[N]= data;
      return N++; // Return subscript at which item was stored.
 }
0
Comment
Question by:rmvprasad
  • 2
  • 2
5 Comments
 
LVL 12

Expert Comment

by:andrewjb
ID: 11750657
You generally have to put the function definition in the .h along with the class

e.g

template <class T> FlexArray
{
  int put( T data ) { if (...) };
};
0
 
LVL 1

Expert Comment

by:ageraldnaveen
ID: 11752833
your code is not sufficient to comment on it..

share the code for FlexArray declaration also..

you shall declare the functions outside the class declaration like how you have done, but both of them is expected to be in the same header file.
0
 
LVL 30

Expert Comment

by:Axter
ID: 11761385
I recommend you leave the member function and it's implementation with in the class declaration.
It simplifies your code, and makes it easier for the compiler to accepted.  Especially if you're using a compiler like VC++ 6.0.

template <class T>
class FlexArray
{
 int put( T data )
 {
    if ( N == Max ) grow(); // Create more space if necessary.
    Data[N]= data;
    return N++; // Return subscript at which item was stored.
 }

};

The code needs to be in the header file any way, so you might as well put it in the class declaration.
0
 
LVL 1

Accepted Solution

by:
ageraldnaveen earned 50 total points
ID: 11763350
I get no compile/linker errors for this code.. is your code like this??

#include <iostream.h>

template <class T>
class FlexArray
{
public:
      int put(T data);
};

template <class T>
int FlexArray<T>::put(T data)
{
      return 10;
}

void main()
{
      FlexArray<int> f;
      f.put(10);
}
0
 
LVL 30

Expert Comment

by:Axter
ID: 11763861
FYI:
>>#include <iostream.h>

You should not use iostream.h
It's not part of the C++ standard, and therefor not portable.

Instead use extensionless version, which is part of the C++ standard.
#include <iostream>
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question