This is my current code, only thing that I can't get to work right now is outputting the second variable b which is of type string.
#include <iostream>
#include <string>
using namespace std;
template<class TypeA, class TypeB>
class Pair
{
public:
//default constructor
//initialize both members to their default values
//as per data type of Type1 and Type2
Pair() : a(TypeA()), b(TypeB()) {};
//parameterized constructor taking TypeA and TypeB to
//initialize values of a and b:
Pair(const TypeA& one, const TypeB& two) :
a(one), b(two) {}
//copy-constructor to copy one Pair object from
//another Pair object exactly same type:
Pair(const Pair<TypeA, TypeB>& rhs) :
a(rhs.a), b(rhs.b) {}
void write(const TypeA&, const TypeB&);
void read();
private:
TypeA a;
TypeB b;
};
template<class TypeA, class TypeB>
void Pair<TypeA, TypeB>::write(const TypeA& one,const TypeB& two)
{
a = one;
b = two;
}
template<class TypeA, class TypeB>
void Pair<TypeA, TypeB>::read()
{
std::cout << "These are the values: " << a << b << std::endl;
}
int main()
{
Pair<int, std::string> object1;
object1.write(1, "one");
object1.read();
system("Pause");
return 0;
}
If I remove the b from the read() code it prints the single 1 fine. However, if I run it as is I get the following error:
C2679 binary '<<': no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
I tried overloading the operator<< but I haven't been able to wrap my mind around the syntax once you trow multiple typesets into the mix...
Any help is appreciated.
Aucun commentaire:
Enregistrer un commentaire