Good day all,
I'm trying to create a template class just for learning purposes. And i keep coming up with two errors... Here's the script:

HEADER FILE "lottoclass.h"

#ifndef LOTTOCLASS_H_INCLUDED
#define LOTTOCLASS_H_INCLUDED

using namespace std;
template<typename T>
class Lottery
{
public:
 Lottery();
 ~Lottery();
 T Resize(T* array, int i);
 void addVar(int b, T b2);

private:
 T* array2;
 int arraySize;
}

template<typename T>
Lottery<T>Lottery()
{
array2=0;
arraySize=0;
}

template<typename T>
T Lottery<T>::Resize(T* array, int i)
{
T* arr3= new T [i];
delete[] array;
return array3;
}

template<typename T>
Lottery<T>::~Lottery()
{
delete[] array2;
array2=0;
}

template<typename T>
void Lottery<T>::addVar(int b, T b2)
{
 array2[b]= b2;
}

And here's the int main();

#include <iostream>
#include <cstdlib>
#include <ctime>
#include "numonly.h"
#include <string>
#include "LottoClass.h"

int main()
{
//Setting up variables//
string n;
int n1, b2;
cout<<"Welcome to R-Lot!"<< endl;
//Continue a loop until user inserts correct answer//

while (true)
{
cout<<"R-Lot for Numbers (1) or Names (2)";
n1=stringtonumber(EnterOnlyNumbers());

if (n1 == 1 || n1 == 2)
break;
}

if (n1 == 1)
{
  int i, i2;
  i=i2=0;
  cout<<"How many units?";
  b2=stringtonumber(EnterOnlyNumbers());
  Lottery<int>Lotto();
  Lotto.Resize(array2, b2);
  
  while (i<b2)
  {
     cout<<"What will [ " <<i<<" ] be? ";
     i2=stringtonumber(EnterOnlyNumbers());
     Lotto.addVar(i,i2)
     cout<<"[ "<<i<<" ] = "<<i2<<endl;
     i++;
  }
}
return 0;
}

Ok, first error is in the main(), when the compiler runs Lotto.Resize(array2, b2); it says:
error: request for member 'Resize' in 'Lotto', which of non-class type 'Lottery<int>()'
error: 'array2' was not declared in this scope
error: request for member 'addVar' in 'Lotto', which of non-class type 'Lottery<int>()'


Can someone please help me resolve these errors... Array2 is clearly defined as a private variable, and in the constructor...
Thanks alot

Lottery<int>Lotto();

Welcome to C++, where syntactic ambiguity means you declared a function rather than an object. Remove the parens from this declaration and Lotto will become what you intended it to be:

Lottery<int> Lotto;

And array2 isn't visible in main(), it's a private member of the Lottery class.

So how do I create a constructor in this template class to declare array2 as 0, and arraySize as 0?

So how do I create a constructor in this template class to declare array2 as 0, and arraySize as 0?

You do nothing, because that constructor already exists...

Thanks

Ok but now, its saying with the Resize function:
error: invalid conversion from 'int' to 'int*' can you tell me why?

I think you have many problems with your Resize method:

template<typename T>
T Lottery<T>::Resize(T* array, int i)
{
    T* arr3= new T [i];
    delete[] array;
    return array3;
}

You have (1) dynamically allocated a new array of i objects of type T, and done nothing with it, (2) deleted the array that you passed in, for no obvious reason, and without doing anything with it, and (3) returned a variable that isn't defined.

So I think that needs work before you get too far. Once you determine what you need to pass in to a Resize method, vs. what information the class instance already has, I think you'll find that the error message becomes a non-issue (though if you really want to know, it's because the first variable you're passing into the method is an integer, when it's expecting and array-of-integers).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.