C++ 对象在程序结束后重新初始化
C++ Object re-initialized after end of program
快速 C++ 问题:所以我正在做一些 Raspberry Pi/Arduino 的事情,一切都已设置好,所以我正在为控制 Arduino 的 Raspberry Pi 编写代码。
这是代码的图片,然后是输出的图片。如果您在代码中看到:最后一行是 "cout << "End Test" << endl;",但在输出中,该语句之后还有其他内容,这是在 Decepticon 初始化时写入的输出(正如您可以在输出的开头看到)。知道为什么会这样吗?
程序代码如下:
int main() {
Decepticon d;
cout << "Hello, World!"<< endl;
//traverse(d);
d.forward();
//cout << "Start Test" << endl;
// cout << d->get_arduino_message() << endl;
cout << "End Test" << endl;
return 0;
}
目标代码如下:
#include <stdio.h>
#include "decepticon.hpp"
#include <cstring>
#include <iostream>
//using namespace cv;
/** Constructor
*/
Decepticon::Decepticon() {
this->left_speed = 0;
this->right_speed = 0;
this->claw_pos = 0;
//this->camera = raspiCamCvCreateCameraCapture(CAMERA_DEV);
serial_connect(&this->commlink, NULL, BAUDRATE, 0);
if (this->commlink.connected){
std::cout << "Connected!" << std::endl;
send_to_arduino();
}
}
/** Deconstructor
*/
Decepticon::~Decepticon() {
if (this->commlink.connected) {
stop();
serial_disconnect(&this->commlink);
}
// if (this->camera)
//raspiCamCvReleaseCapture(&this->camera);
}
/** Set the left-hand side speed
* @param speed
* a value from -255 (full backward) to 255 (full forward)
*/
void Decepticon::set_left(int speed) {
this->left_speed = limit_signal(speed, -255, 255);
send_to_arduino();
}
/** Set the right-hand side speed
* @param speed
* a value from -255 (full backward) to 255 (full forward)
*/
/** Make robot go full speed forward
*/
void Decepticon::forward() {
this->left_speed = 255;
std::cout << "left speed: " << this->left_speed << std::endl;
this->right_speed = 255;
send_to_arduino();
}
char *Decepticon::get_arduino_message() {
char *message;
message = serial_read(&this->commlink);
if (strlen(message) == 0){
std::cout << "message is NULL" << std::endl;
return NULL;
}
return message;
}
void Decepticon::send_to_arduino() {
int l, r, c;
std::cout << "Start" << std::endl;
l = this->left_speed < 0 ? -this->left_speed + 255 : this->left_speed;
std::cout << "l = " << l << std::endl;
r = this->right_speed < 0 ? -this->right_speed + 255 : this->right_speed;
c = this->claw_pos;
sprintf(this->buf, "%03d%03d%03d\n", c, l, r);
std::cout << this->buf << std::endl;
serial_write(&this->commlink, this->buf);
std::cout << "End" << std::endl;
}
/** Make the robot stop in place
*/
void Decepticon::stop() {
this->left_speed = 0;
this->right_speed = 0;
send_to_arduino();
}
这是输出:
Connected!
Start
l = 0
000000000
000000000
Write Succeeded
End
Hello, World!
left speed: 255
Start
l = 255
000255255
000255255
Write Succeeded
End
End Test
Start
l = 0
000000000
000000000
Write Succeeded
End
您问的是:
The last line is "cout << "End Test" << endl;", but in the output, there's something else after that statement and that's the output that gets written when Decepticon is initalized (as you can see in the beginning of the output). Any idea why that's happening?
Decepticon
的析构函数在从函数返回之前和执行函数中的最后一行代码之后为 d
调用。如果 Decepticon
的析构函数打印任何消息,它们将在函数最后一行输出后打印。
Decepticon
析构函数正在调用 stop()
,后者又调用 send_to_arduino()
。 函数是输出您在 main()
完成后看到的消息的函数。
如果您不希望发生这种情况,您可能最好修改析构函数,使其不调用特定的函数链。
或者,如果您需要完成实际工作但没有生成输出,您可以简单地拥有一个名为 echo
的成员变量,它在构造函数中初始化为 true 并在构造函数的开头设置为 false析构函数。然后修改send_to_arduino()
为:
void Decepticon::send_to_arduino() {
int l, r, c;
if (echo) std::cout << "Start" << std::endl;
l = this->left_speed < 0 ? -this->left_speed + 255 : this->left_speed;
if (echo) std::cout << "l = " << l << std::endl;
r = this->right_speed < 0 ? -this->right_speed + 255 : this->right_speed;
c = this->claw_pos;
sprintf(this->buf, "%03d%03d%03d\n", c, l, r);
if (echo) std::cout << this->buf << std::endl;
serial_write(&this->commlink, this->buf);
if (echo) std::cout << "End" << std::endl;
}
毫无疑问,根据您的需要,还有其他方法可以做到这一点。
我几乎肯定 Decepticon
class 有一个析构函数打印出一些东西。一旦 d
超出范围,就会打印一些东西,但是无法判断是否正是 d
谁在打印一些东西。例如,全局变量可能是一个例外。
编辑:
我现在知道是
stop();
或
serial_disconnect(&this->commlink);
它负责打印一些东西,并且证实它确实是析构函数。
编辑:如果你想停止向 arduino 发送额外的消息,请尝试在析构函数的停止调用中注释掉发送调用。
遵循析构函数的逻辑
它调用 stop
调用 send_to_arduino
等。这是额外的输出。
你可以通过调试器来做到这一点
快速 C++ 问题:所以我正在做一些 Raspberry Pi/Arduino 的事情,一切都已设置好,所以我正在为控制 Arduino 的 Raspberry Pi 编写代码。 这是代码的图片,然后是输出的图片。如果您在代码中看到:最后一行是 "cout << "End Test" << endl;",但在输出中,该语句之后还有其他内容,这是在 Decepticon 初始化时写入的输出(正如您可以在输出的开头看到)。知道为什么会这样吗?
程序代码如下:
int main() {
Decepticon d;
cout << "Hello, World!"<< endl;
//traverse(d);
d.forward();
//cout << "Start Test" << endl;
// cout << d->get_arduino_message() << endl;
cout << "End Test" << endl;
return 0;
}
目标代码如下:
#include <stdio.h>
#include "decepticon.hpp"
#include <cstring>
#include <iostream>
//using namespace cv;
/** Constructor
*/
Decepticon::Decepticon() {
this->left_speed = 0;
this->right_speed = 0;
this->claw_pos = 0;
//this->camera = raspiCamCvCreateCameraCapture(CAMERA_DEV);
serial_connect(&this->commlink, NULL, BAUDRATE, 0);
if (this->commlink.connected){
std::cout << "Connected!" << std::endl;
send_to_arduino();
}
}
/** Deconstructor
*/
Decepticon::~Decepticon() {
if (this->commlink.connected) {
stop();
serial_disconnect(&this->commlink);
}
// if (this->camera)
//raspiCamCvReleaseCapture(&this->camera);
}
/** Set the left-hand side speed
* @param speed
* a value from -255 (full backward) to 255 (full forward)
*/
void Decepticon::set_left(int speed) {
this->left_speed = limit_signal(speed, -255, 255);
send_to_arduino();
}
/** Set the right-hand side speed
* @param speed
* a value from -255 (full backward) to 255 (full forward)
*/
/** Make robot go full speed forward
*/
void Decepticon::forward() {
this->left_speed = 255;
std::cout << "left speed: " << this->left_speed << std::endl;
this->right_speed = 255;
send_to_arduino();
}
char *Decepticon::get_arduino_message() {
char *message;
message = serial_read(&this->commlink);
if (strlen(message) == 0){
std::cout << "message is NULL" << std::endl;
return NULL;
}
return message;
}
void Decepticon::send_to_arduino() {
int l, r, c;
std::cout << "Start" << std::endl;
l = this->left_speed < 0 ? -this->left_speed + 255 : this->left_speed;
std::cout << "l = " << l << std::endl;
r = this->right_speed < 0 ? -this->right_speed + 255 : this->right_speed;
c = this->claw_pos;
sprintf(this->buf, "%03d%03d%03d\n", c, l, r);
std::cout << this->buf << std::endl;
serial_write(&this->commlink, this->buf);
std::cout << "End" << std::endl;
}
/** Make the robot stop in place
*/
void Decepticon::stop() {
this->left_speed = 0;
this->right_speed = 0;
send_to_arduino();
}
这是输出:
Connected!
Start
l = 0
000000000
000000000
Write Succeeded
End
Hello, World!
left speed: 255
Start
l = 255
000255255
000255255
Write Succeeded
End
End Test
Start
l = 0
000000000
000000000
Write Succeeded
End
您问的是:
The last line is "cout << "End Test" << endl;", but in the output, there's something else after that statement and that's the output that gets written when Decepticon is initalized (as you can see in the beginning of the output). Any idea why that's happening?
Decepticon
的析构函数在从函数返回之前和执行函数中的最后一行代码之后为 d
调用。如果 Decepticon
的析构函数打印任何消息,它们将在函数最后一行输出后打印。
Decepticon
析构函数正在调用 stop()
,后者又调用 send_to_arduino()
。 函数是输出您在 main()
完成后看到的消息的函数。
如果您不希望发生这种情况,您可能最好修改析构函数,使其不调用特定的函数链。
或者,如果您需要完成实际工作但没有生成输出,您可以简单地拥有一个名为 echo
的成员变量,它在构造函数中初始化为 true 并在构造函数的开头设置为 false析构函数。然后修改send_to_arduino()
为:
void Decepticon::send_to_arduino() {
int l, r, c;
if (echo) std::cout << "Start" << std::endl;
l = this->left_speed < 0 ? -this->left_speed + 255 : this->left_speed;
if (echo) std::cout << "l = " << l << std::endl;
r = this->right_speed < 0 ? -this->right_speed + 255 : this->right_speed;
c = this->claw_pos;
sprintf(this->buf, "%03d%03d%03d\n", c, l, r);
if (echo) std::cout << this->buf << std::endl;
serial_write(&this->commlink, this->buf);
if (echo) std::cout << "End" << std::endl;
}
毫无疑问,根据您的需要,还有其他方法可以做到这一点。
我几乎肯定 Decepticon
class 有一个析构函数打印出一些东西。一旦 d
超出范围,就会打印一些东西,但是无法判断是否正是 d
谁在打印一些东西。例如,全局变量可能是一个例外。
编辑:
我现在知道是
stop();
或
serial_disconnect(&this->commlink);
它负责打印一些东西,并且证实它确实是析构函数。
编辑:如果你想停止向 arduino 发送额外的消息,请尝试在析构函数的停止调用中注释掉发送调用。
遵循析构函数的逻辑
它调用 stop
调用 send_to_arduino
等。这是额外的输出。
你可以通过调试器来做到这一点