Quantcast
Channel: Forum Pasja Informatyki - Najnowsze pytania bez odpowiedzi
Viewing all articles
Browse latest Browse all 21942

Zależność pomiędzy kontenerem stworzonego na podstawie szablonu, a konstruktorami innych klas

$
0
0

Bardzo proszę o pomoc w wyjaśnieniu czego nie rozumiem z dziedziny szablonów.
Mam do napisania program, który przechowuje bazę danych sieci sklepów z zabawkami. Od kilku dni spędza mi on sen z powiek, bo różne próby przebudowania tego programu kończą się podobną serią błędów, których nie potrafię zlikwidować... Treść plików cpp i hpp bez funkcji main(), komunikat kompilatora oraz link do całego projektu zamieszczam poniżej:

//==========================================
// Name        : templateIterator.hpp
//==========================================

#ifndef TEMPLATEITERATOR_HPP
#define TEMPLATEITERATOR_HPP

#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <vector>
#include <windows.h>

using namespace std;

template <typename T, typename P>
class Iterator {
private:
    P* pointer;
    short item;
public:

    Iterator(P& container) : pointer (&container), item(0) {}

    T& operator*() {
        return (*pointer)[item];
    }

    Iterator& operator++() {    // później spróbować bez referencji i gwiazdek
        item++;
        return *this;
    }

    Iterator& operator++(int) {
        item++;
        return *this;
    }

    Iterator& operator--() {
        item--;
        return *this;
    }

    Iterator& operator--(int) {
        item--;
        return *this;
    }

    Iterator& operator+(int add) {
        item += add;
        return *this;
    }

    Iterator& operator-(int subtract) {
        item -= subtract;
        return *this;
    }

    Iterator& operator=(const Iterator& another) {
        pointer = another.pointer;
        item = another.item;
    }

    bool operator==(const Iterator& another) {
        return (pointer == another.pointer && item == another.item) ? true : false;
    }

    bool operator!=(const Iterator& another) {
        return !(*this == another);
    }
};

#endif
//==========================================
// Name        : templateContainer.hpp
//==========================================

#ifndef TEMPLATECONTAINER_HPP
#define TEMPLATECONTAINER_HPP

#include "templateIterator.hpp"

template <typename T>
class Container {
private:
    T* table;
    static const short initial = 5;
    unsigned short capacity;
    unsigned short quantity;
public:
    typedef Iterator<T, Container<T> > iterator;

    Container(int cccc) : capacity(initial), quantity(0), table(new T[capacity]) {} // brakuje jedynie argumentów

    ~Container() {
        delete[] table;
    }

    Container(const Container& another) {
        quantity = another.quantity();
        capacity = another.capacity();
        if (table != 0)
            delete[] table;
        table = new T[capacity];
        for (unsigned short i = 0 ; i < quantity; i++)
            table[i] = another.table[i];
    }

    Container& operator=(const Container& another) {
        quantity = another.quantity;
        capacity = another.quantity;
        if (table != NULL)
            delete[] table;
        table = new T[capacity];
        for (unsigned short i = 0 ; i < quantity; i++)
            table[i] = another.table[i];
        return *this;
    }

    T& operator[](int position) const {
        return table[position];
    }

    void erase(int position) {
        for (unsigned short i = position ; i < quantity; i++)
            table[i] = table[i+1];
        quantity--;
    }

    void insert(const T& element, int position) {
        quantity++;
        if (quantity >= capacity)
            resize();
        T* temp = new T[capacity];
        for (unsigned short i = 0; i < position; i++)
            temp[i] = table[i];
        temp[position] = element;
        for (unsigned short i = position + 1; i < capacity; i++)
            temp[i] = table[i];
        delete[] table;
        table = temp;
    }

    void push_back(const T& element) {
        insert(element, quantity);
    }

    void pop_back() {
        erase(quantity - 1);
    }

    void resize() {
        capacity+=5;
        T* temp = new T[capacity];
        for (unsigned short i = 0; i < quantity; i++)
            temp[i] = table[i];
        delete[] table;
        table = temp;
    }

    void clear() {
        quantity = 0;
    }

    int getQuantity() {
        return quantity;
    }

    iterator begin() {
        return iterator(*this);
    }

    iterator end() {
        return iterator(*this) + quantity;
    }
};

#endif

// Iterator it --> invalid use of template-name iterator without an argument list
// int Container::size() --> template <class T> class Container used without template parameters
//==========================================
// Name        : entrepreneur.hpp
//==========================================

#ifndef ENTREPRENEUR_HPP
#define ENTREPRENEUR_HPP

#include "templateContainer.hpp"

class Toy {
public:
    string name;
    double price;
    int amount;
    Toy(string name, double price, int amount);
    ~Toy() {}
    friend ostream& operator<<(ostream& out, const Toy& toy);
    friend istream& operator>>(istream& in, Toy& toy);
};

class Shop {
private:
    string trademark;
    string address;
    vector<Toy> Toys;
public:
    Shop(string trademark, string address);
    ~Shop() {}
    void addToys();
    void deleteToy(string name);
    friend ostream& operator<<(ostream& out, const Shop& shop);
    friend istream& operator>>(istream& in, Shop& shop);
};

class Chain {
private:
    string trademark;
    unsigned short amount;
    Container<Shop> Shops;
public:
    Chain(string trademark);
    ~Chain() {}
    void addShop();
    friend ostream& operator<<(ostream& out, const Chain& chain);
    friend istream& operator>>(ostream& in, Chain& chain);
};

double controlPrice(double value);
void pressKey();
void clearScreen();

#endif
//==========================================
// Name        : entrepreneur.cpp
//==========================================

#include "entrepreneur.hpp"

Toy::Toy(string name, double price, int amount) : name(name), price(price), amount(amount) {}

ostream& operator<<(ostream& out, const Toy& toy) {
    out << left << setw(20) << toy.name;
    out << left << setw(7) << toy.price;
    out << left << setw(7) << toy.amount;
    return out;
}

Shop::Shop(string trademark, string address) : trademark(trademark), address(address) {}

void Shop::addToys() {
    cout << "[ 3] --> Add a toy"<< endl << endl;
    string name;
    double price;
    int amount;
    cout << "Name: ";
    cin.ignore(USHRT_MAX,'\n');
    getline(cin, name);
    if (name.size() > 20)
        do {
            cout << "Toy's name can have only 20 letters. Try again: ";
            getline(cin, name);
        } while (name.size() > 20);
    cout << "Price: ";
    cin >> price;
    if (!cin.good() || price < 0)
        do {
            cout << "Incorrect data or sub-zero number. Try again: ";
            cin.clear();
            cin.ignore(USHRT_MAX,'\n');
            cin >> price;
        } while (!cin.good() || price <0);
    cout << "Amount: ";
    double test;
    cin >> test;
    if (!cin.good() || test < 0 || test != ceil(test))
        do {
            cout << "Incorrect data, sub-zero number or floating-point number. Try again: ";
            cin.clear();
            cin.ignore(USHRT_MAX,'\n');
            cin >> test;
        } while (!cin.good() || test < 0 || test != ceil(test));
    amount = int(test);
    Toys.push_back(Toy(name, price, amount));
    pressKey();
}

void Shop::deleteToy(string name) {
    for (unsigned short i = 0; i < Toys.size(); i++)
        if (Toys[i].name == name)
            Toys.erase(Toys.begin() + i);
}

ostream& operator<<(ostream& out, const Shop& shop) {
    out << left << setw(20) << shop.trademark;
    out << left << setw(40) << shop.address;
    return out;
}

Chain::Chain(string trademark) : trademark(trademark), amount(0) {}

void Chain::addShop() {
    cout << "[ 2] --> Add a shop"<< endl << endl;
    string trademark;
    string address;
    cout << "Chain's name: ";
    cin.ignore(USHRT_MAX,'\n');
    getline(cin, trademark);
    if (trademark.size() > 20)
        do {
            cout << "Shop's name can have only 20 letters. Try again: ";
            getline(cin, trademark);
        } while (trademark.size() > 20);
    cout << "Address: ";
    getline(cin, address);//
    if (address.size() > 40)
        do {
            cout << "Address of shops can have only 50 letters. Try again: ";
            getline(cin, address);
        } while (address.size() > 40);
    cout << endl << "Press any key to return."<< endl;
    Shops.push_back(Shop(trademark,address));
        amount++;
    getchar();
    system("cls");
}

ostream& operator<<(ostream& out, const Chain& chain) {
    out << left << setw(20) << chain.trademark;
    return out;
}

void pressKey() {
    cout << endl << "Press any key to return."<< endl;
    getchar();
    getchar();
    system("cls");
}

void clearScreen() {
    printf("\033[2J"); // Screen
    printf("\033[0;0f"); // Cursor
}
||=== Build: Debug in 01 Tester (compiler: GNU GCC Compiler) ===|
E:\01 [C++]\entrepreneur\entrepreneur.cpp||In constructor 'Chain::Chain(std::string)':|
E:\01 [C++]\entrepreneur\entrepreneur.cpp|70|error: no matching function for call to 'Container<Shop>::Container()'|
E:\01 [C++]\entrepreneur\entrepreneur.cpp|70|note: candidates are:|
E:\01 [C++]\entrepreneur\templateContainer.hpp|29|note: Container<T>::Container(const Container<T>&) [with T = Shop]|
E:\01 [C++]\entrepreneur\templateContainer.hpp|29|note:   candidate expects 1 argument, 0 provided|
E:\01 [C++]\entrepreneur\templateContainer.hpp|23|note: Container<T>::Container(int) [with T = Shop]|
E:\01 [C++]\entrepreneur\templateContainer.hpp|23|note:   candidate expects 1 argument, 0 provided|
E:\01 [C++]\entrepreneur\templateContainer.hpp||In instantiation of 'void Container<T>::insert(const T&, int) [with T = Shop]':|
E:\01 [C++]\entrepreneur\templateContainer.hpp|75|required from 'void Container<T>::push_back(const T&) [with T = Shop]'|
E:\01 [C++]\entrepreneur\entrepreneur.cpp|92|required from here|
E:\01 [C++]\entrepreneur\templateContainer.hpp|64|error: no matching function for call to 'Shop::Shop()'|
E:\01 [C++]\entrepreneur\templateContainer.hpp|64|note: candidates are:|
E:\01 [C++]\entrepreneur\entrepreneur.cpp|19|note: Shop::Shop(std::string, std::string)|
E:\01 [C++]\entrepreneur\entrepreneur.cpp|19|note:   candidate expects 2 arguments, 0 provided|
E:\01 [C++]\entrepreneur\entrepreneur.hpp|24|note: Shop::Shop(const Shop&)|
E:\01 [C++]\entrepreneur\entrepreneur.hpp|24|note:   candidate expects 1 argument, 0 provided|
E:\01 [C++]\entrepreneur\templateContainer.hpp||In instantiation of 'void Container<T>::resize() [with T = Shop]':|
E:\01 [C++]\entrepreneur\templateContainer.hpp|63|required from 'void Container<T>::insert(const T&, int) [with T = Shop]'|
E:\01 [C++]\entrepreneur\templateContainer.hpp|75|required from 'void Container<T>::push_back(const T&) [with T = Shop]'|
E:\01 [C++]\entrepreneur\entrepreneur.cpp|92|required from here|
E:\01 [C++]\entrepreneur\templateContainer.hpp|84|error: no matching function for call to 'Shop::Shop()'|
E:\01 [C++]\entrepreneur\templateContainer.hpp|84|note: candidates are:|
E:\01 [C++]\entrepreneur\entrepreneur.cpp|19|note: Shop::Shop(std::string, std::string)|
E:\01 [C++]\entrepreneur\entrepreneur.cpp|19|note:   candidate expects 2 arguments, 0 provided|
E:\01 [C++]\entrepreneur\entrepreneur.hpp|24|note: Shop::Shop(const Shop&)|
E:\01 [C++]\entrepreneur\entrepreneur.hpp|24|note:   candidate expects 1 argument, 0 provided|
||=== Build failed: 3 error(s), 7 warning(s) (0 minute(s), 0 second(s)) ===|

Link: https://drive.google.com/drive/folders/0B7P1TugcU99rSFpjb1lZZzBDRDA?usp=sharing

 


Viewing all articles
Browse latest Browse all 21942