Problem z Bst i Complex (zarówno Bst jak i Complex są strukturami)

0

Dzień dobry!

Moim zadaniem jest stworzyć Drzewo binarne zawierające w sobie liczby urojone. Jedno i drugie musi być strukturą (struct) oraz jedno i drugie musi mieć swój szablon. Mój kod wygląda jak poniżejj, jednak cały czas przy kompilacji występuje błąd i nie potrafię dojść do tego, dlaczego tak się dzieje. Będę wdzięczny za każdą pomoc

#include "pch.h"
#include <iostream>
#include <memory>
#include <fstream>
using namespace std;
// definicja struktury i deklaracje funkcji
 
template <typename T>
struct Complex
{
    T real;
    T img;
    Complex(T dataR, T dataI)
    {
        this->real = dataR;
        this->img = dataI;
    }
    Complex(T data)
    {
        this->real = data;
        this->img = data;
    }
    Complex()
    {
        this->real = 0;
        this->img = 0;
    }
};
 
template <typename T>
struct BstNode
{
    std::unique_ptr<BstNode> left;
    T value;
    std::unique_ptr<BstNode> right;
    BstNode(int value) {
        this->value = value;
        left = nullptr;
        right = nullptr;
    }
    BstNode() {
        this->value = 0;
        left = nullptr;
        right = nullptr;
    }
};
 
template <typename T>
std::fstream& operator << (std::fstream& stream, const Complex<T>& p) {
    stream.open("plik.txt");
    if (stream)
    {if (p.img>=0)
        return stream  << p.real << "+" << p.img << "i";
    else
        return stream  << p.real << " " << p.img << "i";
    }
 
    stream.close();
}
 
template <typename T>
std::ostream & operator<<(std::ostream& stream, BstNode<T> *bst)
{
    if (bst != nullptr)
        stream << bst->left.get() << bst->value << endl << bst->right.get();
    return stream;
}
 
template <typename T>
void addToTree(std::unique_ptr<BstNode<T>>& bst, int value)
{
    if (bst) {
        if (bst->value > value) {
            addToTree(bst->left, value);
        }
        else if (bst->value < value) {
            addToTree(bst->right, value);
        }
    }
    else {
        bst = std::make_unique<BstNode<T>>(value);
    }
}
 
int main()
{
    std::unique_ptr <BstNode<Complex<int>>> bst = nullptr;
    Complex<int> z1;
    z1.real = 10;
    z1.img = 10;
    addToTree(bst, z1);
    addToTree(bst, z1);
    addToTree(bst, z1);
    addToTree(bst, 15);
    cout << bst;
    cout << minimum(bst.get()) << endl;
    cout << maximum(bst.get()) << endl;
 
 
 
    //cout << bst << endl;
}
1

1. Jedyne co jestem w stanie tutaj wskazać, to wyświetlenie: cout << bst. To się nie uda, jeśli chcesz wyświetlić wartość wskaźnika, to musisz go pobrać .get(), lub wziąć surowy adres &.
2. Nie wiemy jaki to błąd, a nie jesteśmy w stanie sami odpalić tego kodu, bo brakuje nam definicji na przykład addToTree() oraz minimum i 'maximum`. Gdybyś podał przynajmniej linijkę, to zawsze byłby to dla nas jakiś punkt zaczepienia.

0

Dzięki, za odpowiedzi. Dokonałem pewnych zmian w kodzie, jednak dalej wyskakuje poniższy błąd:

Błąd C2679 dwuargumentowy "<<": nie znaleziono żadnego operatora, który przyjmuje prawostronny operand typu "T" (lub nie istnieje akceptowalna konwersja)

Tutaj załączam kod:

#include "pch.h"
#include <iostream>
#include <memory>
//#include <fstream>
using namespace std;
// definicja struktury i deklaracje funkcji

template <typename T>
struct Complex
{
	T real;
	T img;
	Complex(T dataR, T dataI)
	{
		this->real = dataR;
		this->img = dataI;
	}
	Complex(T data)
	{
		this->real = data;
		this->img = data;
	}
	Complex()
	{
		this->real = 0;
		this->img = 0;
	}
};

template <typename T>
struct BstNode
{
	std::unique_ptr<BstNode> left;
	T value;
	std::unique_ptr<BstNode> right;
	BstNode(T value) {
		this->value = value;
		left = nullptr;
		right = nullptr;
	}
	BstNode() {
		this->value = 0;
		left = nullptr;
		right = nullptr;
	}
};
template<typename T>
bool operator<(const Complex<T>& a, const Complex<T>& b)
{
	if (a.real < b.real)
	{
		return true;
	}
	else return false;
}
template<typename T>
bool operator>(const Complex<T>& a, const Complex<T>& b)
{
	if (a.real > b.real)
	{
		return true;
	}
	else return false;
}

template <typename T>
std::ostream & operator<<(std::ostream& stream, BstNode<T> *bst)
{
	if (bst != nullptr)
		stream << bst->left.get() << bst->value << endl << bst->right.get();
	return stream;
}
template <typename T>
void addToTree(std::unique_ptr<BstNode<T>>& bst, T value)
{
	if (bst) {
		if (bst->value > value) {
			addToTree(bst->left, value);
		}
		else if (bst->value < value) {
			addToTree(bst->right, value);
		}
	}
	else {
		bst = std::make_unique<BstNode<T>>(value);
	}
}
template <typename T>
auto minimum (BstNode<T>* root)
{
	BstNode<T>* current = root;
	if (root = NULL)
	{
		std::cout << "drzewo jest puste!\n";
	}
	while (current->left != NULL)
	{
		current = current->left.get();
	}
	std::cout << current->value;
	//return current->value;
	
}
int main()
{
	Complex<float> z1;
	std::unique_ptr <BstNode<Complex<float>>> bst = NULL;
	
	z1.real = 10;
	z1.img = 10;
	addToTree(bst, z1);
	addToTree(bst, z1);
	addToTree(bst, z1);
	
	cout << bst.get();
	minimum(bst.get());
}

2

Zapomniałeś zrobić operator << dla Complex.

1

Operator dla Complex jest, jednak jako szablon. Problem jest taki, że tutaj chodzi bardziej o wyspecyfikowanie tego:

template <typename T>
std::ostream & operator<<( std::ostream& stream, BstNode<Complex<int>> *bst )
{

Ale wtedy rzecz jasna nie trzeba nawet robić z tego szablonu.

0

Zrobiłem operator << dla Complex, tak jak @_0x666_ napisał, ponieważ potrzebuje, aby struktura Complex mogła przyjmować różne typy i wszystko działa. Dziękuje Wam bardzo za pomoc :)

1 użytkowników online, w tym zalogowanych: 0, gości: 1