Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

templates

Posted on 2004-08-08
5
Medium Priority
?
360 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 150 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

715 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