2013-04-20 28 views
2

У меня есть функция isEqualTo, которая выполняет сравнение нескольких типов данных с помощью шаблонов. Когда у меня было все это в файле main.cpp, все было хорошо, но как только я разделил класс Complex на его заголовок, и это файл cpp, я получаю сообщение об ошибке, «список аргументов для шаблона класса« 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 

} 
+1

'template {' недопустимый код на C++. ваше отсутствие имени вашего класса. ** Пожалуйста ** отправьте свой ** настоящий ** код. – WhozCraig

+0

Возможный дубликат [Список аргументов для шаблона шаблона отсутствует] (http://stackoverflow.com/questions/15283195/argument-list-for-class-template-is-missing) – dthorpe

ответ

5

Комплекс - это класс шаблонов. Поэтому при построении ему нужен аргумент шаблона.

Complex<int> complexA(10, 5); 
     ^^^^ 
+0

Как повторно инициализировать глобальную переменную шаблона this путь ? У меня есть комплекс complexA' в моей глобальной области, и я хочу повторно инициализировать 'complexA' inside' main() ' –

+0

Зависит от реализации сложного класса. Вероятно, вы можете сделать 'complexA = Complex (newval_real, newval_img)'. Опять же, это зависит от вашей реализации класса Complex (ваши конструкторы и перегрузки операторов присваивания). – stardust

Смежные вопросы