У меня есть небольшая программа, которая хранит некоторые объекты, и есть функция, которая должна найти требуемый объект, если есть соответствующий объект абстракции, возвращает первый найденный или объект по умолчанию. Мне нужно, чтобы отобразить их, но когда я использую функцию «шоу» Я получаю эту ошибку:Отображение объектов, которые находятся в C++
no matching function for call to 'show'
Вот код основного исходного файла:
#include <iostream>
#include "iPhone.h"
#include "Inventory.h"
using namespace std;
void fill(Inventory& i)
{
i.add_item("iPhone5s", 4.0, 16, "silver");
i.add_item("iPhone6", 4.7, 64, "gold");
i.add_item("iPhone6s Plus", 5.5, 128, "silver");
}
void show(iPhone& item)
{
cout << item.get_modelName() << " "
<< item.get_screenSize()<< " "
<< item.get_capacity() << " "
<< item.get_colour() << " "
<< endl;
}
int main()
{
Inventory inventory;
inventory.init();
fill(inventory);
iPhone query1;
query1.init("", 0, 128, "silver");
show(inventory.find_item(query1));
iPhone query2;
query2.init("iPhone5s", 4.0, 16, "silver");
show(inventory.find_item(query2));
#ifndef NDEBUG
cin.get();
#endif
return 0;
}
Inventory.h:
#ifndef Inventory_h
#define Inventory_h
#pragma once
#include "iPhone.h"
class Inventory
{
public:
// Initializes inventory for storing abstraction objects
void init() { _count = 0; }
// Returns the number of currently stored abstraction objects
size_t get_count() { return _count; }
// Returns stored abstraction object by its index or default if index is invalid
iPhone get_item(size_t i) { return (i <_count) ? _items[i] : iPhone(); }
// From passed property values, creates and adds new abstraction object in an array _items
void add_item(std::string modelName, float screenSize, int capacity, std::string colour);
// Looks for a matching abstraction object and returns the first found or default object
iPhone find_item(iPhone& query);
private:
// The maximum number of abstraction objects that can be stored
static const size_t MAX_SIZE{ 10 };
// An actual array for storing abstraction objects
iPhone _items[Inventory::MAX_SIZE];
// The number of currently stored abstraction objects in the array _items
size_t _count;
};
#endif /* Inventory_h */
Inventory.cpp:
#include "Inventory.h"
void Inventory::add_item(std::string modelName, float screenSize, int capacity, std::string colour)
{
if (_count < Inventory::MAX_SIZE)
{
iPhone new_item;
new_item.init(modelName, screenSize, capacity, colour);
_items[_count] = new_item;
_count++;
}
}
iPhone Inventory::find_item(iPhone &query)
{
for (size_t i = 0U; i < _count; i++)
{
iPhone& item = _items[i];
//for string type property
if (query.get_modelName()!= ""
&& query.get_modelName() != item.get_modelName())
continue;
// for number type property
if (query.get_screenSize() != 0
&& query.get_screenSize() != item.get_screenSize())
continue;
// for number type property
if (query.get_capacity() != 0
&& query.get_capacity() != item.get_capacity())
continue;
//for string type property
if (query.get_colour()!= ""
&& query.get_colour() != item.get_colour())
continue;
return item;
}
return iPhone{}; // return the default value object (or null object)
}
Это мой первый опыт работы на C++. Вот почему у меня могут быть некоторые ошибки. Почему я получаю эту ошибку?
Сделать return type 'inventory.find_item (query1)' правильный. – MikeCAT
Измените свой вопрос, чтобы включить определение класса «Inventory». Кроме того, я уверен, что компилятор действительно говорит вам что-то еще. При размещении вопросов о ошибках сборки, пожалуйста, укажите сообщение об ошибке * complete *, включая любые информационные заметки. –
Наконец, примечание о вашей терминологии: «Основной класс», который вы показываете, не является * классом *, это исходный файл, содержащий функцию 'main' * *. –