可能的循环引用
possible circular referencing?
也许是因为我已经连续盯着这个5个小时了,我只是在阅读明显的解决方案,但Xcode声明我的对象向量(在头文件中声明)在一个。cpp文件中未声明;然而,我的另一个。cpp文件可以访问向量,所以我不确定问题是什么。也许我的头文件不合适,或者我无意中"循环引用"??建议好吗?
publications.h
#ifndef PUBLICATIONS_H
#define PUBLICATIONS_H
#include "std_lib_facilities.h"
class Publication {
private:
string
publication_title,
publication_author,
publication_year,
publication_genre,
publication_media,
publication_target_age,
publication_ISBN;
bool currently_checked_out;
public:
Publication(string title, string author, string year, string genre, string media, string target_age, string ISBN, bool checked_out) {
publication_title = title;
publication_author = author;
publication_year = year;
publication_genre = genre;
publication_media = media;
publication_target_age = target_age;
publication_ISBN = ISBN;
currently_checked_out = checked_out;
}
Publication(){};
};
#endif /* PUBLICATIONS_H */
library.h
#ifndef LIBRARY_H
#define LIBRARY_H
#include "std_lib_facilities.h"
class Publication;
class Library {
private:
vector<Publication> lb;
public:
Library(){};
void documentation();
void list_all_publications();
void new_publication(string title, string author, string year, string genre, string media, string target_age, string ISBN, bool currently_checked_out);
};
#endif /* LIBRARY_H */
patron.h
#ifndef PATRON_H
#define PATRON_H
#include "std_lib_facilities.h"
class Publication;
class Patron {
private:
string
customer_name,
customer_telephone;
public:
void check_out_publication(string publication_requested);
void return_publication(string check_in_publication_name);
void check_out();
bool is_checked_out();
Patron(){};
};
#endif /* PATRON_H */
library.cpp(工作完美,可以访问vector in library.h)
#include "library.h"
#include "patron.h"
#include "publications.h"
#include "std_lib_facilities.h"
Patron p;
void Library::documentation() {
cout << "n-----Create a new publication----n";
cout << "You will enter all the necessary info for a new publication (title, author, year, genre, media, target age, and ISBN).n";
cout << "n----List all Publications-----n";
cout << "List all publications that have been entered (in this current session).n";
cout << "n---Check out Publication----n";
cout << "You will enter your name and telephone number and the publication will be checked out.n";
cout << "n-----Return Publication------n";
cout << "A previously checked out publication will be marked as returned.n";
}
void Library::new_publication(string title, string author, string year, string genre, string media, string target_age, string ISBN, bool checked_out) {
lb.push_back(Publication(title, author, year, genre, media, target_age, ISBN, checked_out));
}
void Library::list_all_publications() {
for (int i = 0; i < lb.size(); i++) {
cout << "Title: " << "tChecked Out: " << p.is_checked_out() << endl;
}
}
patron.cpp(有问题的文件,不能访问library.h)
#include "publications.h"
#include "library.h"
#include "patron.h"
#include "std_lib_facilities.h"
void Patron::check_out_publication(string publication_requested) {
// check to make sure publication isn't already checked out.
for (int i = 0; i < lb.size(); i++) {
if ((publication_requested == lb[i].publication_title) && lb[i].currently_checked_out) {
cout << "Sorry, this publication is currently checked out." << endl;
} else if ((publication_requested == lb[i].publication_title) && !(lb[i].currently_checked_out)) {
cout << "Enter your name: ";
getline(cin, customer_name);
cout << "Enter your telephone number (no dashes/no spaces): ";
cin >> customer_telephone;
} else {
continue;
}
}
}
void Patron::return_publication(string check_in) {
for (int i = 0; i < lb.size(); i++) {
if ((check_in == lb[i].publication_title) && lb[i].currently_checked_out) {
// marked as no longer checked out.
lb[i].currently_checked_out = false;
}
}
}
bool Patron::is_checked_out() {
return currently_checked_out;
}
void Patron::check_out() {
currently_checked_out = true;
}
patron.cpp错误
USE OF UNDECLARED IDENTIFIER "lb"
lb
是Library
类的私有成员。您可以在library.cpp文件中访问它,因为您在Library
成员函数中使用它。而在Patron
类中,你直接访问它。但是编译器认为它只是另一个变量,您没有声明它。您可能需要为Library
添加一个访问器。
相关文章:
- 打破循环引用在C++中更优雅
- 基于范围的 for 循环unordered_map和引用
- 如何编译使用循环引用的代码?
- 关于C++中引用计数和循环引用的概念
- CBasePin 递增对拥有过滤器的引用.循环引用?
- 使用带有循环引用的weak_ptr
- 在 c++ 中基于范围的 for 循环中使用引用作为控制变量
- 使用基于范围的for循环取消对矢量指针的引用
- 为什么在C/C++中的while循环中只发生一次取消引用
- 为什么基于范围的 for 循环中的结构化绑定只是一个副本而不是引用?
- 纯引用而不是weak_ptr来打破循环依赖关系
- 如何避免成员回调和shared_ptr循环引用?
- for 循环说 - 错误 成员引用基类型"int [13]"不是结构或联合
- 在循环迭代期间将引用保存到矢量中的空指针
- 如何引用或调用在 c++ 中的其他 while 循环中创建的向量?
- std :: make_shared(),std :: feek_ptr和循环引用
- 有没有办法在不使用循环的情况下非具体地引用映射中的每个值?C++
- 由于具有循环引用的类中的unique_ptr或向量而导致核心转储
- C++ 是否可以使用对变量的引用来简化这种嵌套循环模式?
- 复制具有相互引用/循环依赖关系的类的构造函数