C++:成员不可访问-使用友元函数允许一个类修改另一个类的成员数据
C++: Member is inaccessible - using friend function to allow one class to modify another class member data
好吧,我已经到处找答案了。
我正在尝试建立一个系统,在这个系统中,对象的私有成员只能通过一个单独类的两个成员函数进行修改。
这是要修改的对象的标题内容,表.h:
#ifndef TABLE_H
#define TABLE_H
#include "party.h"
/*
Seats a party for their dining experience at the restaurant.
*/
class table
{
friend void server::seatGuests(const table &, const party &);
friend void server::cleanTable(const table &);
private:
const unsigned int cap_; //guest capacity (shouldn't change)
const unsigned int id_; //table id (shouldn't change)
mutable bool isTaken_; //flag (changeable by friend functions)
party *const group_; //party seated at the table (starts off as null)
public:
table::table(const unsigned int id, const unsigned int cap = 4); //ctor
inline const unsigned int table::getCap() const { return cap_; }
inline const unsigned int table::getId() const { return id_; }
inline const bool table::isTaken() const { return isTaken_; }
inline const party *const table::getParty() const { return group_; };
};
void server::seatGuests(const table &t, const party &p)
{
t.isTaken_ = false;
}
#endif // TABLE_H
这是服务器。h:
class server : public employee
{
private:
public:
explicit server::server();
void server::seatGuests(const table &, const party &);
void server::cleanTable(const table &);
};
...
我收到以下错误:member "table::isTaken_" (declared at line 17) is inaccessible
server.h文件间接包含在table.h中,因为party.h包含restaurant.h,restaurant/h包含server.h。
我试着把这个放在桌子上。h之前:
class server;
namespace server {
void server::seatGuests(const table &, const party &);
void server::cleanTable(const table &);
}
但这让我更加头疼,因为编译器会吐出类似"类服务器没有成员‘seatGuests’或‘cleanTable’"的内容,我认为这是由于一些包含冲突。这就是为什么我在原始的server.h文件中声明了函数,而不是在table.h文件中。
话虽如此,我读过的这类源声明,friend函数的实现应该在数据被修改的类中,这就是我所做的。
除此之外,编译器仍然认为成员数据是不可访问的。有人看到我在这里做错了什么吗?
如果你想让我发布更多代码,请告诉我。
提前谢谢。
编辑
对延迟发布编译器输出表示歉意。请参阅以下内容:
> Executing task: clang++ -Wall -std=c++17 -stdlib=libc++ -o main.out /Users/ajm/Projects/restaurant/cpp/main/main.cpp -I/Users/ajm/Projects/restaurant/cpp/main/ -I/Users/ajm/Projects/restaurant/cpp/restaurant/ -I/Users/ajm/Projects/restaurant/cpp/people/ -I/Users/ajm/Projects/restaurant/cpp/people/workers/ -I/Users/ajm/Projects/restaurant/cpp/data/ --debug <
In file included from /Users/ajm/Projects/restaurant/cpp/main/main.cpp:1:
In file included from /Users/ajm/Projects/restaurant/cpp/main/simulation.h:4:
In file included from /Users/ajm/Projects/restaurant/cpp/restaurant/restaurant.h:4:
In file included from /Users/ajm/Projects/restaurant/cpp/restaurant/tablespace.h:4:
In file included from /Users/ajm/Projects/restaurant/cpp/restaurant/table.h:4:
In file included from /Users/ajm/Projects/restaurant/cpp/people/workers/server.h:4:
In file included from /Users/ajm/Projects/restaurant/cpp/people/workers/employee.h:5:
In file included from /Users/ajm/Projects/restaurant/cpp/main/../data/job.h:4:
/Users/ajm/Projects/restaurant/cpp/people/party.h:25:16: error: unknown type name 'restaurant'
void start(restaurant &spot);
^
In file included from /Users/ajm/Projects/restaurant/cpp/main/main.cpp:1:
In file included from /Users/ajm/Projects/restaurant/cpp/main/simulation.h:4:
In file included from /Users/ajm/Projects/restaurant/cpp/restaurant/restaurant.h:4:
In file included from /Users/ajm/Projects/restaurant/cpp/restaurant/tablespace.h:4:
/Users/ajm/Projects/restaurant/cpp/restaurant/table.h:8:11: error: redefinition of 'server' as different kind of
symbol
namespace server
^
/Users/ajm/Projects/restaurant/cpp/restaurant/table.h:7:7: note: previous definition is here
class server;
^
/Users/ajm/Projects/restaurant/cpp/restaurant/table.h:10:23: error: unknown type name 'table'
void seatGuests(const table &t, const party *const p);
^
/Users/ajm/Projects/restaurant/cpp/restaurant/table.h:11:23: error: unknown type name 'table'
void cleanTable(const table &t);
^
/Users/ajm/Projects/restaurant/cpp/restaurant/table.h:13:31: error: unknown type name 'table'
void server::seatGuests(const table &t, const party *const p)
^
/Users/ajm/Projects/restaurant/cpp/restaurant/table.h:18:31: error: unknown type name 'table'
void server::cleanTable(const table &t)
^
/Users/ajm/Projects/restaurant/cpp/restaurant/table.h:42:25: error: out-of-line declaration of 'seatGuests' does not
match any declaration in 'server'
friend void server::seatGuests(const table &t, const party *const p);
^~~~~~~~~~
/Users/ajm/Projects/restaurant/cpp/restaurant/table.h:43:25: error: out-of-line declaration of 'cleanTable' does not
match any declaration in 'server'
friend void server::cleanTable(const table &t);
^~~~~~~~~~
8 errors generated.
The terminal process terminated with exit code: 1
server::seatGuests
和server::cleanTable
的声明必须出现在table
中的friend
声明之前。#include "server.h"
出现在table
的声明之前,所以应该足够了吧?server
包括table
,table
包括server
,所以我们有循环依赖关系。为了绕过这一点,table
应该包括server
,server
应该转发声明table
。
// table.h
#include "party.h"
class table
{
friend void server::seatGuests(const table &, const party &);
friend void server::cleanTable(const table &);
};
// server.h
class table;
class party;
class server : public employee
{
public:
explicit server();
void seatGuests(const table &, const party &);
void cleanTable(const table &);
};
循环依赖关系和友元声明对我来说似乎是一个糟糕的设计。你可能需要重新思考这个设计,而不是使用前向声明来解决这个问题。找到一种方法来避免循环,避免朋友声明我的答案是变通办法,而不是解决方案
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 我们可以访问一个不存在的联盟的成员吗
- 为什么我不能在一个类的不同行中声明和定义成员变量?
- 我不小心调用了一个没有自己类对象的成员函数.但这是怎么回事呢
- 如何从另一个文件继承私有成员变量和公共函数
- C++-我可以创建另一个类的成员并在构造函数中使用它吗
- 是否可以同时声明一个类成员的常量/非常量?
- int数据类型的指针指向的是什么,如果是一个类的私有数据成员,我们创建了该类的两个对象?
- 在作为静态成员包含在另一个类中的类的构造函数中使用 cout
- 如何将 c++ 类包装到 python 中,以便我可以使用 pybind11 访问其成员的公共方法(成员是一个对象指针)
- 在构造函数中从另一个类成员构造一个类成员
- 有没有办法为静态对象成员定义一个符合开关标准的常量?
- 非静态数据成员和一个定义规则
- 将一个私有成员从一个类转到另一个类
- 将所有成员从一个结构复制到另一个结构
- javascript中的闭包与c++中的类实例(它有一个私有成员和一个公共方法)相当吗
- 为数据成员分配一个新的随机方向,该方向不同于 c++ 中的当前方向
- 为类成员指定一个点
- 为一个c++ vector成员添加一个标志