在C ++中的循环引用[复制](Circular Reference in C++ [duplicate])
这个问题在这里已经有了答案:
- 解决由于类之间的循环依赖关系造成的错误 8个答案
我正在做一个Snake游戏,并且我在C ++中遇到了Circular Reference的问题。 这是SnakeBody类的头部,代表Snake的任何部分,而不是头部。
#ifndef SNAKEBODY_HPP_ #define SNAKEBODY_HPP_ class SnakeBody { public: SnakeBody(SnakeHead *origin, int left, int x, int y); ~SnakeBody() = default; void move(); void grow(int size); protected: SnakeBody *next; SnakeHead *head; SnakeGame *game; int x; int y; int growWait; }; #endif /* !SNAKEBODY_HPP_ */
还有源代码(包含构造函数的部分)。
#include "../include/SnakeHead.hpp" #include "../include/SnakeGame.hpp" #include "../include/SnakeBody.hpp" SnakeBody::SnakeBody(SnakeHead *origin, int left, int x, int y) { this->head = origin; this->game = this->head->getGame(); this->x = x; this->y = y; this->growWait = 0; if (left > 0) this->next = new SnakeBody(origin, left - 1, x + 1, y); else this->next = nullptr; }
我一直在关心如何使每个包含在源代码中而不是标题中,以避免未完成的引用和循环。 但它仍然显示SnakeHead有一个未定义的参考。
SnakeBody:蛇的任何部分不是头部。 SnakeHead:蛇的头。 SnakeGame:包含棋盘的状态,需要SnakeHead和SnakeHead的参考,而SnakeBody需要SnakeGame的参考来获取有关游戏状态的数据(例如墙壁,食物等)。
错误消息是:./src/../include/SnakeBody.hpp:19:9:错误:'SnakeHead'没有命名一个类型; 你的意思是'SnakeBody'吗? SnakeHead *头;
This question already has an answer here:
i'm working on a Snake game and i'm having trouble with Circular Reference in C++. This is the header of the class SnakeBody who represent anypart of the Snake that is not the head.
#ifndef SNAKEBODY_HPP_ #define SNAKEBODY_HPP_ class SnakeBody { public: SnakeBody(SnakeHead *origin, int left, int x, int y); ~SnakeBody() = default; void move(); void grow(int size); protected: SnakeBody *next; SnakeHead *head; SnakeGame *game; int x; int y; int growWait; }; #endif /* !SNAKEBODY_HPP_ */
And there is the source code (the part containing the constructor).
#include "../include/SnakeHead.hpp" #include "../include/SnakeGame.hpp" #include "../include/SnakeBody.hpp" SnakeBody::SnakeBody(SnakeHead *origin, int left, int x, int y) { this->head = origin; this->game = this->head->getGame(); this->x = x; this->y = y; this->growWait = 0; if (left > 0) this->next = new SnakeBody(origin, left - 1, x + 1, y); else this->next = nullptr; }
I have been carefull about make every include in source and not header to avoid unfinished reference and loop. But it still appear that SnakeHead has a undefined reference.
SnakeBody : Any part of the snake that is not the head. SnakeHead : The head of the snake. SnakeGame : Contain the state of the board, need a reference of SnakeHead and SnakeHead and SnakeBody need a reference of SnakeGame to get data about the state of the game (ie : wall, food, etc...).
The error message is : ./src/../include/SnakeBody.hpp:19:9: error: ‘SnakeHead’ does not name a type; did you mean ‘SnakeBody’? SnakeHead *head;
原文:https://stackoverflow.com/questions/49684818
最满意答案
其实你从
FirstViewController
设置委托属性,然后当你提交LogInViewController
你已经编写了代码var delegate:SendDel!=nil
,这使得每次点击按钮时委托nil
。所以请尝试以下代码:
var delegate: SendDel?
希望对你有效。
Actually you setting the delegate property from
FirstViewController
and then when you present theLogInViewController
you have write the codevar delegate:SendDel!=nil
which makes delegatenil
every time you tapped the button.So try below code :
var delegate: SendDel?
Hope it works for you.
相关问答
更多-
其实你从FirstViewController设置委托属性,然后当你提交LogInViewController你已经编写了代码var delegate:SendDel!=nil ,这使得每次点击按钮时委托nil 。 所以请尝试以下代码: var delegate: SendDel? 希望对你有效。 Actually you setting the delegate property from FirstViewController and then when you present the LogInV ...
-
使用instantiateViewControllerWithIdentifier ,不会调用prepareForSegue 。 您只需在实例化后分配scoretext。 let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) let vc : ViewTwo = storyboard.instantiateViewControllerWithIdentifier("viewtwo") as! ViewTwo vc.sco ...
-
使用Swift通过视图控制器传递数据而不使用Storyboard(Passing Data through View Controllers with Swift without Using Storyboard)[2020-01-10]
在你想要的类型上UberWebviewController一个UberWebviewController ,例如[Any]? : var dataFromAPI : [Any]? 然后在创建控制器之后但在呈现之前设置它: let viewController = UberWebviewController() viewController.dataFromAPI = whateverYourDataIs self.navigationController?.presentViewController(vi ... -
Swift 3 - 在视图控制器之间传递变量和函数(Swift 3 - Passing Variables and Functions Between View Controllers)[2023-07-11]
当我在不同的类中调用它时,我正在创建一个新的类实例。 这就是为什么我所有未经过初始化的值都等于零。 这是我固定它的方式... let connectionManager = ConnectionManager() 这是创建一个实例,而不是一个参考。 至 var connectionManager: ConnectionManager? 这是创建一个对已经存在的类的引用。 我不得不做的另一件事是,当我继续到另一个视图控制器,传递所有值。 override func prepare(for segue: U ... -
您不需要实例化ChatLogController来传递数据,因为如果您正确设置了NavigationController ,那么他将为您实例化。 func setupChatLogControllerForUser(user: User) { let chatLogController = ChatLogController() chatLogController.selectedUserID = user.id! chatLogController.user = user } 因 ...
-
正如评论中所述,问题在于你在UIViewController上使用push segue。 请注意,push和modal segues已被弃用...请看看这篇文章条目,并告诉我,如果您有任何疑问 所有选择区段之间有什么区别? As stated on the comment the problem was that you were using a push segue on a UIViewController. Please note that push and modal segues are depr ...
-
您可以使用storyboard实例实例化目标视图控制器并进一步传递数据: let storyboard = UIStoryboard(name: "Main", bundle: nil) let controller = storyboard.instantiateViewController(withIdentifier: "TestViewController") as! TestViewController controller.variableX = self.variableX self.pres ...
-
在控制器之间发送数据(Send data between controllers)[2023-01-15]
问题出在嵌套表格上。 因此,当我提交表单以进行新评论时,它还提交了更新活动的父表单。 The problem was with nested forms. So, when I submitted form for new comment, it also submitted the parent form for updating activity. -
IOS Swift,委托两个视图控制器之间进行通信(IOS Swift, delegate to communicate between two view controllers)[2023-03-13]
好的,所以从评论我们得到你没有设置delegate属性,从问题我们知道你正在使用segues。 要设置属性,您需要覆盖ViewController一个方法(第一个): override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if (segue.identifier == "ConnectDeviceSegue") { let mapViewController = segue.destinationVie ... -
如何在视图控制器StoryBoard Xcode之间发送数据(How to Send data between view controllers StoryBoard Xcode)[2021-11-11]
使用此方法在所有视图控制器中传递属性 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([[segue identifier] isEqualToString:@"ShowNewVC"]) { NextViewController *nextVC = (NextViewController *)[segue destinationViewController]; nex ...