Lectures
EDIT: Открих си грешката, забравих да създам default constuctor и инициализирам в Resource.h. Не видях warning-ите във Visual Studio. Така поне се получи!
Здравейте!
Моля за помощ за задача 5 от домашното. Judge ми дава Runtime Error и не мога да открия къде достига до проблем. В Visual Studio кодът ми работи и вади каквото очаквам; не достигам до грешка при изпълнение, когато тествам. Ето и кодът:
Lecture.h
#define LECTURE_H
#include "Resource.h"
#include <vector>
#include <algorithm>
const int MAX_AMOUNT_OF_RESOURCE_TYPES = 3;
namespace SoftUni {
class Lecture {
std::vector <Resource> resources;
public:
friend Lecture& operator<<(Lecture& lecture, const Resource& obj);
std::vector<Resource>::iterator begin() {
return resources.begin();
}
std::vector<Resource>::iterator end() {
return resources.end();
}
int& operator[](const int type) const {
int amount = 0;
for (size_t i = 0; i < resources.size(); i++) {
std::cout << i << " ";
if (resources[i].getType() == type) amount++;
}
return amount;
}
size_t get_size() { return resources.size(); }
bool object_exists(const Resource& obj) {
for (size_t i = 0; i < resources.size(); i++) {
if (resources[i].get_id() > obj.get_id()) return 0;
if (resources[i] == obj) {
resources[i].set_link(obj.get_link());
return 1;
}
}
return 0;
}
ResourceType get_resource_type(const int i) {
return resources[i].getType();
}
};
Lecture& operator<<(Lecture& lecture, const Resource& obj) {
if (lecture.object_exists(obj)) return lecture;
lecture.resources.push_back(obj);
std::sort(lecture.resources.begin(), lecture.resources.end());
return lecture;
}
bool type__has_not_been_saved(const std::vector<ResourceType>& resourceTypes, const ResourceType& resourceType) {
for (size_t i = 0; i < resourceTypes.size(); i++) {
if (resourceTypes[i] == resourceType) return 0;
}
return 1;
}
std::vector<ResourceType>& operator<<(std::vector<ResourceType>& resourceTypes, Lecture& lectures) {
for (size_t i = 0; i < lectures.get_size(); i++) {
if (type__has_not_been_saved(resourceTypes, lectures.get_resource_type(i))) {
resourceTypes.push_back(lectures.get_resource_type(i));
}
if (resourceTypes.size() == MAX_AMOUNT_OF_RESOURCE_TYPES) break;
}
std::sort(resourceTypes.begin(), resourceTypes.end());
return resourceTypes;
}
}
#endif
Resource.h
#ifndef RESOURCE_H
#define RESOURCE_H
#include "ResourceType.h"
#include <iostream>
namespace SoftUni {
class Resource {
int id;
ResourceType resource_type;
std::string link;
friend Lecture;
public:
ResourceType getType() const { return resource_type; }
friend std::ostream& operator<<(std::ostream& os, const Resource& resource);
friend std::istream& operator>>(std::istream& is, Resource& resource);
Resource& operator=(const Resource& resource) {
id = resource.id;
resource_type = resource.resource_type;
link = resource.link;
return *this;
}
bool operator< (const Resource& resource) const {
return id < resource.id;
}
bool operator== (const Resource& resource) const {
return id == resource.id && resource_type == resource.resource_type;
}
void set_link(std::string link_) {
link = link_;
return;
}
std::string get_link() const { return link; }
int get_id() const { return id; }
};
std::ostream& operator<<(std::ostream& os, const Resource& resource) {
os << resource.id << " " << resource.resource_type << " " << resource.link;
return os;
}
std::istream& operator>>(std::istream& is, Resource& resource) {
is >> resource.id;
std::string string_;
is >> string_;
if (string_ == "Demo") resource.resource_type = ResourceType::DEMO;
else if (string_ == "Presentation") resource.resource_type = ResourceType::PRESENTATION;
else if (string_ == "Video") resource.resource_type = ResourceType::VIDEO;
is >> resource.link;
return is;
}
}
#endif // !RESOURCE_H
Благодаря Ви предварително!