In my programming class we're just being introduced to the concept of templates within C++. This is a concept we never covered in my class on Java last semester, and the whole syntax of C++ is really throwing me for a loop. I'm getting a long string of compilation errors with the code I will post below. It would make me think that I'm missing something very obvious within the template Syntax. The following is just an example template I'm trying to work with, something to get me started on the homework. If any of you have any insights as to why this isn't compiling, I'd be grateful. Thanks!!
keyValuePair.h
#include <fstream>
#include <iostream>
#include <string>
#ifndef KEYVALUEPAIR
#define KEYVALUEPAIR
template<class key, class value>
class keyValuePair
{
private:
key kvar;
value vvar;
public:
keyValuePair(); //Default Constructor
void setKvar(key object1); //Method to set kvar to a value
void setVvar(value object2); //Method to set vvar to a value
key getKvar(); //Method to return kvar
value getVvar(); //Method to return vvar
};
#include "keyValuePair.cpp"
#endif
keyValuePair.cpp
#include <iostream>
#include <fstream>
#include <string>
#include "keyValuePair.h"
template<class key, class value>;
keyValuePair<key, value>::keyValuePair()
{
}
template<class key, class value>; //return the value of kvar
key keyValuePair<key, value>::getKvar()
{
return kvar;
}
template<class key, class value>; //return the value of vvar
value keyValuePair<key, value>::getVvar()
{
return vvar;
}
template<class key, class value>; //set the value of kvar
void keyValuePair<key, value>::setKvar(key& object1)
{
object1 = kvar;
}
template<class key, class value>; //set the value of vvar
void keyValuePair<key, value>::setVvar(value& object2)
{
object2 = vvar;
}
main.cpp
#include <fstream>
#include <iostream>
#include <string>
#include "keyValuePair.h"
using namespace std;
int main(int argc, char* argv[])
{
fstream myFile(argv[1], ios::in);
fstream fout("out.txt", ios::out);
myFile.close();
fout.close();
keyValuePair<string, int> sample;
sample.setKvar("Hello World.");
sample.setVvar(3);
cout << sample.getKvar() << sample.getVvar() << "\n";
return 0;
}