Solved

templates

Posted on 2004-08-08
5
358 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

734 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