How do I pass a templated class to the constructor of another class? I am trying to pass a templated hash table class to a menu class which will allow me to then allow the user to decide the type of the hash table.
template <class T>
class OpenHash
{
private:
vector <T> hashTab;
vector <int> emptyCheck;
int hashF(string);
int hashF(int);
int hashF(double);
int hashF(float);
int hashF(char);
public:
OpenHash(int);
int getVectorCap();
int addRecord (T);
int sizeHash();
int find(T);
int printHash();
int deleteEntry(T);
};
template <class T>
OpenHash<T>::OpenHash(int vecSize)
{
hashTab.clear();
hashTab.resize(vecSize);
emptyCheck.resize(vecSize);
for (int i=0; i < emptyCheck.capacity(); i++)
{
emptyCheck.at(i) = 0;
}
}
So I have this class Open hash that is templated, because it supposed to allow for any type to be added, I have this working if initiate a object of it in my main and change input types etc.
int main ()
{
cout << "Please input the size of your HashTable" << endl;
int vecSize = 0;
cin >> vecSize;
cout << "Please select the type of you hash table integer, string, float, "
"double or char." << endl;
bool typeChosen = false;
string typeChoice;
cin >> typeChoice;
while (typeChosen == false)
{
if (typeChoice == "int" || "integer" || "i")
{
OpenHash<int> newTable(vecSize);
typeChosen = true;
}
else if (typeChoice == "string" || "s")
{
OpenHash<string> newTable(vecSize);
hashMenu<OpenHash> menu(newTable);
typeChosen = true;
}
else if (typeChoice == "float" || "f")
{
OpenHash<float> newTable(vecSize);
typeChosen = true;
}
else if (typeChoice == "double" || "d")
{
OpenHash<double> newTable(vecSize);
typeChosen = true;
}
else if (typeChoice == "char" || "c" || "character")
{
OpenHash<char> newTable(vecSize);
typeChosen = true;
}
else
{
cout << "Incorrect type";
}
}
return 0;
}
In my main I want to ask the user what type they which to make the hash table. depending what they enter it should create a instance of this class with the type they want and then pass this to another class called menu which should allow them to call functions from the hash class.
You can use:
class Ctor {
public:
Ctor(const Other<int>&); // if you know the specific type
};
or:
class Ctor {
public:
template<class T>
Ctor(const Other<T>&); // if you don't know the specific type
};
Live demo
Related
I wrote a working, linked-queue that is templated on it's datatype, however the user may be inputing data in one of a few different types. How can I select which datatype will be used at run-time?
It works fine if I use each type individually; I just need every possibility covered without changing code or re-writing overloaded functions for every data type.
Below I've provided the relevant pieces of my code. As I said, no problems with my class member functions.
I already have tried a switch statement that creates x type version of the queue, but that cannot work as later possibilities within the switch "contradict" with other queue data types. I am currently trying if/else if statements, with no errors other than when I try to use the input of x type, it says it is undefined.
// From Source.cpp
#include <iostream>
#include <string>
using namespace std;
#include "LQueue.h"
int mainMenu();
int main()
{
int value;
bool stop = false;
Queue<int> *theQueue;
int choice = mainMenu();
if (choice == 1) {
Queue<int> theQueue;
int dataType;
}
else if (choice == 2) {
Queue<double> theQueue;
double dataType;
}
else if (choice == 3) {
Queue<string> theQueue;
string dataType;
}
else if (choice == 4) {
Queue<char> theQueue;
char dataType;
}
cout << "\n\nHow many items would you like to initially"
<< " populate the queue with? ";
int howMany;
cin >> howMany;
for (int i = 0; i < howMany; i++)
{
cin >> dataType;
theQueue.enqueue(dataType)
}
theQueue.display(cout);
theQueue.dequeue();
theQueue.display(cout);
return 0;
}
int mainMenu()
{
int choice;
cout << "What type of data will you be storing in the queue?\n"
<< "1. integers\n2. decimal numbers\n3. words\n4. chars\n\n";
cin >> choice;
if (choice > 0 && choice < 5)
return choice;
cout << "\n\nInvalid choice\n\n";
mainMenu();
}
// Guess I'll include shown functions from the Queue class file below
//--- Definition of enqueue()
template <typename QueueElement>
void Queue<QueueElement>::enqueue(const QueueElement & value)
{
if (empty())
{
myFront = myBack = new Node(value);
}
else
{
myBack->next = new Node(value);
myBack = myBack->next;
}
}
//--- Definition of dequeue()
template <typename QueueElement>
void Queue<QueueElement>::dequeue()
{
if (empty() == false)
{
Queue::NodePointer oldFront = myFront;
myFront = myFront->next;
delete oldFront;
}
}
//--- Definition of display()
template <typename QueueElement>
void Queue<QueueElement>::display(ostream & out) const
{
Queue::NodePointer ptr;
for (ptr = myFront; ptr != 0; ptr = ptr->next)
out << ptr->data << " ";
out << endl;
}
//--- Definition of front()
template <typename QueueElement>
QueueElement Queue<QueueElement>::front() const
{
if (!empty())
return (myFront->data);
else
{
cerr << "*** Queue is empty "
" -- returning garbage ***\n";
QueueElement * temp = new(QueueElement);
QueueElement garbage = *temp; // "Garbage" value
delete temp;
return garbage;
}
}
Compiler (visual studio 2017) is showing identifier "dataType" is undefined within the following loop:
```c++
for (int i = 0; i < howMany; i++)
{
cin >> dataType;
theQueue.enqueue(dataType);
}
2 errors: E0020 and C2065 on the "cin >> dataType;" line, and also another
C2065 on the next line
Maybe there is a more efficient way overall of doing this? I am open to any and all suggestions, thank you!
The problem (a problem) is that when you write
if (choice == 1) {
Queue<int> theQueue;
int dataType;
}
else if (choice == 2) {
Queue<double> theQueue;
double dataType;
}
else if (choice == 3) {
Queue<string> theQueue;
string dataType;
}
else if (choice == 4) {
Queue<char> theQueue;
char dataType;
}
you define four different theQueue and four different dataType variables that are, each one, valid only inside the corresponding body of the corresponding if.
So, when you write
for (int i = 0; i < howMany; i++)
{
cin >> dataType;
theQueue.enqueue(dataType)
}
theQueue.display(cout);
theQueue.dequeue();
theQueue.display(cout);
there are no more dataType and theQueue available (all of they are out of scope).
I suggest something as follows
if (choice == 1) {
foo<int>();
}
else if (choice == 2) {
foo<double>();
}
else if (choice == 3) {
foo<std::string>();
}
else if (choice == 4) {
foo<char>();
}
where foo() is a template function almost like this (caution: code not tested)
template <typename T>
void foo ()
{
Queue<T> theQueue;
T dataType;
std::cout << "\n\nHow many items would you like to initially"
<< " populate the queue with? ";
int howMany;
std::cin >> howMany;
for (int i = 0; i < howMany; i++)
{
std::cin >> dataType;
theQueue.enqueue(dataType)
}
theQueue.display(cout);
theQueue.dequeue();
theQueue.display(cout);
}
Write a templated member function that does what you want:
template<class DataType>
void processInput(int howMany) {
DataType value;
for (int i = 0; i < howMany; i++)
{
cin >> value;
theQueue.enqueue(value);
}
theQueue.display(cout);
theQueue.dequeue();
theQueue.display(cout);
}
Method 1 - switch statement
We can then use a switch statement to select between them in main:
int main()
{
int choice = mainMenu();
cout << "\n\nHow many items would you like to initially "
"populate the queue with? ";
int howMany;
cin >> howMany;
switch(choice) {
case 1:
processInput<int>(howMany);
break;
case 2:
processInput<double>(howMany);
break;
case 3:
processInput<string>(howMany);
break;
case 4:
processInput<char>(howMany);
break;
}
}
Method 2 - array of methods
We can use an array to do a lookup!
using func_t = void(*)(int);
int main() {
std::vector<func_t> options = {
processInput<int>,
processInput<double>,
processInput<string>,
processInput<char>
};
int choice = mainMenu();
func_t selectedOption = options[choice - 1];
cout << "\n\nHow many items would you like to initially "
"populate the queue with? ";
int howMany;
cin >> howMany;
selectedOption(howMany);
}
I'm new to programming in C++. Also new to implementing stacks. My objective is creating RPN Calculator using template stack. Cant use the built in stack classes.
I have everything so far and now I am stuck, I can't think of how to fix this problem. I am currently getting these errors:
Error C2109 subscript requires array or pointer type
Warning C4244 'return': conversion from 'double' to 'int', possible loss of data
This is my stack class:
#include<stack>
#define STACK_MAX 500
template<class T>
class RPNCalculator
{
private:
//Insanciating stack class
T data[STACK_MAX];
int size;
//stack<T> rpnstack;
public:
RPNCalculator() {
size = 0;
}
~RPNCalculator();
int Top() {
if (size == 0) {
fprintf(stderr, "Error: stack empty\n");
return -1;
}
return data[size - 1];
}
void push(T data); // pushes a new operand onto the stack
// the following operations are to be performed as defined for Reverse Polish Notation
// binary operators:
T value(); // returns the topmost value
void pop(); // returns the topmost value and pops it off the top
double add();
double subtract();
double multiply();
double divide();
// unary operators:
double square(); // squares the current value
double negate(); // negates, i.e. 3 becomes -3
bool isEmpty(); // tests to see if there are elements on the stack
void clear(); // clears out the stack
};
template<class T>
inline bool RPNCalculator<T>::isEmpty()
{
bool status;
if (!top)
status = true;
else
status = false;
return status;
}
template<class T>
void RPNCalculator<T>::clear()
{
}
template<class T>
inline RPNCalculator<T>::~RPNCalculator()
{
}
template<class T>
inline void RPNCalculator<T>::push(T data)
{
if (size < STACK_MAX)
data[size++] = data;
else
fprintf(stderr, "Error: stack full\n");
}
template<class T>
inline T RPNCalculator<T>::value()
{
return T();
}
template<class T>
inline void RPNCalculator<T>::pop()
{
if (size == 0)
fprintf(stderr, "Error: stack empty\n");
else
size--;
}
This is my main class:
#include <iostream>
#include "RPNCalculator.h"
#include <string>
#include <sstream>
using namespace std;
bool isOperator(const string& input);
void performOperation(const string& st, RPNCalculator<double>& rpnstack);
int main() {
cout << "Welcome to the RPN Calculator by AbdulFatai Saliu __D00168401" << endl;
cout << "Enter c to clear \n"
<< "s to square \n"
<< "n to negate \n"
<< "p to pop current value \n"
<< "q to quit \n"
;
RPNCalculator<double> rnpstack;
string input;
while (true) {
//Dispaly prompt
cout << ">> ";
//get user input
cin >> input;
//check for numeric values
double numereric;
if (istringstream(input) >> numereric) {
}
else if (isOperator(input)) {
}
else if (input == "q") {
return 0;
}
else {
cout << "Input Not Valid" << endl;
}
//check for operators
//check for exit
// display invalid value message
}
system("PAUSE");
//return 0;
}
bool isOperator(const string& input) {
string operators[] = { "-","+","*","/"};
for (int i = 0; i < 6; i++) {
if (input == operators[i]) {
return true;
}
}
return false;
}
void performOperation(const string& input, RPNCalculator<double>& rpnstack) {
double firstValue, secondValue, result;
firstValue = rpnstack.Top();
rpnstack.pop();
secondValue = rpnstack.Top();
rpnstack.pop();
if (input == "-") {
result = secondValue - firstValue;
}
else if (input == "+") {
result = secondValue + firstValue;
}
else if (input == "*") {
result = secondValue * firstValue;
}
else if (input == "/") {
result = secondValue / firstValue;
}
cout << result << endl;
rpnstack.push(result);
}
the problem seems to be coming from my push() method in the RPNCalculator template class.
Looks like you have a parameter for the function void push(T data); where the parameter has the same name as the class member (data, your storage). Try changing the parameter name in the function implementation that doesn't yield this conflict. You could also be specific which data you want to use if you really want to use that name.
Try this one instead
template<class T>
inline void RPNCalculator<T>::push(T arg)
{
if (size < STACK_MAX)
data[size++] = arg;
else
fprintf(stderr, "Error: stack full\n");
}
or, if you want to be explicit about which data you are assigning
template<class T>
inline void RPNCalculator<T>::push(T data)
{
if (size < STACK_MAX)
this->data[size++] = data; // this->data is the member, data is the function local variable
else
fprintf(stderr, "Error: stack full\n");
}
This is usually avoided by naming the member variables in a way where there can't be conflicts. One way is to prefix your members with m_, where data would become m_data. Feel free to use any style of code that you want, but I'd suggest avoiding conflicts (and the second approach) when possible.
I'm learning about templating in C++ and I'm trying to write a template class to handle different data types in order to read a configuration text file formatted in a way similar to
TYPE,DEFAULT_VALUE
I defined the following class
template <class T>
class option_t
{
public:
option_t(std::string _type, std::string _defaultValue);
//~option_t();
std::string get_type();
T get_defaultValue();
private:
T defaultValue;
};
template <class T>
option_t<T>::option_t(std::string _type,std::string _defaultValue)
{
type = _type;
if( type.compare("integer") == 0)
{
defaultValue = std::stoi(_defaultValue);
}
else if(type.compare("real") == 0)
{
char *pEnd;
defaultValue = std::strtod(_defaultValue.c_str(),&pEnd);
}
else if( type.compare("boolean") == 0 )
{
std::transform(_defaultValue.begin(),_defaultValue.end(),_defaultValue.begin(),::tolower);
if(_defaultValue.compare("true") == 0 ||
_defaultValue.compare("1") == 0 ||
_defaultValue.compare("on") == 0)
{
defaultValue = true;
}
else
{
defaultValue = false;
}
}
else
{
//LOG(ERROR) << "Option " << name << " : unknown data type ( " << type << " )";
}
template <class T>
std::string option_t<T>::get_type()
{
return type;
}
template <class T>
T option_t<T>::get_defaultValue()
{
return defaultValue;
}
and when I use the following line into my main code
int tmpInt = option.get_defaultValue();
I get a compilation error "no viable conversion from 'std::__1::basic_string' to 'int'"
What does it mean? And how can I solve it?
Thanks and sorry for the stupid question :-)
Here all rest of my code
class options_t
{
public:
options_t();
//~options_t();
template <class T>
void set_option(option_t<T> option);
private:
};
options_t::options_t()
{
// read file and depending on _type create a specific option object
std::string _type = "integer";
std::string _defaultValue = "5";
if(_type.compare("integer") == 0)
{
option_t<int> option(_type,_defaultValue);
set_option(option);
}
else if(_type.compare("real") == 0)
{
option_t<double> option(_type,_defaultValue);
set_option(option);
}
else if(_type.compare("boolean") == 0)
{
option_t<bool> option(_type,_defaultValue);
set_option(option);
}
else if(_type.compare("string") == 0)
{
option_t<std::string> option(_type,_defaultValue);
set_option(option);
}
else
{
// LOG(ERROR) << " invalid data type( " << _type << " )";
}
}
template <class T>
void options_t::set_option(option_t<T> option)
{
std::string _type = option.get_type();
if(_type.compare("integer") == 0)
{
int tmpInt = option.get_defaultValue();
option_t<int> tmpOption(option.get_type(),defaultValue);
}
else if(_type.compare("real") == 0)
{
//todo;
}
else if(_type.compare("boolean") == 0)
{
//todo;
}
else if(_type.compare("string") == 0)
{
//todo;
}
else
{
// LOG(ERROR) << " invalid data type( " << option.get_type() << " )";
}
}
int main()
{
options_t options();
}
Depends on what you want to do. I will assume that int tmpInt is correct.
To retrieve an int, option has to be option_t<int> or option_t<T> where T is convertible to int. Look likes you're trying to use a string.
I'm trying to create a program that takes a polynomial function from the user, counts the number of terms it has, creates an array large enough to store all of the terms, and then stores the terms there. The problem is that I'm not quite sure how to add a private class variable (or more specifically, a string array) AFTER the program determines how the large the function is. I need this string array to be a private class variable because I want to be able to access its contents through other class methods to do things like, for example, cout each of the function terms.
main.cpp:
#include <iostream>
#include <string>
#include "Function.h"
using namespace std;
int main()
{
Function func1;
func1.coutFuncTerms();
func1.coutFunc();
return 0;
}
Function.h:
#ifndef FUNCTION_H
#define FUNCTION_H
#include <iostream>
#include <string>
#include "Function.h"
using namespace std;
class Function
{
public:
Function();
~Function();
void removePlus(string*);
void removeWhitespace(string*);
void setFuncTerms();
void splitTerms();
void coutFuncTerms();
void coutFunc();
void coutTerms(string);
protected:
private:
string func;
int funcTerms;
};
#endif
Function.cpp:
#include <iostream>
#include <string>
#include "Function.h"
using namespace std;
// Function Constructor
//
// Stores a function inputted by the user
// Adds a null character ('\0') to the end of a string
// Erases a redundant '+' sign at the beginning of a string if there's one there
// Erases any whitespace characters in a string
// Stores the number of terms in the function
Function::Function()
{
getline(cin, func);
setFuncTerms();
//splitTerms();
}
Function::~Function()
{
}
// removePlus Function
//
// Erases a redundant '+' sign at the beginning of a string if there's one there
void Function::removePlus(string* func)
{
if(func->at(0) == '+')
{
func->erase(0, 1);
}
}
// removeWhitespace Function
//
// Erases any whitespace characters in a string
void Function::removeWhitespace(string* func)
{
for(int x = 0; unsigned(x) < func->length() - 1; x++)
{
while(func->at(x) == ' ' || func->at(x) == '\t')
{
func->erase(x, 1);
}
}
}
// setFuncLength Function
//
// Finds the number of terms in a Function object's 'func' variable
// Assigns this number to the object's 'funcLength' variable
void Function::setFuncTerms()
{
funcTerms = 0;
for(int funcTerm = 0; unsigned(funcTerm) < func.length(); funcTerm += 1)
{
bool isAPotentialTerm = false;
bool isATrueTerm = false;
if(func.at(funcTerm) == '+' || func.at(funcTerm) == '-')
{
isAPotentialTerm = true;
}
if(isAPotentialTerm == true)
{
for(int newFuncTerm = funcTerm + 1; unsigned(newFuncTerm) < func.length(); newFuncTerm += 1)
{
if(func.at(newFuncTerm) == '+' || func.at(newFuncTerm) == '-')
{
break;
}
if(func.at(newFuncTerm) != ' ' && func.at(newFuncTerm) != '\t')
{
isATrueTerm = true;
break;
}
}
}
if(isATrueTerm)
{
funcTerms++;
}
}
}
// splitTerms Function
//
// Calls the splitTerm function for each term in 'func' according to the function array 'funcArray'
void Function::splitTerms()
{
string funcArray[funcTerms];
int tempFuncLength = 0;
for(int funcTerm = 0; unsigned(funcTerm) < func.length(); funcTerm += 1)
{
bool isAPotentialTerm = false;
bool isATrueTerm = false;
if(func.at(funcTerm) == '+' || func.at(funcTerm) == '-')
{
isAPotentialTerm = true;
}
if(isAPotentialTerm == true)
{
for(int newFuncTerm = funcTerm + 1; unsigned(newFuncTerm) < func.length(); newFuncTerm += 1)
{
if(func.at(newFuncTerm) == '+' || func.at(newFuncTerm) == '-')
{
break;
}
if(func.at(newFuncTerm) != ' ' && func.at(newFuncTerm) != '\t')
{
isATrueTerm = true;
break;
}
}
}
if(isATrueTerm)
{
string temp;
for(; unsigned(funcTerm) < func.length() && func.at(funcTerm) != '+' && func.at(funcTerm) != '-'; funcTerm += 1)
{
funcArray[tempFuncLength].append(1, func.at(funcTerm));
}
tempFuncLength++;
}
}
for(int x = 0; x < funcTerms; x++)
{
cout << "Term " << x + 1 << " is: " << funcArray[x] << endl;
}
}
void Function::coutFuncTerms()
{
cout << "Terms: " << funcTerms << endl;
}
void Function::coutFunc()
{
cout << "Function: " << func << endl;
}
void Function::coutTerms(string funcArrayTerm)
{
/*for(int x = 0; x < funcLength; x++)
{
cout << "Term " << x << " is: " << funcArray[x] << endl;
}*/
//cout << funcArray[0] << endl;
}
I highly recommend you change your design.
A function is a container of terms. So let's define a term:
A term minimally has a coefficient and an exponent:
struct Fundamental_Term
{
double coefficient;
int exponent;
};
If your function is only in terms of one variable, all you need is the Fundamental_Term. Otherwise, you need to have the base variable name:
struct Term_With_Base
: public Fundamental_Term
{
std::string variable_name;
};
Note: if you can't use inheritance, copy the member variables of Fundamental_Term into Term_With_Base.
Remember a function is a collection or container of terms. Assuming a function with multiple bases, we can declare:
struct Function
{
std::vector<Term_With_Base> terms;
};
Evaluation of Terms
To evaluate a function, f(x), all terms must be evaluated and their results summed.
This decomposes into two requirements: 1) Terms must have an evaluation method; 2) The function class must have an evaluation method that sums the terms.
So, we add an evaluation function to the base class:
struct Fundamental_Term
{
double coefficient;
int exponent;
double evaluate(double value)
{
return coefficient * pow(value, exponent);
}
};
struct Function
{
std::vector<Term_With_Base> terms;
double evauate(double value)
{
const unsigned int quantity = terms.size();
double result = 0.0;
for (unsigned int i = 0; i < quantity; ++i)
{
result = result + terms[i].evaluate(value);
}
return result;
}
};
When creating a function from a string, a preference is to create a constructor of Fundamental_Term that takes a string parameter. The term object should read its coefficient, variable name and exponent, not the Function container.
For more examples, search StackOverflow for "c++ parse term evaluation".
Edit 1: Inserting terms
One method to insert terms, is to have a method in the term data structure that loads a term from a string:
bool
Fundamental_Term ::
load_from string(const std::string& input,
unsigned int & start_position)
{
bool term_is_valid = false;
// Parse the string and load appropriate fields.
// Set the start position to the first position after the valid term.
// Set term_is_valid to true if the term has valid syntax.
return term_is_valid;
}
The Function object would have a member to load terms from a string.
bool
Function ::
load_terms_from_string(const std::string& input)
{
Term_With_Base term;
unsigned int position_in_string = 0;
bool term_is_valid = true;
while (term_is_valid && (position_in_string < input.size()))
{
term_is_valid = term.load_from_string(input, position_in_string);
if (term_is_valid)
{
terms.push_back(term);
}
}
}
The std::vector used to contain the terms will expand as necessary with each additional term that is parsed. The loop will terminate when the string is parsed or there is an invalid term.
For my c++ class we were given the task of writing a template class who's class object's type is defined by the user using templates.
Code snipit from main:
if (dataType == "1" || dataType == "int") {
simpleVector<int> userArray;
} else if (dataType == "2" || dataType == "double") {
simpleVector<double> userArray;
} else if (dataType == "3" || dataType == "char") {
simpleVector<char> userArray;
} else if {
simpleVector<string> userArray;
}
userArray.setDefaultArray();
From this I get error code C2065 - undeclared identifier error. I see why I am getting the error but i do not know how I can declare userArray before I know the data type.
Source Code:
#include <stdio.h>
#include <string>
using std::string;
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
template<class T>
class simpleVector {
public:
void setDefaultArray ();
void setArraySize (int size);
void copy (T *arr);
void desctruct ();
int getArraySize ();
T getElementAt (int index);
void fillArray();
private:
int arraySize;
T *myArray;
};
int main () {
string dataType;
int arraySize;
bool loopCondition = false;
do {
cout << "Data Type:";
cin >> dataType;
if (dataType == "1" || dataType == "2" || dataType == "3" || dataType == "4"
|| dataType == "int" || dataType == "double" || dataType == "char" || dataType == "string") {
loopCondition = false;
} else {
cout << "WARNING: invalid data type entered." << endl;
cout << "Valid entries are (1.int, 2.double, 3.char, 4.string)" << endl;
loopCondition = true;
}
} while (loopCondition);
if (true)
int num = 9;
else
int num = 7;
int num2 = num;
//simpleVector userArray; //?? Review
if (dataType == "1" || dataType == "int") {
simpleVector<int> userArray;
} else if (dataType == "2" || dataType == "double") {
simpleVector<double> userArray;
} else if (dataType == "3" || dataType == "char") {
simpleVector<char> userArray;
} else if (dataType == "4" || dataType == "char") {
simpleVector<string> userArray;
}
userArray.setDefaultArray();
cout << "Number of Inputs:";
cin >> arraySize;
userArray.setArraySize(arraySize);
userArray.fillArray();
return 0;
}
//Should call desctruct before this if reusing.
template<class T>
void simpleVector<T>::setDefaultArray() {
arraySize = 0;
myArray = NULL; //note: NULL is case sensitive (#include <stdio.h>)
}
template<class T>
void simpleVector<T>::setArraySize (int size) {
myArray = new T[size];
}
template<class T>
void simpleVector<T>::copy (T *arr) {
//ToDo
}
template<class T>
void simpleVector<T>::desctruct () {
//ToDo
}
template<class T>
int simpleVector<T>::getArraySize () {
//ToDo
}
template<class T>
T simpleVector<T>::getElementAt (int index) {
//ToDo
}
template<class T>
void simpleVector<T>::fillArray() {
cout << "Enter Array Values" << endl;
for (int i; i < arraySize; i++) {
cout << "Element " + i + ":";
cin >> myArray[i];
}
}
Thanks,
Mike
The code in Eugene's answer looks great, but is maybe too complicated for learning C++?
A very simple solution could look like this
declare a class vectorBase, which declares all the methods you need in all your vectors
let the templated class inherit from vectorBase
template
class simpleVector : public vectorBase { ...
then declare a pointer of type vectorBase before your
if (dataType == "1" || dataType == "int") ...
in the if-block assign the newly created userArrays to the base class pointer
later, access the methods through the baseClass pointer, which is identical for all specific template classes
You can't do this, because types determination are a compile-time process. Use inheritance instead templates, if you want to determine types at runtime.
Also I can suggest you the "variant" pattern. For example:
#include <memory>
#include <string>
class variant
{
public:
template <class T>
variant& operator = (T const& t)
{
typedef type<T> assign_type;
object = std::auto_ptr<assign_type>(new assign_type(t));
return *this;
}
template <class T>
operator T ()
{
typedef type<T> assign_type;
assign_type& type = dynamic_cast<assign_type&>(*object);
return type.get();
}
private:
class base
{
public:
virtual ~base() {}
};
typedef std::auto_ptr<base> base_ptr;
template <class T>
class type : public base
{
public:
type(T const& t)
: object(t)
{
}
T get() const
{
return object;
}
private:
T object;
};
base_ptr object;
};
struct dummy
{
int a;
int b;
int c;
};
int main()
{
variant v1, v2, v3, v4;
v1 = 2;
v2 = 5.0f;
v3 = std::string("Pot of gold");
v4 = dummy();
int i = v1;
float f = v2;
std::string s = v3;
dummy d = v4;
return 0;
}
As I understand, the intention of this problem is to teach how template usage is limited by type definition on compile time. It's pretty straightforward that user's choice will be limited by some list of types which developer cared to explicitly specify. Now the question is - how it affects the resulting program?
First, you should realize that code-paths for all possible values of your template argument will be instantiated on compile time. In other words, binary code for setDefaultArray, setArraySize, fillArray and other member functions which you explicitly or implicitly call in your generic algorithm will be generated for int, double, char and std::string template arguments. There's not much you can do to optimize it out of the executable.
However, what you can do is to decide how to store your object(s) in memory in most efficient way. And obviously for your task you need only a single instance of some simpleVector at a time. So you may think of a memory block big enough to keep any simpleVector instantiation and also designating which of them it currently contains. In C++ it will sound like this:
struct SimpleVectors {
VectorTypeEnum vte;
union {
simpleVector<int> v_int;
simpleVector<double> v_double;
simpleVector<char> v_char;
simpleVector<string> v_string;
};
};
Please note that you can do it only with POD-structures (google for the definition).
Inheritance-based approaches eventually boil-down to this kind of layout.
To complete the picture, we just need to connect a processing logic to this data structure:
template <
typename T
>
inline void handleTask (
simpleVector <
T
>
& v
)
{
int arraySize;
v.setDefaultArray();
cout << "Number of Inputs:";
cin >> arraySize;
v.setArraySize(arraySize);
v.fillArray();
}
The benefit of this approach over the inheritance-based is that you can make your class member functions inline, and the compiler will take care that their calls will be an order of magnitude faster than the virtual member functions.
And finally, the key piece of your main function will look as:
SimpleVectors userArray;
// we don't really need to initialize userArray.vte in this sample
if (dataType == "1" || dataType == "int") {
handleTask(userArray.v_int);
} else if (dataType == "2" || dataType == "double") {
handleTask(userArray.v_double);
} else if (dataType == "3" || dataType == "char") {
handleTask(userArray.v_char);
} else if (dataType == "4" || dataType == "string") {
handleTask(userArray.v_string);
}