• Najnowsze pytania
  • Bez odpowiedzi
  • Zadaj pytanie
  • Kategorie
  • Tagi
  • Zdobyte punkty
  • Ekipa ninja
  • IRC
  • FAQ
  • Regulamin
  • Książki warte uwagi

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

VPS Starter Arubacloud
0 głosów
274 wizyt
pytanie zadane 12 września 2016 w C i C++ przez plkpiotr Stary wyjadacz (12,420 p.)
edycja 13 września 2016 przez plkpiotr

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

 

komentarz 13 września 2016 przez plkpiotr Stary wyjadacz (12,420 p.)
edycja 9 października 2017 przez plkpiotr

Edycja, ważne!
Program zaczyna chyba działać poprawnie gdy dodam konstruktory klas zgodne ilością argumentów (o pustej definicji) z konstruktorem kontenera... Wniosek: Jestem na tropie : )

1 odpowiedź

+1 głos
odpowiedź 13 września 2016 przez criss Mędrzec (172,590 p.)
wybrane 14 września 2016 przez plkpiotr
 
Najlepsza

error: no matching function for call to 'Container<Shop>::Container()'

Zdefiniowałeś Container(int), więc bezargumentowy konstruktor nie został utworzony.

 no matching function for call to 'Shop::Shop()'

Zdefiniowałeś Shop(string, string), więc bezargumentowy konstruktor nie został utworzony.

komentarz 13 września 2016 przez plkpiotr Stary wyjadacz (12,420 p.)
Dzięki :)
Póki co łączę wątki...
Cały problem sprowadza się do tego czy konstruktory klas są zgodne co do konstruktora kontenera (mają argumenty lub nie), dobrze to rozumiem?
komentarz 13 września 2016 przez criss Mędrzec (172,590 p.)

czy konstruktory klas są zgodne co do konstruktora kontenera (mają argumenty lub nie)

Nie wiem o co ci chodzi 

komentarz 13 września 2016 przez plkpiotr Stary wyjadacz (12,420 p.)

Jeżeli kontener Container tworzę poprzez konstruktor nie przyjmujący argumentów, to także konstruktory Toy, Shop i Chain muszą być bez argumentów?

1
komentarz 13 września 2016 przez criss Mędrzec (172,590 p.)

Ekhm no to zależy od ciebie, bo ty piszesz kod... Ale zakładając, że tworzysz Container<Toy>, to  Toy musi posiadać publiczny bezargumentowy konstruktor, bo będziesz wywoływał new Toy[capacity] a new[] wymaga bezargumentowego konstruktora.

komentarz 14 września 2016 przez plkpiotr Stary wyjadacz (12,420 p.)
Dziękuję, chyba rozwiało to moje wątpliwości ;)

Podobne pytania

0 głosów
1 odpowiedź 1,198 wizyt
pytanie zadane 16 czerwca 2017 w C i C++ przez excavelty Bywalec (2,480 p.)
0 głosów
1 odpowiedź 1,696 wizyt
pytanie zadane 13 listopada 2016 w C i C++ przez Bączal Nowicjusz (120 p.)
0 głosów
1 odpowiedź 491 wizyt
pytanie zadane 8 września 2016 w C i C++ przez plkpiotr Stary wyjadacz (12,420 p.)

92,452 zapytań

141,262 odpowiedzi

319,079 komentarzy

61,854 pasjonatów

Motyw:

Akcja Pajacyk

Pajacyk od wielu lat dożywia dzieci. Pomóż klikając w zielony brzuszek na stronie. Dziękujemy! ♡

Oto polecana książka warta uwagi.
Pełną listę książek znajdziesz tutaj.

Akademia Sekuraka

Akademia Sekuraka 2024 zapewnia dostęp do minimum 15 szkoleń online z bezpieczeństwa IT oraz dostęp także do materiałów z edycji Sekurak Academy z roku 2023!

Przy zakupie możecie skorzystać z kodu: pasja-akademia - użyjcie go w koszyku, a uzyskacie rabat -30% na bilety w wersji "Standard"! Więcej informacji na temat akademii 2024 znajdziecie tutaj. Dziękujemy ekipie Sekuraka za taką fajną zniżkę dla wszystkich Pasjonatów!

Akademia Sekuraka

Niedawno wystartował dodruk tej świetnej, rozchwytywanej książki (około 940 stron). Mamy dla Was kod: pasja (wpiszcie go w koszyku), dzięki któremu otrzymujemy 10% zniżki - dziękujemy zaprzyjaźnionej ekipie Sekuraka za taki bonus dla Pasjonatów! Książka to pierwszy tom z serii o ITsec, który łagodnie wprowadzi w świat bezpieczeństwa IT każdą osobę - warto, polecamy!

...