我有一個isEqualTo函數,它通過使用模板來比較幾種數據類型。當我第一次把所有的東西都放在main.cpp文件中時,一切都很好,但是一旦我將Complex類拆分爲它的頭並且它是cpp文件,我收到一個錯誤消息,提示「類模板」Complex「的參數列表丟失」 。所以我當然檢查我的默認構造函數的類複雜,這裏它是:類模板「Complex」的參數列表丟失
**main.cpp**
#include <iostream>
#include <string> objects
#include "Complex.h"
using namespace std;
template <class T>
class Complex<T>
bool isEqualTo(T a, T b)
{
if(a==b)
{
cout << a << " is EQUAL to " << b << endl;
return true;
}
else cout << a << " is NOT EQUAL to " << b << endl;
return false;
}
int main()
{
//Comparing Complex class
Complex<int> complexA(10, 5), complexB(10, 54), complexC(10, -5), complexD(-10, -5); //Creating complex class objects
cout << endl << endl << "******Comparing Complex Objects:****** \n";
isEqualTo(complexA, complexA); //true
isEqualTo(complexA, complexB); //false
isEqualTo(complexC, complexA); //false
isEqualTo(complexD, complexD); //true
}
**Complex.h**
#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream>
using namespace std;
template <class T>
class Complex
{
public:
//default constructor for class Complex
Complex(int realPart, int imaginaryPart) : real(realPart), imaginary(imaginaryPart)
//Overloading equality operator
bool operator==(const Complex &right) const //address of our cosnt right operand will have two parts as a complex data type, a real and imaginary
{
return real == right.real && imaginary==right.imaginary; //returns true if real is equal to BOTH of its parts right.real and right.imaginary
}
//overloading insertion operator
friend ostream &operator<<(ostream&, Complex<T>&);
private: //private data members for class Complex
int real; //private data member real
int imaginary; //private data member imaginary
};
#endif
**Complex.cpp**
#include "Complex.h"
#include <iostream>;
using namespace std;
template<class T>
ostream &operator<<(ostream& os, Complex,T.& obj)
{
if(obj.imaginary > 0)//If our imaginary object is greater than 0
os << obj.real << " + " << obj.imaginary << "i";
else if (obj.imaginary == 0) //if our imaginary object is ZERO
os << obj.real; //Then our imaginary does not exist so insert only the real part
else //If no other condition is true then imaginary must be negative so therefor
{
os << obj.real << obj.imaginary << "i"; //insert the real and the imaginary
}
return os; //return the ostream object
}
'template {'不是有效的C++代碼。你錯過了你的班級名字。 **請**發佈您的**真實**代碼。 –
WhozCraig
2013-04-20 06:21:52
[類模板的參數列表丟失]的可能重複(http://stackoverflow.com/questions/15283195/argument-list-for-class-template-is-missing) – dthorpe 2013-04-20 06:24:16