简体   繁体   English

GTKMM错误:空虚值不会被忽视,因为它应该是

[英]GTKMM Error: Void Value Not Ignored As it Ought to Be

I am trying to create a simple window in GTKMM that contains a box. 我试图在GTKMM中创建一个包含框的简单窗口。 I've got the window part working, but I can't get my box code to work. 我有窗口部分工作,但我无法让我的盒子代码工作。 I am following along with this tutorial 我正在按照本教程进行操作

I think the tutorial is a little bit dated because Anjuta (the IDE I'm using) generated some different code. 我认为该教程有点过时了,因为Anjuta(我正在使用的IDE)生成了一些不同的代码。 Here is my code that should add a box: 这是我的代码,应该添加一个框:

 #include <gtkmm.h>
#include <iostream>
#include "config.h"
 using namespace Gtk;



  int main (int argc, char *argv[])
  {
Gtk::Main kit(argc, argv);


Gtk::Window *main_win = new Gtk::Window (Gtk::WINDOW_TOPLEVEL);
main_win->set_title ("Image-Viewer");

Gtk::Box *box = Gtk::manage (new Gtk::Box());
box ->set_orientation (ORIENTATION_VERTICAL);
box->set_spacing(6);
*main_win -> add(*box);

if (main_win)
{
    kit.run(*main_win);
}
return 0;
 }

In the code on the tutorial the window is not created in the same way. 在本教程的代码中,窗口不是以相同的方式创建的。 As you can see below, the window in my code is being created so that it is in the heap, rather than the stack. 正如您在下面看到的,我的代码中的窗口正在创建,因此它位于堆中,而不是堆栈中。 (or at least I think [I am new to C++]). (或者至少我认为[我是C ++的新手])。 I know that items in the heap should be used like a pointer, so for the add function I did that, (rather than using the dot notation described in the tutorial). 我知道堆中的项应该像指针一样使用,所以对于add函数我做了,(而不是使用教程中描述的点符号)。 When I run this code, I get an error stating the following: 当我运行此代码时,我收到一条错误说明以下内容:

error:void value not ignored as it out to be

The error is pertaining to the add method being called on the window. 该错误与在窗口上调用的add方法有关。 Can somone tell me what I'm doing incorrectly? 可以告诉我我做错了什么吗? Thanks 谢谢

This instruction: 这条指令:

Gtk::Window *main_win = new Gtk::Window (Gtk::WINDOW_TOPLEVEL);

Declares a pointer to Gtk::Window . 声明一个指向Gtk::Window的指针。 Later, you do: 之后,你做:

*main_win -> add(*box);

This is incorrect, because you basically try to apply operator -> after you already dereferenced the main_win pointer - and the result of this dereferencing is not a pointer itself, but a reference to an object of type Gtk::Window . 这是不正确的,因为在你已经取消引用main_win指针之后,你基本上尝试应用operator -> - 这个解除引用的结果不是指针本身,而是对Gtk::Window类型的对象的引用。

To fix the problem, remove the extra dereferencing: 要解决此问题,请删除额外的解除引用:

main_win -> add(*box);

NOTE: 注意:

I do not know Gtk::Window and its member function add() , but if it is the case that add() accepts a pointer as its argument, then you also shouldn't dereference box . 我不知道Gtk::Window及其成员函数add() ,但如果是add()接受指针作为其参数的情况,那么你也不应该取消引用box

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM