this is where i define my template and 3 additional classes
#ifndef PLANILHAH
#define PLANILHAH
#include <iostream>
#include "planilha_func.h"
template <class TIPO> class planilha {
friend class celula;
friend class listaT;
friend class elo;
protected:
celula * primeiro;
unsigned int count;
public:
planilha();
planilha(const planilha<TIPO>& origem);
~planilha(void)
TIPO obtem_valor(const unsigned int num_cel);
//many more methods
};
i dont know if I should make this a nested class in 'planilha' or not if i make it nested, how do i implement it? it needs to recieve the type 'TIPO'
template <class TIPO> class celula{
friend class listaT;
friend class elo;
protected:
unsigned int idCelula;
TIPO constante;
TIPO total;
listaT termos;
//etc
void insere(int novoCons);
void apaga();
void setIdCelula(unsigned int idCelula);
void setTotal(TIPO total);
TIPO getTotal() const;
TIPO getConstante() const;
void setConstante(TIPO constante);
};
I dont know if friend class is the best option, these are used in the class celula
class listaT {
friend class elo;
protected:
elo * primeiro;
public:
listaT();
~listaT();
listaT(listaT& origem);
};
class elo {
friend class listaT;
protected:
elo();
elo(unsigned int novaRef, double novoFator, bool x = true, elo * proxElo = NULL);
elo operator=(const elo& origem);
};
#endif;
heres where i implement them, i am getting 100 errors or more. i cant understand why
#include "planilha.h"
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
template <class TIPO>
planilha<TIPO>::planilha() {
primeiro = NULL;
count = 1;
}
template <class TIPO>
planilha<TIPO>::~planilha(void) {
celula * p = primeiro;
while(primeiro!=NULL) {
p=primeiro->prox;
delete primeiro
primeiro=p;
}
}
template <class TIPO>
planilha<TIPO>::planilha(const planilha<TIPO>& origem) {
this->count = origem.count;
celula * p1;
this->primeiro = NULL;
celula * p2;
for(p2 = origem.primeiro; p2!=NULL; p2=p2->prox) {
p1 = p2;
if(this->primeiro == NULL) {
this->primeiro = p1;
}
p1 = p1->prox;
}
}
template <class TIPO>
unsigned int planilha<TIPO>::getCount() const {
return count;
}
template <class TIPO>
void typename planilha<TIPO>::setCount(unsigned int count) {
this->count = count;
}
template <class TIPO>
celula * planilha<TIPO>::finder(unsigned int id) {
celula * resposta;
for(resposta=this->primeiro; resposta!=NULL; resposta=resposta->prox) {
if(resposta->idCelula == id) break;
}
return resposta;
}
template <class TIPO>
celula<TIPO>::celula() {
prox = NULL;
}
template <class TIPO>
celula<TIPO>::celula(unsigned int novoId, TIPO novaConstante, planilha<TIPO> * proxCel) {
idCelula = novoId;
constante = novaConstante;
total = novaConstante;
prox = proxCel;
}
template <class TIPO>
void celula<TIPO>::setTotal(TIPO total) {
this->total = total;
}
listaT::listaT() {
this->primeiro = NULL;
}
listaT::~listaT() {
elo * p = primeiro;
while(primeiro!=NULL) {
p=primeiro->prox;
delete primeiro;
primeiro=p;
}
}
listaT::listaT(listaT& origem) {
elo * p2;
elo * p1;
primeiro = NULL;
for(p2 = origem.primeiro; p2!=NULL; p2 = p2->prox) {
p1 = p2;
if(primeiro == NULL) {
primeiro = p1;
}
p1 = p1->prox;
}
}
bool listaT::vazia() {
return (primeiro == NULL);
}
void listaT::insere(int novaRef, double novoFator, bool absoluta) {
elo * p = primeiro;
elo * novoElo = new elo(novaRef, novoFator, absoluta);
if(vazia()) {
primeiro = novoElo;
} else {
while(p->prox!=NULL) {
p = p->prox;
}
p->prox = novoElo;
}
}
bool listaT::operator==(const listaT &listaT2) {
elo * p1 = this->primeiro;
elo * p2 = listaT2.primeiro;
bool resposta = true;
while(p1!=NULL && p2!=NULL) {
if(p1->fator != p2->fator || p1->referencia != p2->referencia || p1->absolut != p2->absolut) {
resposta = false;
}
p1=p1->prox;
p2=p2->prox;
}
if(p2!=NULL || p1!=NULL) {
resposta = false;
}
return resposta;
}
elo * listaT::getPrimeiro() {
elo * resposta;
resposta = primeiro;
return resposta;
}
elo::elo() {
prox = NULL;
}
elo::elo(unsigned int novaRef, double novoFator, bool x, elo * proxElo) {
referencia = novaRef;
fator = novoFator;
prox = proxElo;
absolut = x;
}
elo elo::operator=(const elo& origem) {
unsigned int r = origem->referencia;
double f = origem.fator;
bool x = origem.absolut;
elo p(r, f, x);
return p;
}
Have you got an include for the cpp files, and have you made sure that if you dont you need to put template class source code in the same file as the header
Related
This question already has answers here:
Why can templates only be implemented in the header file?
(17 answers)
Closed 4 months ago.
I'm trying to do a custom dynamic array class using templates, and currently the .h file looks like this:
//
// Created by juanfra on 10/10/22.
//
#ifndef IMAGENES_VDINAMICO_H
#define IMAGENES_VDINAMICO_H
#include <cmath>
#include <climits>
using namespace std;
template <typename T>
class VDinamico {
private:
int tamL, tamF;
T *contenedor;
public:
// CONSTRUCTORES
VDinamico<T>();
VDinamico<T>(int tamLog);
VDinamico<T>(const VDinamico<T> &origen);
VDinamico<T>(const VDinamico<T> &origen, unsigned int posicionInicial, unsigned int numElementos);
// OPERADORES
VDinamico &operator=(const VDinamico<T> &vector);
T operator[](int i);
// FUNCIONES
void insertar(const T &dato, unsigned int pos = UINT_MAX);
T borrar (unsigned int pos = UINT_MAX);
bool isGreater(const T& i, const T& j);
void ordenar();
void ordenarRev();
// GETTERS Y SETTERS
int getTamL() const;
void setTamL(int tamL);
int getTamF() const;
void setTamF(int tamF);
T *getContenedor() const;
void setContenedor(T *contenedor);
};
#endif //IMAGENES_VDINAMICO_H
While the .cpp file looks like this:
//
// Created by juanfra on 10/10/22.
//
#include "VDinamico.h"
template<class T>
VDinamico<T>::VDinamico() {
tamF = tamL = 0;
contenedor = new T[tamF];
}
template<class T>
VDinamico<T>::VDinamico(int tamLog) {
tamL = tamLog;
tamF = pow(tamL, 2.0);
contenedor = new T[tamF];
}
template<class T>
VDinamico<T>::VDinamico(const VDinamico<T> &origen) {
tamL = origen.tamL;
tamF = origen.tamF;
contenedor = new T[tamF];
for (int i = 0; i < tamL; i++) {
contenedor[i] = origen.contenedor[i];
}
}
template<class T>
VDinamico<T>::VDinamico(const VDinamico<T> &origen, unsigned int posicionInicial, unsigned int numElementos) {
tamL = numElementos;
tamF = pow(tamL, 2.0);
contenedor = new T[tamF];
for (int i = 0; i < numElementos; i++) {
contenedor[i] = origen.contenedor[i + posicionInicial];
}
}
// OPERADORES
template<class T>
VDinamico<T> &VDinamico<T>::operator=(const VDinamico<T> &vector) {
tamL = vector.tamL;
tamF = vector.tamF;
contenedor = new T[tamF];
T *contenedorAux = vector.contenedor;
for (int i = 0; i < tamL; i++) {
contenedor[i] = contenedorAux[i];
}
}
template<class T>
T VDinamico<T>::operator[](int i) {
return contenedor[i];
}
// FUNCIONES
template<class T>
void VDinamico<T>::insertar(const T &dato, unsigned int pos) {
tamF = this->getTamF();
tamL = this->getTamL();
if (tamF == tamL) { //< Si los tamanyos son iguales, duplico tamF e inserto
T *vAux = new T[tamF * 2];
for (int i = 0; i < tamL; i++) {
vAux[i] = contenedor[i];
}
delete[]contenedor;
contenedor = vAux;
}
if (pos == UINT_MAX) { //< Si la posición no se indica, insertamos por el final
contenedor[tamL++] = dato;
} else { //< Si la posición se indica, abrimos un hueco en la posición indicada copiando el resto del vector
for (int i = tamL - 1; i >= pos; i--) {
contenedor[i + 1] = contenedor[i];
}
}
this->setTamL(tamL + 1);
}
template<class T>
T VDinamico<T>::borrar(unsigned int pos) {
tamF = this->getTamF();
tamL = this->getTamL();
if (tamL*3 < tamF){ //< Si tamL es 3 veces menor que tamF, hago un nuevo vector de tamF/2
T *vAux = new T[tamF/2];
for (int i = 0; i < tamL; i++){
vAux[i] = contenedor[i];
}
delete[]contenedor;
contenedor = vAux;
}
if (pos == UINT_MAX){
return contenedor[tamL--];
} else {
for (int i = pos; i < tamL; i++){
contenedor[i] = contenedor[i+1];
}
}
this->setTamL(tamL--);
return contenedor;
}
template<class T>
bool VDinamico<T>::isGreater(const T& i, const T& j) {
return (i.getId() > j.getId());
}
template<class T>
void VDinamico<T>::ordenar() {
sort(contenedor, contenedor + tamL);
}
template<class T>
void VDinamico<T>::ordenarRev() {
sort(contenedor, contenedor + tamL, isGreater());
}
// GETTERS Y SETTERS
template<class T>
int VDinamico<T>::getTamL() const {
return tamL;
}
template<class T>
void VDinamico<T>::setTamL(int tamL) {
VDinamico::tamL = tamL;
}
template<class T>
int VDinamico<T>::getTamF() const {
return tamF;
}
template<class T>
void VDinamico<T>::setTamF(int tamF) {
VDinamico::tamF = tamF;
}
template<class T>
T *VDinamico<T>::getContenedor() const {
return contenedor;
}
template<class T>
void VDinamico<T>::setContenedor(T *contenedor) {
VDinamico::contenedor = contenedor;
}
My problem is that, in the .h, everytime I use <T>, it throws me an error like:
error: expected unqualified-id before ‘)’ token in line 20
error: expected unqualified-id before ‘int’ in line 22
error: expected unqualified-id before ‘const’ in line 24
and so on...
What am I doing wrong?
I removed all the <T> in the .h file, to not split a template class across .h and .cpp files.
Here is why: Why can templates only be implemented in the header file?
Ok i wrote this class that counts how many leaves are there in a binary tree:
#ifndef _UTIL_BIN_TREE_H_
#define _UTIL_BIN_TREE_H_
#include"bin_treec.h"
template <class T>
class util_bin_tree
{
public:
static int n_leaf(const Bin_treec<T>& T) {
int i;
if (!T.empty())
{
if (T->spazio[i].sinistro == NULL && T->spazio[i].destro == NULL)
return 1;
else
return n_leaf(T->T->spazio[i + i].sinistro) + n_leaf(T->T->spazio[i + i].destro);
}
};
static int n_level(const Bin_treec<T>& T, int i)
{
//
};
};
#endif
this is the class that creates the binary tree:
#ifndef _Bin_treecC_H_
#define _Bin_treecC_H_
#include "Bin_tree.h"
#include "exceptions.h"
template <class T>
class Bin_treec : public Bin_tree<T, int> {
static const int NIL = -1;
public:
typedef typename Bin_tree<T, int>::value_type value_type;
typedef typename Bin_tree<T, int>::Nodo Nodo;
struct _cella {
Nodo genitore;
Nodo sinistro;
Nodo destro;
value_type valore;
};
typedef struct _cella Cella;
// costruttori e distruttori
Bin_treec();
Bin_treec(int);
~Bin_treec();
// operatori
void create();
bool empty() const;
Nodo root() const;
Nodo parent(Nodo) const;
Nodo sx(Nodo) const;
Nodo dx(Nodo) const;
bool sx_empty(Nodo) const;
bool dx_empty(Nodo) const;
//void costr(Bin_treec<T>);
void erase(Nodo);
T read(Nodo) const;
void write(Nodo, value_type);
void ins_root();
void ins_sx(Nodo);
void ins_dx(Nodo);
private:
int MAXLUNG;
Cella* spazio;
int nNodi;
Nodo inizio;
Nodo libera;
};
template <class T>
Bin_treec<T>::Bin_treec()
{
MAXLUNG = 100;
spazio = new Cella[MAXLUNG];
create();
}
template <class T>
Bin_treec<T>::Bin_treec(int nNodi) : MAXLUNG(nNodi)
{
spazio = new Cella[nNodi];
create();
}
template <class T>
Bin_treec<T>::~Bin_treec()
{
erase(inizio);
delete[] spazio;
}
template <class T>
void Bin_treec<T>::create()
{
inizio = NIL;
for (int i = 0; i < MAXLUNG; i++)
{
spazio[i].sinistro = (i + 1) % MAXLUNG;
}
libera = 0;
nNodi = 0;
}
template <class T>
bool Bin_treec<T>::empty() const
{
return(nNodi == 0);
}
template <class T>
typename Bin_treec<T>::Nodo Bin_treec<T>::root() const
{
return(inizio);
}
template <class T>
typename Bin_treec<T>::Nodo Bin_treec<T>::parent(Nodo n) const
{
if (n != inizio)
return (spazio[n].genitore);
else
return(n);
}
template <class T>
typename Bin_treec<T>::Nodo Bin_treec<T>::sx(Nodo n) const
{
if (!sx_empty(n))
return (spazio[n].sinistro);
else
return(n);
};
template <class T>
typename Bin_treec<T>::Nodo Bin_treec<T>::dx(Nodo n) const
{
if (!dx_empty(n))
return (spazio[n].destro);
else
return(n);
}
template <class T>
bool Bin_treec<T>::sx_empty(Bin_treec<T>::Nodo n) const
{
return (spazio[n].sinistro == NIL);
}
template <class T>
bool Bin_treec<T>::dx_empty(Bin_treec<T>::Nodo n) const
{
return (spazio[n].destro == NIL);
}
template <class T>
void Bin_treec<T>::ins_root()
{
if (inizio == NIL)
{
inizio = libera;
libera = spazio[libera].sinistro;
spazio[inizio].sinistro = NIL;
spazio[inizio].destro = NIL;
nNodi++;
}
else
throw RootExists();
}
template <class T>
void Bin_treec<T>::ins_sx(Nodo n)
{
if (inizio == NIL)
throw EmptyTree();
if (n == NIL)
throw NullNode();
if (spazio[n].sinistro != NIL)
throw NodeExists();
if (nNodi >= MAXLUNG)
throw FullSize();
else
{
Nodo q = libera;
libera = spazio[libera].sinistro;
spazio[n].sinistro = q;
spazio[q].sinistro = NIL;
spazio[q].genitore = n;
spazio[q].destro = NIL;
nNodi++;
}
}
template <class T>
void Bin_treec<T>::ins_dx(Nodo n)
{
if (inizio == NIL)
throw EmptyTree();
if (n == NIL)
throw NullNode();
if (spazio[n].destro != NIL)
throw NodeExists();
if (nNodi >= MAXLUNG)
throw FullSize();
else
{
Nodo q = libera;
libera = spazio[libera].sinistro;
spazio[n].destro = q;
spazio[q].genitore = n;
spazio[q].sinistro = NIL;
spazio[q].destro = NIL;
nNodi++;
}
}
template <class T>
void Bin_treec<T>::erase(Nodo n)
{
if (n != NIL) {
if (!sx_empty(n))
erase(spazio[n].sinistro);
if (!dx_empty(n))
erase(spazio[n].destro);
if (n != inizio) {
Nodo p = parent(n);
if (spazio[p].sinistro == n)
spazio[p].sinistro = NIL;
else
spazio[p].destro = NIL;
}
else
inizio = NIL;
nNodi--;
spazio[n].sinistro = libera;
libera = n;
}
else
throw NullNode();
}
template <class T>
T Bin_treec<T>::read(Nodo n) const
{
if (n != NIL)
return (spazio[n].valore);
else
throw NullNode();
}
template <class T>
void Bin_treec<T>::write(Nodo n, value_type a)
{
if (n != NIL)
spazio[n].valore = a;
else
throw NullNode();
}
#endif /* _Bin_treecC_H_ */
this is the main
#include "util_bin_tree.h"
#include <iostream>
using namespace std;
int main() {
Bin_treec<int> T;
typename Bin_treec<int>::Nodo n1 = 0, n2 = 0;
T.ins_root();
T.write(T.root(), 1);
n1 = T.root();
T.ins_sx(n1);
T.ins_dx(n1);
T.write(T.sx(n1), 2);
n1 = T.dx(n1);
T.write(n1, 3);
T.ins_dx(n1);
T.write(T.dx(n1), 4);
T.print();
cout << T;
n_leaf(T); // here i have error C3681
It says that my function is undeclared but i don't know why.
Full error is :
Severity Code Description Project File Line Suppression State
Error C3861 'n_leaf': identifier not found esercizio C:\Users\mypc\source\repos\esercizio\test.cpp 26
I also have a virtual bin_tree header where util_bin_tree is not specified, but I don't think it really matters because i don't use any functions related to the tree.
Also, is it my way to pass an array from T correct? I just wanted to pass an entire object to another class function and the compiler doesn't find any error for the time being. But i can't test the function just because of that problem. Any help?
Your n_leaf function is not a namespace-scoped function, it's a static function inside the util_bin_tree class. You can call it with util_bin_tree<int>::n_leaf(T).
Since your util_bin_tree class does not have any data members, you could use namespace util_bin_tree { } instead, and put template<class T> before each function.
I'm practicing c++ and I got stuck on the following codes trying to optimize them. I'd like to know if there is something I can do to optimize their method's implementation. Because the methods are the same except for the consts. Thanks in advance.
dominios.h
class HP {
private:
int valor;
static const int LIMITE_INFERIOR = 0;
static const int LIMITE_SUPERIOR = 1000;
public:
void setValor(int);
int getValor() {
return valor;
}
};
class MP {
private:
int valor;
static const int LIMITE_INFERIOR = 0;
static const int LIMITE_SUPERIOR = 500;
public:
void setValor(int);
int getValor() {
return valor;
}
};
dominios.cpp
void HP::setValor(int valor) {
if (valor < LIMITE_INFERIOR) this->valor = LIMITE_INFERIOR;
else if (valor > LIMITE_SUPERIOR) this->valor = LIMITE_SUPERIOR;
else this->valor = valor;
}
void MP::setValor(int valor) {
if (valor < LIMITE_INFERIOR) this->valor = LIMITE_INFERIOR;
else if (valor > LIMITE_SUPERIOR) this->valor = LIMITE_SUPERIOR;
else this->valor = valor;
}
As you can see the setValor of both classes are the same. I tried to do hierarchy using a "template" but that didn't work for me because of the consts.
this->valor = std::clamp(valor, LIMITE_INFERIOR, LIMITE_SUPERIOR);
template <typename Tag, int lo, int hi>
class Metric {
private:
int valor;
public:
void setValor(int v) { valor = std::clamp(v, lo, hi); }
int getValor() { return valor; }
};
struct HPTag;
using HP = Metric<HPTag, 0, 1000>;
struct MPTag;
using MP = Metric<MPTag, 0, 500>;
Let me post my code first:
Set.h
#pragma once
#include <string>
template<class _type> class Set
{
public:
Set();
Set m_add(Set _set1, Set _set2);
void m_addElem(Set *const _set, _type _elem);
void m_deleteElem(Set *const _set, _type _elem);
void m_addArray(_type _arr[], int _size);
Set(Set &_coll);
void operator+(_type _num);
void operator+(_type _elem[]);
Set operator+(Set *const _set);
void operator-(_type _num);
Set & operator=(Set &_set);
void m_display();
int m_check(_type elem);
~Set(void);
private:
_type * m_pelements;
int m_setSize;
};
Set.cpp
#pragma warning( disable : 4996 )
#include "Set.h"
#include <iostream>
#include <string>
template <class _type>
Set<_type>::Set()
{
m_setSize = 0;
}
template <class _type>
Set<_type>::Set(Set<_type> &_coll)
{
m_setSize = _coll.m_setSize;
m_pelements = new _type[_coll.m_setSize];
for (int i = 0;i<m_setSize;i++)
{
m_pelements[i] = _coll.m_pelements[i];
}
}
template <class _type>
Set<_type>::~Set()
{
delete [] m_pelements;
}
template <class _type>
Set<_type> Set<_type>::m_add(Set<_type> _set1, Set<_type> _set2)
{
Set<_type> finalSet;
finalSet = _set1;
for (int i = 0;i<_set2->m_setSize;i++)
{
m_addElem(finalSet, _set2->m_pelements[i]);
}
return finalSet;
}
template <class _type>
void Set<_type>::m_addElem(Set<_type> *const _set, _type _elem)
{
if (_set->m_setSize == 0)
{
_set->m_pelements = new _type[1];
_set->m_pelements[0] = _elem;
_set->m_setSize += 1;
}
else
{
_set->m_setSize += 1;
_type * helpElements = new _type[_set->m_setSize];
std::copy(_set->m_pelements, _set->m_pelements + _set->m_setSize-1, helpElements);
helpElements[_set->m_setSize-1] = _elem;
delete [] _set->m_pelements;
_set->m_pelements = helpElements;
/*
_type * helpElements = new _type[_set->m_setSize];
for (int i = 0;i<_set->m_setSize;i++)
{
helpElements[i] = _set->m_pelements[i];
}
delete _set->m_pelements;
_set->m_setSize += 1;
_set->m_pelements = new _type[_set->m_setSize];
for (int i = 0;i<_set->m_setSize;i++)
{
_set->m_pelements[i] = helpElements[i];
}
_set->m_pelements[_set->m_setSize-1] = _elem;
*/
}
}
template <class _type>
void Set<_type>::m_deleteElem(Set<_type> *const _set, _type _elem)
{
int index = _set->m_check(_elem);
if (index >= 0)
{
int k = 0;
_set->m_setSize -= 1;
_type * temp = new _type[_set->m_setSize];
for (int i = 0;i<_set->m_setSize;i++)
{
if (i == index)
k++;
temp[i] = _set->m_pelements[i+k];
}
delete [] _set->m_pelements;
_set->m_pelements = temp;
}
}
template <class _type>
void Set<_type>::m_addArray(_type _elem[], int size)
{
for (int i = 0;i<size;i++)
{
m_addElem(this,_elem[i]);
}
}
template <class _type>
void Set<_type>::operator+( _type _elem)
{
m_addElem(this,_elem);
}
template <class _type>
Set<_type> Set<_type>::operator+(Set<_type> *const _set)
{
return m_add(this,_set);
}
template <class _type>
void Set<_type>::operator+( _type _elem[])
{
m_addArray(this,_elem);
}
template <class _type>
void Set<_type>::operator-( _type _elem)
{
m_deleteElem(this,_elem);
}
template <class _type>
Set<_type> & Set<_type>::operator=(Set<_type> &_set)
{
if(&_set==this) return *this;
delete [] m_pelements;
m_setSize = _coll.m_setSize;
m_pelements = new _type[_coll.m_setSize];
for (int i = 0;i<m_setSize;i++)
{
m_pelements[i] = _coll.m_pelements[i];
}
}
template <class _type>
void Set<_type>::m_display()
{
for (int i = 0;i<m_setSize;i++)
{
std::cout << m_pelements[i] << " " ;
}
std::cout << std::endl;
}
template <class _type>
int Set<_type>::m_check(_type _elem)
{
for (int i = 0;i<m_setSize;i++)
{
if (m_pelements[i] == _elem)
return i;
}
return -1;
}
Main.cpp
#pragma warning( disable : 4996 )
#include "Set.h"
#include "Set.cpp"
#include <iostream>
int main()
{
Set<std::string> zbior1;
zbior1 + std::string("abc");
zbior1 + std::string("abcd");
zbior1 + std::string("abcdef");
zbior1 + std::string("XD");
zbior1.m_display();
zbior1 - "XD";
zbior1.m_display();
std::string tablica[3] = {"ala", "ma", "kota" };
zbior1.m_addArray(tablica,3);
zbior1.m_display();
Set<std::string> zbior2;
zbior2 + std::string("abDDc");
zbior2 + std::string("abcdDD");
zbior2 + std::string("abcdeDDf");
zbior2 + std::string("XDDD");
zbior2.m_display();
Set<std::string> zbior3;
zbior3 = zbior1 + zbior2; //HERE'S THE PROBLEM
}
Problem appears int the last line of Main.cpp
When arguments of Set operator+ are (Set *const _set) I get error " no operator found which takes a right-hand operand of type 'Set<_type>' (or there is no acceptable conversion)'" and if i remove *const there's different error saying "cannot convert parameter 1 from 'Set<_type> *const ' to 'Set<_type>'"
I have no idea how to repair it.
Your
Set operator+(Set *const _set);
is defined as taking a const pointer (which is for sure what you don't want), but you then pass to it an object instead, and not the address of an object. Pass by reference, like
Set operator+(Set const & _set);
Try reading Operator overloading for a very good introduction to the subject.
This method:
Set operator+(Set *const _set);
should probably look like:
Set operator+(Set const &rhs) const;
if it's going to stay a method. See the link in vsoftco's answer for why it should probably be a free non-friend function instead, implemented in terms of operator+=.
You pasted the same message for both errors, afaics, but at least one is complaining about the attempt to pass zbior2 to a method expecting a pointer.
I have overloaded the "+" operator so when I make class1 + class1 = class2 . The only Problem is when I pass it to another object.
The "+" operator is already overloaded so when you make class2 + class1, it adds the class1 object to the class2 object.
Here is the example:
#include<iostream.h>
class Figura {
public:
int x, y, poz;
int tip; //1 = punct ; 2 = dreapta; 3 = dreptunghi
Figura() { };
Figura(const Figura&) { };
};
class Grup {
private:
int nr_elemente;
Figura **figuri;
public:
int i;
Grup(int nr_el) {
nr_elemente = nr_el;
figuri = new Figura*[nr_elemente];
i = 0;
}
Grup() {
nr_elemente = 100;
figuri = new Figura*[nr_elemente];
i = 0;
}
~Grup() { /*delete[] figuri;*/ };
Grup(const Grup&) { };
Grup& operator=(const Grup&) {
return *this;
}
int _nr_elemente() {
return i;
}
void afiseaza_elemente() {
for(int j = 0; j < i; j++)
cout<<"Figura nr:"<<j<<" tip :"<<figuri[j]->tip<<" x:"<<figuri[j]->x<<" y:"<<figuri[j]->y<<" poz:"<<figuri[j]->poz<<endl;
}
friend Grup operator+(const Figura& fig1, const Figura& fig2) {
Grup grt;
grt + fig1;
grt + fig2;
cout<<grt.i<<endl; // current number of elements begining with 0, so it should print 1 (fig1 and fig2)
grt.afiseaza_elemente(); // prints the elements attributes
return grt;
};
friend Grup operator+(const Grup& gr1, const Grup& gr2) {};
void operator+(const Figura& fig);
};
void Grup::operator+(const Figura& fig) {
Grup::figuri[Grup::i] = new Figura;
Grup::figuri[Grup::i]->tip = fig.tip;
Grup::figuri[Grup::i]->poz = fig.poz;
if(fig.tip == 2) {
Grup::figuri[Grup::i]->x = fig.x;
Grup::figuri[Grup::i]->y = 0;
} else if(fig.tip == 3) {
Grup::figuri[Grup::i]->x = fig.x;
Grup::figuri[Grup::i]->y = fig.y;
}
else {
Grup::figuri[Grup::i]->x = 0;
Grup::figuri[Grup::i]->y = 0;
}
Grup::i++;
}
class Punct : public Figura
{
public:
Punct(int poz) {
Punct::tip = 1;
Punct::poz = poz;
}
};
class Segment : public Figura
{
public:
Segment(int poz, int x) {
Segment::tip = 2;
Segment::poz = poz;
Segment::x = x;
}
};
class Dreptunghi : public Figura
{
public:
Dreptunghi(int poz, int x, int y) {
Dreptunghi::tip = 3;
Dreptunghi::poz = poz;
Dreptunghi::x = x;
Dreptunghi::y = y;
}
};
void main(void) {
Grup gr(10);
Punct pct(1);
Segment sgm(3, 5);
gr + pct;
gr + sgm;
Grup gr2 = pct + sgm;
cout<<"--------------------------"<<endl;
cout<<gr2.i<<endl; // prints a weird number
gr2.afiseaza_elemente(); // prints the elemenets atributes, but because gr2.i is negative shows nothing
}