Reputation:
I'm having trouble identifying the problem here as I'm using multiple techniques I am not that familiar with (splitting code into files, templates), so I have recreated it in the simplest way I could think:
classes.h:
class baseClass{
public:
virtual void myfunction(double dA, double dB) = 0; //Pure virtual function
virtual void myfunction(double dC) = 0;//Overloaded pure virtual function
};
class derivedClass1 :baseClass{
public:
void myfunction(double dA, double dB)override;
void myfunction(double dC)override;
};
class derivedClass2 :baseClass{
public:
void myfunction(double dA, double dB)override;
void myfunction(double dC)override;
};
classes.cpp :
#include"classes.h"
void derivedClass1::myfunction(double dA, double dB){
//DO STUFF
}
void derivedClass2::myfunction(double dA, double dB){
//Do different stuff to derivedClass1
}
template <class type>
void type::myfunction(double dC){
double dA = dC;
double dB = 0; //In place of a complex calculation
myfunction(dA, dB) //Call the function for the
//relevant class and 2 input arguments.
}
main:
#include"classes.h"
int main(){
derivedClass1 example;
example.myfunction(1.0);
}
What I want to do is overload all myfunction for only one input argument with a template function. As there's many derived classes I wanted to use templates. However, when I do something like this I get this error:
error C2063: 'myfunction' : not a function
Is there an easy way to do this or a better way around it? I've tried putting the template in the header file and removing the in class declarations but that doesn't work either.
Upvotes: 0
Views: 68
Reputation: 1367
I think there lot of problems in the above code you provided like the following
void type::myfunction(double dC)
In this above statement "type" should be the class name in which myfunction will be member. Instead you given this as a template type.
I am rewritten your code as follows,
class baseClass
{
public:
template<typename Type>
void myfunction(Type dA, double dB)
{};
template<typename Type>
void myfunction(Type dC)
{};
};
class derivedClass1 :baseClass
{
public:
template<typename Type>
void myfunction(Type dA, double dB)
{
//DO STUFF
}
template<typename Type>
void myfunction(Type dC)
{
Type dA = dC;
double dB = 0; //In place of a complex calculation
myfunction(dA, dB);
};
};
class derivedClass2 :baseClass
{
public:
template<typename Type>
void myfunction(Type dA, double dB)
{
//Do different stuff to derivedClass1
}
template<typename Type>
void myfunction(Type dC)
{
Type dA = dC;
double dB = 0; //In place of a complex calculation
myfunction(dA, dB);
};
};
int main()
{
derivedClass1 example;
example.myfunction(1.0);
}
Upvotes: 0
Reputation: 1
I think you want to write general function outside any class to call the respective class function.
1) You don't need to put type::myfunction
template <class type>
void myfunction(baseClass *caller, double dc)
{
double dA = dC;
double dB = 0; //In place of a complex calculation
caller->myfunction(double dA, double dB) //Call the function for the
}
2) actually, you don't need type template for it if there are not multiple data types
void myfunction(baseClass *caller,double dc)
{
double dA = dC;
double dB = 0; //In place of a complex calculation
caller->myfunction(double dA, double dB) //Call the function for the
}
int main(){
derivedClass1 example;
myfunction(&example,1.0);
derivedClass2 example2;
myfunction(&example2,2.0);
baseClass example3;
myfunction(&example3,4.0);
}
Upvotes: 0
Reputation: 3589
You don't need templates at all. Polymorphism will solve this problem for you. Would this work for you:
class baseClass{
public:
virtual void myfunction(double dA, double dB) = 0; //Pure virtual function
virtual void myfunction(double dC) {
double dA = dC;
double dB = 0;
myfunction(dA, dB) // Calls the function of the derived class
}
};
Then your main function can look like:
#include"classes.h"
int main(){
derivedClass1 example;
baseClass *caller = &example;
caller.myfunction(1.0);
}
Upvotes: 1