I am trying to make a calculator program and I am getting an error when I try to define a function from a class template
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream> //Basic I/O
#include <sstream> //to handle bad input
#include <windows.h> //For future key state use
#include <cmath> //Extended math functions from C
#include <stdexcept> //To handle exceptions
#include <exception> //To handle exceptions
#include "Calculator.h"
#define pi 3.141592654
usingnamespace std;
int main()
{
//This file has nothing, so it is useless for this thread
return 0;
}
#include <iostream>
#include "Calculator.h"
template <typename T> class Calculator final
{ //This class template will handle the main functionality of the calculator. the generic type T will hold either an int or a double and the result will be either a
//double or int. In the future two generic types will be provided to use operands of different types
public:
Calculator() : operand1(NULL), operand2(NULL), result(operand1 + operand2), counter(1) {}
//This sets the operands and the result to a null state to avoid accidental operations on them.
void set_operand_one(T op1);
T get_operand_one();
void set_operand_two(T op2);
T get_operand_two() ;
T get_result();
//The following functions are responsible for calculations: Addition(), Subtraction(), Multiplication(), Division().
T Addition();
T Subtraction();
T Multiplication();
T Division();
vector <char*> operation; //This will hold the entire operation to print to the screen
void tick();
size_t get_count();
private:
T mOperand1;
T mOperand2;
T mResult; //+, -, *, /, =
T mOperator;
static size_t counter; //This tells whether a calculation function has been called before, to calculate result
};
Calculator.h
1 2 3 4 5 6 7 8 9 10 11 12
#pragma once
#include <iostream> //Basic I/O
#include <sstream> //to handle bad input
#include <windows.h> //For future key state use
#include <cmath> //Extended math functions from C
#include <stdexcept> //To handle exceptions
#include <exception> //To handle exceptions
usingnamespace std;
template <typename T> void Calculator<T>::set_operand_one() {cout << "hi";}
Thank you everyone for your replies, but I finally figured out the problem...
I accidentally put the declaration of the class in the .cpp file and the definition in the .h file, when it should be the reverse of that! I apologize for making this stupid mistake. I will mark this thread as solved for others that make the same mistake :)
Oh that's right - there is a difference with templates. It may be working so far if you don't have the main function built yet that would be trying to use the template class?
A way to keep separate files that I learned (and is mentioned in that linked SO thread) is to put an include to the .cpp file at the bottom of the .h file.
Have you tried exercising an object of type Calculator in main?
All of the code required for creation of a default constructed Calculator object is present in the header. Code for parameterized methods is not generated unless the methods are used.