Solved

C++ class template problem

Posted on 2010-11-13
3
350 Views
Last Modified: 2012-05-10
I am experimenting with templates in C++ using Borland C++ Builder.
I have used STL quite successfully in the past and I have encountered a problem in using a simple class template that I have written myself.

The code snipplet shows my header file and the cpp file.
The compile-time error occurs when compiling the header file with the following error message:

[C++ Error] Unit1.h(25): E2293 ) expected
 

The error refers to the line of code where the x member is declared in the Form1 class.

If I move the declaration of x from the Form1 header to the initialisation code in the constructor for Form1 however, no error is reported. (As per the code that is shown that is commented out).

Is is possible to declare a variable x of type Vtor<int> in the header of Form1 with a size of 5 ?



// header file Unit1.h

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>

template <class T> class Vtor
{
   T *data;
   int size;
public:
   Vtor(int);
   ~Vtor( ) { delete[ ] data; }
   T& operator[ ] (int i) { return data[i]; }
};
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
private:	// User declarations
   Vtor<int> x(5);   // Generate a vtor to store five integers

public:		// User declarations
    __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

// cpp file Unit1.cpp
#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

template <class T> Vtor<T>::Vtor(int n)
{
   data = new T[n];
   size = n;
};

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
//  Vtor<int> x(5);
  for (int i = 0; i < 5; ++i)
      x[i] = i;              //  Initialize the vector.
}
//---------------------------------------------------------------------------

Open in new window

0
Comment
Question by:alcindor
[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
3 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 34127627
You can't initialize the data member there.

I assume you meant something like this (left out irrelevant stuff) :
class TForm1 : public TForm
{
private:
   Vtor<int> x;                      // <--- definition

public:
    TForm1() : x(5) { }              // <--- initialization via constructor
};

Open in new window

0
 
LVL 2

Author Closing Comment

by:alcindor
ID: 34150966
I have already accepted this solution. There is a fault on your web application ?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 34152726
It looks like it worked now. If you think there is a bug or other issue with the web site, you can report that by posting a question in the EE bugs zone (or the general Community Support zone).
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
How to convert MFC::CString to UTF8 wchar_t* 10 449
VS2015 Redefinition errors 4 102
C++ Code Issue 4 47
Need some help with design 17 46
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 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.

732 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