これは私のコードです。テンプレートを使って練習することです。
ヘッダ
#ifndef H_rectangleType
#define H_rectangleType
#include <iostream>
using namespace std;
namespace Rectangle{
template <class myType>
class rectangleType
{
//Overload the stream insertion and extraction operators
friend ostream& operator << (ostream&, const rectangleType &);
friend istream& operator >> (istream&, rectangleType &);
public:
void setDimension(myType l, myType w);
myType getLength() const;
myType getWidth() const;
myType area() const;
myType perimeter() const;
void print() const;
rectangleType<myType> operator+(const rectangleType<myType>&) const;
//Overload the operator +
rectangleType<myType> operator*(const rectangleType<myType>&) const;
//Overload the operator *
bool operator==(const myType&) const;
//Overload the operator ==
bool operator!=(const myType&) const;
//Overload the operator !=
rectangleType();
rectangleType(myType l, myType w);
private:
myType length;
myType width;
};
}
#endif
メンバーの定義
#include <iostream>
#include "rectangleType.h"
using namespace std;
namespace Rectangle{
template <class myType>
void rectangleType<myType>::setDimension(myType l, myType w)
{
if (l >= 0)
length = l;
else
length = 0;
if (w >= 0)
width = w;
else
width = 0;
}
template <class myType>
myType rectangleType<myType>::getLength() const
{
return length;
}
template <class myType>
myType rectangleType<myType>::getWidth()const
{
return width;
}
template <class myType>
myType rectangleType<myType>::area() const
{
return length * width;
}
template <class myType>
myType rectangleType<myType>::perimeter() const
{
return 2 * (length + width);
}
template <class myType>
void rectangleType<myType>::print() const
{
cout << "Length = " << length
<< "; Width = " << width;
}
template <class myType>
rectangleType<myType>::rectangleType(myType l, myType w)
{
setDimension(l, w);
}
template <class myType>
rectangleType<myType>::rectangleType()
{
length = 0;
width = 0;
}
template <class myType>
rectangleType<myType> rectangleType<myType>::operator+
(const rectangleType<myType>& rectangle) const
{
rectangleType<myType> tempRect;
tempRect.length = length + rectangle.length;
tempRect.width = width + rectangle.width;
return tempRect;
}
template <class myType>
rectangleType<myType> rectangleType<myType>::operator*
(const rectangleType<myType>& rectangle) const
{
rectangleType<myType> tempRect;
tempRect.length = length * rectangle.length;
tempRect.width = width * rectangle.width;
return tempRect;
}
template <class myType>
bool rectangleType<myType>::operator==
(const myType& rectangle) const
{
return (length == rectangle.length &&
width == rectangle.width);
}
template <class myType>
bool rectangleType<myType>::operator!=
(const myType& rectangle) const
{
return (length != rectangle.length ||
width != rectangle.width);
}
template <class myType2>
ostream& operator << (ostream& osObject,
const rectangleType<myType2>& rectangle)
{
osObject << "Length = " << rectangle.length
<< "; Width = " << rectangle.width;
return osObject;
}
template <class myType2>
istream& operator >> (istream& isObject,
rectangleType<myType2>& rectangle)
{
isObject >> rectangle.length >> rectangle.width;
return isObject;
}
}
主要
#include <iostream> //Line 1
#include "rectangleType.h" //Line 2
using namespace std; //Line 3
using namespace Rectangle;
int main() //Line 4
{ //Line 5
cout << "Enter the type of your Rectangle.\n1. int\n2. double\n3. float";
rectangleType<double> myRectangle(23, 45); //Line 6
int int1 = 1;
int double1 = 2;
int float1 = 3;
int temp= 0;
cin >> temp; //Line 7
if(temp == int1)
{
rectangleType<int> myRectangle(23, 45);
rectangleType<int> yourRectangle;
cout << "Line 8: myRectangle: " << myRectangle
<< endl;
cout << "Line 9: Enter the length and width "
<<"of a rectangle: ";
cin >> yourRectangle;
cout << endl;
cout << "Line 12: yourRectangle: "
<< yourRectangle << endl;
cout << "Line 13: myRectangle + yourRectangle: "
<< myRectangle + yourRectangle << endl;
cout << "Line 14: myRectangle * yourRectangle: "
<< myRectangle * yourRectangle << endl;
}
if(temp == double1)
{
rectangleType<double> myRectangle(23, 45);
rectangleType<double> yourRectangle;
cout << "Line 8: myRectangle: " << myRectangle
<< endl;
cout << "Line 9: Enter the length and width "
<<"of a rectangle: ";
cin >> yourRectangle;
cout << endl;
cout << "Line 12: yourRectangle: "
<< yourRectangle << endl;
cout << "Line 13: myRectangle + yourRectangle: "
<< myRectangle + yourRectangle << endl;
cout << "Line 14: myRectangle * yourRectangle: "
<< myRectangle * yourRectangle << endl;
}
if(temp == float1)
{
rectangleType<float> myRectangle(23, 45);
rectangleType<float> yourRectangle;
cout << "Line 8: myRectangle: " << myRectangle
<< endl;
cout << "Line 9: Enter the length and width "
<<"of a rectangle: ";
cin >> yourRectangle;
cout << endl;
cout << "Line 12: yourRectangle: "
<< yourRectangle << endl;
cout << "Line 13: myRectangle + yourRectangle: "
<< myRectangle + yourRectangle << endl;
cout << "Line 14: myRectangle * yourRectangle: "
<< myRectangle * yourRectangle << endl;
}
return 0;
}
ここで、これらのエラーが発生します
1>testOpOverLoadClass.obj : error LNK2019: unresolved external symbol "public: class Rectangle::rectangleType<float> __thiscall Rectangle::rectangleType<float>::operator*(class Rectangle::rectangleType<float> const &)const " (??D?$rectangleType@M@Rectangle@@QBE?AV01@ABV01@@Z) referenced in function _main
1>testOpOverLoadClass.obj : error LNK2019: unresolved external symbol "public: class Rectangle::rectangleType<float> __thiscall Rectangle::rectangleType<float>::operator+(class Rectangle::rectangleType<float> const &)const " (??H?$rectangleType@M@Rectangle@@QBE?AV01@ABV01@@Z) referenced in function _main
1>testOpOverLoadClass.obj : error LNK2019: unresolved external symbol "class std::basic_istream<char,struct std::char_traits<char> > & __cdecl Rectangle::operator>>(class std::basic_istream<char,struct std::char_traits<char> > &,class Rectangle::rectangleType<float> &)" (??5Rectangle@@YAAAV?$basic_istream@DU?$char_traits@D@std@@@std@@AAV12@AAV?$rectangleType@M@0@@Z) referenced in function _main
1>testOpOverLoadClass.obj : error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl Rectangle::operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Rectangle::rectangleType<float> const &)" (??6Rectangle@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV12@ABV?$rectangleType@M@0@@Z) referenced in function _main
1>testOpOverLoadClass.obj : error LNK2019: unresolved external symbol "public: __thiscall Rectangle::rectangleType<float>::rectangleType<float>(void)" (??0?$rectangleType@M@Rectangle@@QAE@XZ) referenced in function _main
1>testOpOverLoadClass.obj : error LNK2019: unresolved external symbol "public: __thiscall Rectangle::rectangleType<float>::rectangleType<float>(float,float)" (??0?$rectangleType@M@Rectangle@@QAE@MM@Z) referenced in function _main
1>testOpOverLoadClass.obj : error LNK2019: unresolved external symbol "public: class Rectangle::rectangleType<double> __thiscall Rectangle::rectangleType<double>::operator*(class Rectangle::rectangleType<double> const &)const " (??D?$rectangleType@N@Rectangle@@QBE?AV01@ABV01@@Z) referenced in function _main
1>testOpOverLoadClass.obj : error LNK2019: unresolved external symbol "public: class Rectangle::rectangleType<double> __thiscall Rectangle::rectangleType<double>::operator+(class Rectangle::rectangleType<double> const &)const " (??H?$rectangleType@N@Rectangle@@QBE?AV01@ABV01@@Z) referenced in function _main
1>testOpOverLoadClass.obj : error LNK2019: unresolved external symbol "class std::basic_istream<char,struct std::char_traits<char> > & __cdecl Rectangle::operator>>(class std::basic_istream<char,struct std::char_traits<char> > &,class Rectangle::rectangleType<double> &)" (??5Rectangle@@YAAAV?$basic_istream@DU?$char_traits@D@std@@@std@@AAV12@AAV?$rectangleType@N@0@@Z) referenced in function _main
1>testOpOverLoadClass.obj : error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl Rectangle::operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Rectangle::rectangleType<double> const &)" (??6Rectangle@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV12@ABV?$rectangleType@N@0@@Z) referenced in function _main
1>testOpOverLoadClass.obj : error LNK2019: unresolved external symbol "public: __thiscall Rectangle::rectangleType<double>::rectangleType<double>(void)" (??0?$rectangleType@N@Rectangle@@QAE@XZ) referenced in function _main
1>testOpOverLoadClass.obj : error LNK2019: unresolved external symbol "public: class Rectangle::rectangleType<int> __thiscall Rectangle::rectangleType<int>::operator*(class Rectangle::rectangleType<int> const &)const " (??D?$rectangleType@H@Rectangle@@QBE?AV01@ABV01@@Z) referenced in function _main
1>testOpOverLoadClass.obj : error LNK2019: unresolved external symbol "public: class Rectangle::rectangleType<int> __thiscall Rectangle::rectangleType<int>::operator+(class Rectangle::rectangleType<int> const &)const " (??H?$rectangleType@H@Rectangle@@QBE?AV01@ABV01@@Z) referenced in function _main
1>testOpOverLoadClass.obj : error LNK2019: unresolved external symbol "class std::basic_istream<char,struct std::char_traits<char> > & __cdecl Rectangle::operator>>(class std::basic_istream<char,struct std::char_traits<char> > &,class Rectangle::rectangleType<int> &)" (??5Rectangle@@YAAAV?$basic_istream@DU?$char_traits@D@std@@@std@@AAV12@AAV?$rectangleType@H@0@@Z) referenced in function _main
1>testOpOverLoadClass.obj : error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl Rectangle::operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Rectangle::rectangleType<int> const &)" (??6Rectangle@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV12@ABV?$rectangleType@H@0@@Z) referenced in function _main
1>testOpOverLoadClass.obj : error LNK2019: unresolved external symbol "public: __thiscall Rectangle::rectangleType<int>::rectangleType<int>(void)" (??0?$rectangleType@H@Rectangle@@QAE@XZ) referenced in function _main
1>testOpOverLoadClass.obj : error LNK2019: unresolved external symbol "public: __thiscall Rectangle::rectangleType<int>::rectangleType<int>(int,int)" (??0?$rectangleType@H@Rectangle@@QAE@HH@Z) referenced in function _main
1>testOpOverLoadClass.obj : error LNK2019: unresolved external symbol "public: __thiscall Rectangle::rectangleType<double>::rectangleType<double>(double,double)" (??0?$rectangleType@N@Rectangle@@QAE@NN@Z) referenced in function _main
1>C:\Users\jr\Documents\Visual Studio 2010\Projects\CMPE 126\Lab 2\Debug\Lab 2.exe : fatal error LNK1120: 18 unresolved externals
私はこれらのエラーが何を意味するのか理解できないようです。前もって感謝します