我有一些从 svn 存储库下载的 Qt 代码。我已经有一段时间没研究它了,但我确信它曾经可以编译。
我有一个新版本的 Qt 和编译器(我上次使用的)。我当前的编译器是:mingw 4.9.2 32 位。
所以这是我的问题代码:
QByteArray dataBlock = audioTestFile.read(PACKET_SIZE_TO_ENCODE);
// This line is the issue
uint8Vect_t testVect = encodeData(uint8Vect_t(dataBlock.begin(), dataBlock.end()));
地点:
typedef std::vector<uint8_t> uint8Vect_t;
和
uint8Vect_t encodeData(uint8Vect_t &dataBuff);
所以你可以在这里看到我有一个函数 encodeData(),它接受一个参数 uint8Vect_t &(通过 ref 传递)。我正在传递一个临时变量(我认为是右值),它是使用 QByteArray dataBlock 迭代器(我已经测试过的)的 std::vector 构造函数(其中一个需要两个迭代器)创建的。
但是,我得到了错误:
../audioTest/txaudiostream.cpp: In member function 'void CTxAudioStream::playFile()': ../audioTest/txaudiostream.cpp:212:94: error: no matching function for call to 'CTxAudioStream::encodeData(uint8Vect_t)' uint8Vect_t testVect = encodeData(uint8Vect_t(dataBlock.begin(), dataBlock.end())); ^ ../audioTest/txaudiostream.cpp:212:94: note: candidate is: ../audioTest/txaudiostream.cpp:36:13: note: uint8Vect_t CTxAudioStream::encodeData(uint8Vect_t&) uint8Vect_t CTxAudioStream::encodeData(uint8Vect_t &dataBuff) ^ ../audioTest/txaudiostream.cpp:36:13: note: no known conversion for argument 1 from 'uint8Vect_t {aka std::vector}' to 'uint8Vect_t& {aka std::vector&}'
基本上是说我无法从 uint8Vect_t 转换为 uint8Vect_t&。但是,如果我将 uint8Vect_t 类型的变量传递给函数(而不是构造函数/临时变量的返回值),那么这就可以正常工作。
我认为在 c++11 中你可以传递右值..但我显然在这里遗漏了一些东西。谁能解释一下:
最佳答案
你的问题是
uint8Vect_t encodeData(uint8Vect_t &dataBuff);
此处您引用了 uint8Vect_t .这适用于普通变量,但 uint8Vect_t(dataBlock.begin(), dataBlock.end())是一个临时对象,不能绑定(bind)到左值引用。
如果encodeData()不改变dataBuff那么最简单的解决方案就是采取 const &可以绑定(bind)到临时文件。
uint8Vect_t encodeData(const uint8Vect_t &dataBuff);
如果你必须改变dataBuff的内容那么你将不得不编写另一个版本的 encodeData()采用右值引用
uint8Vect_t encodeData(uint8Vect_t &&dataBuff);
这将允许函数绑定(bind)到临时 vector ,您可以像处理法线 vector 一样在函数中处理它。
我相信你看到这个的原因是你的旧编译器是 Microsoft Visual Studio 的一个版本。 MSVS 有一个默认情况下启用的非标准扩展,它允许临时对象绑定(bind)到左值引用。您可以在以下位置阅读更多相关信息:Non-const reference bound to temporary, Visual Studio bug?
添加这个是为了向您展示如何更改 encodeData()无需编写新函数即可获取右值引用。
#include <iostream>
#include <vector>
std::vector<int> modify(std::vector<int>& foo)
{
for (auto & e : foo)
e *= 2;
return foo;
}
std::vector<int> modify(std::vector<int>&& foo)
{
return modify(foo);
}
int main()
{
std::vector<int> foo = modify({ 1,2,3,4,5 });
for (const auto & e : foo)
std::cout << e << " ";
}
在上面的例子中modify({ 1,2,3,4,5 })电话 modify(std::vector<int>&& foo)然后在函数 foo 中是一个lvaue。然后我们返回将"new"左值传递给 modify(std::vector<int>& foo) 的结果。然后返回修改后的 vector 。
关于c++ - 传递右值作为引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34655590/
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere
我想让一个yaml对象引用另一个,如下所示:intro:"Hello,dearuser."registration:$introThanksforregistering!new_message:$introYouhaveanewmessage!上面的语法只是它如何工作的一个例子(这也是它在thiscpanmodule中的工作方式。)我正在使用标准的rubyyaml解析器。这可能吗? 最佳答案 一些yaml对象确实引用了其他对象:irb>require'yaml'#=>trueirb>str="hello"#=>"hello"ir
我没有找到太多关于如何执行此操作的信息,尽管有很多关于如何使用像这样的redirect_to将参数传递给重定向的建议:action=>'something',:controller=>'something'在我的应用程序中,我在路由文件中有以下内容match'profile'=>'User#show'我的表演Action是这样的defshow@user=User.find(params[:user])@title=@user.first_nameend重定向发生在同一个用户Controller中,就像这样defregister@title="Registration"@user=Use
对于作为String#tr参数的单引号字符串文字中反斜杠的转义状态,我觉得有些神秘。你能解释一下下面三个例子之间的对比吗?我特别不明白第二个。为了避免复杂化,我在这里使用了'd',在双引号中转义时不会改变含义("\d"="d")。'\\'.tr('\\','x')#=>"x"'\\'.tr('\\d','x')#=>"\\"'\\'.tr('\\\d','x')#=>"x" 最佳答案 在tr中转义tr的第一个参数非常类似于正则表达式中的括号字符分组。您可以在表达式的开头使用^来否定匹配(替换任何不匹配的内容)并使用例如a-f来匹配一
我正在使用RubyonRails3.0.9,我想生成一个传递一些自定义参数的link_toURL。也就是说,有一个articles_path(www.my_web_site_name.com/articles)我想生成如下内容:link_to'Samplelinktitle',...#HereIshouldimplementthecode#=>'http://www.my_web_site_name.com/articles?param1=value1¶m2=value2&...我如何编写link_to语句“alàRubyonRailsWay”以实现该目的?如果我想通过传递一些
如何在Ruby中按名称传递函数?(我使用Ruby才几个小时,所以我还在想办法。)nums=[1,2,3,4]#Thisworks,butismoreverbosethanI'dlikenums.eachdo|i|putsiend#InJS,Icouldjustdosomethinglike:#nums.forEach(console.log)#InF#,itwouldbesomethinglike:#List.iternums(printf"%A")#InRuby,IwishIcoulddosomethinglike:nums.eachputs在Ruby中能不能做到类似的简洁?我可以只
如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:
当我创建一个Rails应用程序时,控制台:railsnewfoo我的代码可以使用字符串“foo”吗?puts"Yourapp'snameis"+app_name_bar 最佳答案 Rails.application.class将为您提供应用程序的全名(例如YourAppName::Application)。从那里您可以使用Rails.application.class.parent获取模块名称。 关于ruby-on-rails-应用程序的名称是否可以作为变量使用?,我们在StackOve
我对如何计算通过{%assignvar=0%}赋值的变量加一完全感到困惑。这应该是最简单的任务。到目前为止,这是我尝试过的:{%assignamount=0%}{%forvariantinproduct.variants%}{%assignamount=amount+1%}{%endfor%}Amount:{{amount}}结果总是0。也许我忽略了一些明显的东西。也许有更好的方法。我想要存档的只是获取运行的迭代次数。 最佳答案 因为{{incrementamount}}将输出您的变量值并且不会影响{%assign%}定义的变量,我