Solved

templates

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

760 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

18 Experts available now in Live!

Get 1:1 Help Now