There is an exercise that I tried to do and that does not compile me, despite correcting several foolish failures, but the issue is that it fails due to (I think) errors in the approach.
this is the statement:
Develop the template method void dll_t :: swap (const int i, const int j) that exchanges the value of the data attribute of the i-th node (node in position i of the list) with the jth node.
#pragma once
#include <cassert>
#include <iostream>
#include <cstdio>
#include "dll_node_t.hpp"
using namespace std;
namespace AED {
template <class T>
class dll_t {
private:
dll_node_t<T>* head_;
dll_node_t<T>* tail_;
int sz_;
public:
dll_t(void);
dll_t(const dll_t& L);
virtual ~dll_t(void);
void insert_tail(dll_node_t<T>*);
void insert_head(dll_node_t<T>*);
dll_node_t<T>* extract_tail(void);
dll_node_t<T>* extract_head(void);
dll_node_t<T>* get_tail(void);
dll_node_t<T>* get_head(void);
bool empty(void) const ;
int get_nz (void) const{}
void unlink(dll_node_t<T>*);
dll_node_t<int>* find (const int v);
void dll_union (dll_t<int>& A, dll_t<int>& B, dll_t<int>& C);
int get_size(void) const;
void swap (const int i,const int j);
// esto: contar los nonzeros
//modif: suma elementos matriz
//double suma (void) const; // esto pa arriba
//int get_np (void) const;
//double suma_mat (void) const;
void invert(void);
ostream& write(ostream& os) const;
};
template<class T>
void dll_t<T>::swap(const int i,const int j){
dll_node_t<T>* aux = NULL;
dll_node_t<T>* auxi = get_data();
dll_node_t<T>* auxj = get_data();
while(aux != NULL){
while(auxi != NULL && auxj!=NULL){
auxi->get_data()=aux;
auxi->get_data() = aux->get_data();
aux = auxj ->get_data();
auxj = auxj -> get_next();
auxi = auxi -> get_next();
}
aux = aux -> get_next();
}
}
template<class T>
void dll_t<int>::dll_union (dll_t<int>& A, dll_t<int>& B, dll_t<int>& C){
dll_node_t<int>* aux1 = A.get_head();
dll_node_t<int>* aux2 = B.get_head();
while (aux2 != NULL || aux1 != NULL){
if (B.find((aux1->get_data()))){
C.insert_tail(aux1);
}
if((aux1->get_data()) != (aux2->get_data())){
C.insert_tail(aux1);
C.insert_tail(aux2);
}
aux2=aux2->get_next();
aux1=aux1->get_next();
}
}
template<class T>
dll_node_t<int>* dll_t<int> :: find(const int v){
assert(!empty());
dll_node_t <int>* aux = get_head();
dll_node_t <int>* encontrado = NULL;
while(aux !=NULL){
if ((aux -> get_data())== v){
encontrado= aux;
}
aux = aux-> get_next();
}
return encontrado;
}
template<class T>
void dll_t<T> :: invert(void){
assert(!empty());
dll_node_t<T>* aux = get_tail();
while (aux != NULL){
extract_tail(aux);
aux = aux -> get_prev();
}
while (aux !=NULL){
insert_tail(aux);
aux=aux->get_next();
}
}
template <class T>
dll_t<T>::dll_t(void) :
head_(NULL),
tail_(NULL),
sz_(0) {
}
template <class T>
dll_t<T>::dll_t(const dll_t& L) :
head_(NULL),
tail_(NULL),
sz_(0) {
dll_node_t<T>* aux = L.head_;
while (aux != NULL) {
insert_tail(new dll_node_t<T>(aux->get_data()));
aux = aux->get_next();
}
}
template <class T>
dll_t<T>::~dll_t(void) {
dll_node_t<T>* aux = NULL;
while (head_ != NULL) {
aux = head_;
head_ = head_->get_next();
delete aux;
aux = NULL;
}
sz_ = 0;
head_ = NULL;
tail_ = NULL;
}
The errors that the swap method gives me are these:
In member function 'void AED::dll_t<T>::swap(int, int)':
64 37 [Error] there are no arguments to 'get_data' that depend on a template parameter, so a declaration of 'get_data' must be available [-fpermissive]
64 37 (if you use '-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)
65 37 [Error] there are no arguments to 'get_data' that depend on a template parameter, so a declaration of 'get_data' must be available [-fpermissive]
86 10 [Error] prototype for 'void AED::dll_t<int>::dll_union(AED::dll_t<int>&, AED::dll_t<int>&, AED::dll_t<int>&)' does not match any in class 'AED::dll_t<int>'
40 8 [Error] candidate is: void AED::dll_t<T>::dll_union(AED::dll_t<int>&, AED::dll_t<int>&, AED::dll_t<int>&) [with T = int]
This is the get_data();
method
template <class T>
T dll_node_t<T>::get_data(void) const
{
return data_;
}
Thanks