これは私が作成した簡単なテンプレート Dequeue オブジェクトで、Mac 端末では問題なく動作しましたが、Visual C++ では動作しません。これがばかげた質問である場合は、事前に申し訳ありません。私は C++ とテンプレートに少し慣れていません。コードは次のとおりです。
#include <iostream>
using namespace std;
template <typename T>
class Dequeue;
template <typename T>
class node;
template <typename T>
ostream& operator<<(ostream&, const Dequeue<T>&);
template <typename T>
class node{
public:
node<T>* next;
node<T>* prev;
T data;
node(T data=T(), node<T>* next=NULL, node<T>* prev=NULL);
};
template <typename T>
node<T>::node(T _data, node<T>* _next, node<T>* _prev){
data = _data;
next = _next;
prev = _prev;
}
template <typename T>
class Dequeue{
private:
node<T>* head;
node<T>* tail;
int _size;
void clear();
public:
Dequeue();
~Dequeue();
Dequeue(const Dequeue<T>&);
const Dequeue<T>& operator=(const Dequeue<T>&);
friend ostream& operator<< <>(ostream&, Dequeue<T>&);
void push_front(T&);
void push_back(T&);
void pop_front();
void pop_back();
T front();
T back();
int size();
bool isEmpty();
};
template <typename T>
Dequeue<T>::Dequeue(){
_size = 0;
head = NULL;
tail = NULL;
}
template <typename T>
Dequeue<T>::~Dequeue(){
clear();
}
template <typename T>
void Dequeue<T>::clear(){
while(_size > 0)
pop_front();
}
template <typename T>
Dequeue<T>::Dequeue(const Dequeue<T>& other){
head = NULL;
tail = NULL;
_size = 0;
*this = other;
}
template <typename T>
const Dequeue<T>& Dequeue<T>::operator=(const Dequeue<T>& other){
if(this = &other)
return *this;
clear();
node<T>* np = other.head;
while(np!=NULL){
node<T>* p = new node<T>(np->data);
if(head == tail == NULL)
head = tail = p;
else{
tail->next = p;
p->prev = tail;
}
tail = p;
np = np->next;
}
_size = other._size;
return *this;
}
template <typename T>
ostream& operator<<(ostream& out_str, Dequeue<T>& other){
node<T>* np = other.head;
while(np != NULL){
out_str << np->data << endl;
np = np->next;
}
return out_str;
}
template <typename T>
void Dequeue<T>::push_front(T& object){
node<T>* np = new node<T>(object);
np->next = head;
head->prev = np;
head = np;
_size++;
}
template <typename T>
void Dequeue<T>::push_back(T& object){
node<T>* np = new node<T>(object);
tail->next = np;
np->prev = tail;
tail = np;
_size++;
}
template <typename T>
void Dequeue<T>::pop_front(){
node<T>* np = head;
head = head->next;
delete np;
head->prev=NULL;
_size--;
}
template <typename T>
void Dequeue<T>::pop_back(){
node<T>* np = tail;
tail = np->prev;
delete np;
tail->next = NULL;
size--;
}
template <typename T>
T Dequeue<T>::front(){
return head->data;
}
template <typename T>
T Dequeue<T>::back(){
return tail->data;
}
template <typename T>
int Dequeue<T>::size(){
return _size;
}
template <typename T>
bool Dequeue<T>::isEmpty(){
if(size == 0)
return true;
else
return false;
}
int main(){
Dequeue<int> A;
A.push_back(5);
A.push_front(6);
A.push_back(7);
cout << A << endl;
cin.get();
return 0;
}
そして、ここに私が得ているエラーがあります:
1>------ Build started: Project: Kevin, Configuration: Debug Win32 ------
1> Dequeue.cpp
1>c:\users\kevin nguyen\documents\visual studio 2010\projects\kevin\kevin\dequeue.cpp(184): error C2664: 'Dequeue<T>::push_back' : cannot convert parameter 1 from 'int' to 'int &'
1> with
1> [
1> T=int
1> ]
1>c:\users\kevin nguyen\documents\visual studio 2010\projects\kevin\kevin\dequeue.cpp(185): error C2664: 'Dequeue<T>::push_front' : cannot convert parameter 1 from 'int' to 'int &'
1> with
1> [
1> T=int
1> ]
1>c:\users\kevin nguyen\documents\visual studio 2010\projects\kevin\kevin\dequeue.cpp(186): error C2664: 'Dequeue<T>::push_back' : cannot convert parameter 1 from 'int' to 'int &'
1> with
1> [
1> T=int
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========