Please have a look at the following code
Calculator.h
#pragma once
#include <iostream>
template<class T>
class Calculator
{
public:
Calculator(void);
~Calculator(void);
void add(T x, T y)
{
cout << (x+y) << endl;
}
void min(T x, T y)
{
cout << (x-y) << endl;
}
void max(T x, T y)
{
cout << (x*y) << endl;
}
void dev(T x, T y)
{
cout << (x/y) << endl;
}
};
Main.cpp
#include "Calculator.h"
using namespace std;
int main()
{
Calculator<double> c;
c.add(23.34,21.56);
system("pause");
return 0;
}
When I run this code, I get the below error. I am not much familiar with class templates. Please help!
1>------ Build started: Project: TemplateCalculator, Configuration: Debug Win32 ------
1>LINK : error LNK2001: unresolved external symbol _mainCRTStartup
1>c:\users\yohan\documents\visual studio 2010\Projects\TemplateCalculator\Debug\TemplateCalculator.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Threw this into VS2010 really quick. It was not liking cout and endl due to not using namespace std. I just put in a using statement for speed. I prefer to use std:: conventions normally because this avoids naming issues that ‘using’ can generate
Also It complained about no constructor and destructor (added in {}’s)
And like a previous comment said, having your main in a header file is probably bad too. This is all in a default project in test.cpp
EDIT I changed the file structure to have the Calculator in a .h
Calculator.h
Test.cpp