This is my very first question on StackOverflow. I've searched for similar "Identifier not Found" errors but couldn't find anything helpful. I'll be grateful for your help since I've been trying to work this out for 2 hours with no results.
It basically creates an object of type "Addition" and prints "1 + 1 = 2" (if it worked).
When I was not using templates it was working fine. But when I changed everything to templates it stopped working. I couldn't figure this out.
Visual studio does not underline anything in red, however I get a "identifier not defined" error for m_Argument_1 and m_Argument_2.
Thank you for your help and I apologize if this is a too simple question to ask here, but nevertheless I couldn't find a way out or figure out where the problem is.
This is my header file "Expression.h":
#ifndef OPERATION_HEADER
#define OPERATION_HEADER
#include <string>
#include <iostream>
template <typename T>
class Expression
{
protected:
char m_ExpressionSign; /* Represents Expression's Sign ('+','-','*','/') */
T m_Argument_1; /* Represents L Argument */
T m_Argument_2; /* Represents R Argument */
public:
Expression(T arg_1, T arg_2, char expressionSign) : m_Argument_1(arg_1), m_Argument_2(arg_2), m_ExpressionSign(expressionSign) { };
virtual T Result() const = 0;
virtual void Print(std::ostream& out) const;
friend std::ostream& operator << (std::ostream& out, const Expression<T>& expression);
};
template <typename T>
class Addition : public Expression<T>
{
public:
Addition(T arg1, T arg2) : Expression<T>(arg1, arg2, '+') {};
T Result() const { return m_Argument_1 + m_Argument_2; };
};
template <typename T>
void Expression<T>::Print(std::ostream& out) const
{
out << m_Argument_1 << " " << m_ExpressionSign << " " << m_Argument_2 << " = ";
}
template <typename T>
std::ostream& operator << (std::ostream& out, const Expression<T>& expression)
{
expression.Print(out);
return out;
}
#endif /* OPERATION_HEADER */
And this is my main.cpp:
#include <iostream>
#include <iomanip>
#include "Expression.h"
int main(int argc, char** argv)
{
Addition<int> A2(1, 1);
std::cout << "A2: " << A2 << " = " << std::setw(2) << A2.Result() << std::endl;
retun 0;
}