How can I access the structs data through the template class? I have a template class, I'm going to use to instantiate 1 of three structs containing three variables, Identifier, Description, and Value of various different types.
I instantiate those structs as arrays just fine
GenericRecord<Struct1> inventory[1000];
GenericRecord<Struct2> machines[1000];
GenericRecord<Struct3> physicalStructures[1000];
The trouble comes when I try to access their data.
cout << inventory[0].Identifier;
does not work, I get the error
Main.cpp:40:23: error: no member named 'Identifier' in 'GenericRecord<Struct1>'
It seems that the template class does not have access to the structs public data. I Guess I was expecting it work much like a base and derived class, but C++ has different Ideas about that.
Of Course going straight through the struct works fine.
Struct1 test[1000];
cout << "\ntest[0].Identifier: " << test[0].Identifier; //output 2
cout << "\ntest[0].Description: " << test[0].Description; //output string
cout << "\ntest[0].Value: " << test[0].Value; // output 2
There must be something about templates that I am completely unaware of. Here is my bare bones code.
template <class Type>
class GenericRecord
{
private:
Type structure;
public:
void setRecord(Type recordArg);
Type getRecord();
};
struct Struct1
{
//test data
public:
int Identifier = 2;
string Description = "string";
float Value = 2.0;
};
struct Struct2
{
public:
long int Identifier;
string Description;
float Value;
};
struct Struct2
{
public:
string Identifier;
string Description;
double Value;
};
int main(){
Struct1 test[1000];
cout << test[0].Identifier;
cout <<< inventory[0].Identifier;
}
//not currently using these but bonus points if you want to do them lol
template <class Type>
void GenericRecord<Type>::setRecord(Type recordArg)
{
structure = recordArg;
}
template <class Type>
Type GenericRecord<Type>::getRecord()
{
return this->structure;
}
How can I get the template class to access the structs public data?
edit: I am not using a template method inside of a template class, there is no separate template method that I am even attempting. Take a look again please, I'm trying to do it a shorter way.
Aucun commentaire:
Enregistrer un commentaire