Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 为什么我需要在参数中添加常量?_Linux_Compiler Errors_G++_Gtkmm - Fatal编程技术网

Linux 为什么我需要在参数中添加常量?

Linux 为什么我需要在参数中添加常量?,linux,compiler-errors,g++,gtkmm,Linux,Compiler Errors,G++,Gtkmm,我有这个功能: void SpookyBoy( bool Troublemaker, const Glib::ustring& name, HorrorPunkBand& band ); 如果删除单词const,则会出现以下错误: no known conversion for argument 2 from ‘const char [5]’ to ‘Glib::ustring&’ 我只是想知道编译器做了什么,有人能告诉我为什么编译器可以用const这个词来强制转换

我有这个功能:

void
SpookyBoy( bool Troublemaker, const Glib::ustring& name, HorrorPunkBand& band );
如果删除单词
const
,则会出现以下错误:

 no known conversion for argument 2 from ‘const char [5]’ to ‘Glib::ustring&’
我只是想知道编译器做了什么,有人能告诉我为什么编译器可以用
const
这个词来强制转换吗



即使我不得不承认,这两个问题对于已经知道答案的人(fortiori)来说是相似的,但对于还不知道答案的人(priori)来说肯定是不一样的。

你显然是在用字符串文字作为参数调用你的函数

SpookyBoy(..., "1234", ...);
编译器可以隐式地将字符串文本(在本例中为type
const char[5]
)转换为type
Glib::ustring
。但是隐式转换的结果是一个临时对象。在C++语言中,只有const LValk引用可以绑定到临时对象。非常量引用不能绑定到临时对象

如果没有函数声明中的
const
,您将不得不以

Glib::ustring name("1234");
SpookyBoy(..., name, ...);
i、 e.通过显式引入
Glib::ustring
类型的命名对象并将其作为参数传递


另请参见

您显然是使用字符串文字作为参数调用函数

SpookyBoy(..., "1234", ...);
编译器可以隐式地将字符串文本(在本例中为type
const char[5]
)转换为type
Glib::ustring
。但是隐式转换的结果是一个临时对象。在C++语言中,只有const LValk引用可以绑定到临时对象。非常量引用不能绑定到临时对象

如果没有函数声明中的
const
,您将不得不以

Glib::ustring name("1234");
SpookyBoy(..., name, ...);
i、 e.通过显式引入
Glib::ustring
类型的命名对象并将其作为参数传递


另请参见

您显然是使用字符串文字作为参数调用函数

SpookyBoy(..., "1234", ...);
编译器可以隐式地将字符串文本(在本例中为type
const char[5]
)转换为type
Glib::ustring
。但是隐式转换的结果是一个临时对象。在C++语言中,只有const LValk引用可以绑定到临时对象。非常量引用不能绑定到临时对象

如果没有函数声明中的
const
,您将不得不以

Glib::ustring name("1234");
SpookyBoy(..., name, ...);
i、 e.通过显式引入
Glib::ustring
类型的命名对象并将其作为参数传递


另请参见

您显然是使用字符串文字作为参数调用函数

SpookyBoy(..., "1234", ...);
编译器可以隐式地将字符串文本(在本例中为type
const char[5]
)转换为type
Glib::ustring
。但是隐式转换的结果是一个临时对象。在C++语言中,只有const LValk引用可以绑定到临时对象。非常量引用不能绑定到临时对象

如果没有函数声明中的
const
,您将不得不以

Glib::ustring name("1234");
SpookyBoy(..., name, ...);
i、 e.通过显式引入
Glib::ustring
类型的命名对象并将其作为参数传递


另请参见

您的函数声明真的是这样的吗?根据错误消息,
ustring
参数显然声明为
const Glib::ustring&name
,即作为引用。@AnT是的,你说得对,我忘记了&+1对你可能的重复,你的函数声明真的是这样吗?根据错误消息,
ustring
参数显然声明为
const Glib::ustring&name
,即作为引用。@AnT是的,你说得对,我忘记了&+1对你可能的重复,你的函数声明真的是这样吗?根据错误消息,
ustring
参数显然声明为
const Glib::ustring&name
,即作为引用。@AnT是的,你说得对,我忘记了&+1对你可能的重复,你的函数声明真的是这样吗?根据错误消息,
ustring
参数显然声明为
const Glib::ustring&name
,即作为引用。@AnT yes you right,我忘记了&+1 to you可能的重复项