Solved

templates

Posted on 2004-08-08
5
355 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now