C++ 如何动态创建对象并访问其指针
这似乎是一个微不足道的问题,但我似乎找不到解决办法 我所拥有的:C++ 如何动态创建对象并访问其指针,c++,C++,这似乎是一个微不足道的问题,但我似乎找不到解决办法 我所拥有的: void VideoHandler::demoBurn(QString fileName) { // Create a reader for a video openshot::FFmpegReader r("raw_videos/example0.mp4"); r.Open(); // Open the target reader // Create a writer
void VideoHandler::demoBurn(QString fileName) {
// Create a reader for a video
openshot::FFmpegReader r("raw_videos/example0.mp4");
r.Open(); // Open the target reader
// Create a writer
openshot::FFmpegWriter w("edited_videos/NewVideo.mp4");
w.SetAudioOptions(true, "libvorbis", 44100, 2, openshot::ChannelLayout::LAYOUT_STEREO, 128000);
w.SetVideoOptions(true,"libx264" , openshot::Fraction(30,1), r.info.width, r.info.height, openshot::Fraction(1,1), false, false, 300000);
w.Open();
openshot::Timeline t(r.info.width,r.info.height, r.info.fps, 44100,2, openshot::ChannelLayout::LAYOUT_STEREO);
// Clip example
openshot::Clip c1(new openshot::QtImageReader("edited_videos/0.png"));
c1.Layer(1);
c1.Position(5.9);
c1.Start(5.9);
c1.End(10.0);
c1.scale = openshot::SCALE_NONE;
c1.gravity = openshot::GRAVITY_TOP_LEFT;
c1.location_x = 0.0;
c1.location_y = 0.2;
std::list<openshot::Clip> clipList;
for (int i = 0; i < 2; i++) {
QString imageName = QString("edited_videos/%1.png").arg(i);
clipList.push_back(openshot::Clip(new openshot::QtImageReader(imageName.toUtf8().toStdString())));
}
// Add clip values and add to timeline
std::list<openshot::Clip>::iterator it;
int test = 0;
for (it = clipList.begin(); it != clipList.end(); it++) {
it->Layer(1);
if(test == 0) {
it->Position(5.9);
it->Start(5.9);
it->End(10.0);
} else {
it->Position(10.0);
it->Start(10.0);
it->End(15.0);
}
it->scale = openshot::SCALE_NONE;
it->gravity = openshot::GRAVITY_TOP_LEFT;
it->location_x = 0.0;
it->location_y = 0.2;
test++;
t.AddClip(&it);
}
// Add clips to timeline
t.AddClip(&c1);
openshot::Clip c2(new openshot::FFmpegReader("raw_videos/example0.mp4"));
c2.Position(0.0);
c2.Layer(0);
t.AddClip(&c2);
// Open the timeline reader
t.Open();
// Close the timeline reader
w.WriteFrame(&t, 1, r.info.video_length);
// Close the reader & writer
t.Close();
w.Close();
r.Close();
}
对于t.AddClip(&it)
错误:
cannot initialize a parameter of type 'openshot::Clip *' with an rvalue of type 'std::list<openshot::Clip>::iterator *' (aka '_List_iterator<openshot::Clip> *')
无法使用“std::list::iterator*”类型的右值初始化“openshot::Clip*”类型的参数(也称为“\u list\u iterator*”)
我可以理解这个错误,但是我如何给它一个正确的指针,我想it
会保持指向对象的指针
由于我从未进行过动态对象分配,我不确定这是否是正确的方法,我是否使用了错误类型的列表
因为我从未做过动态对象分配,所以我不确定这样做是否正确
我不知道openshot,所以我不能确定,但是那些简单的
新的表达式似乎有点可疑。除非API强制使用它,否则您可能应该首先使用一个唯一的指针,假设需要动态分配。经过多次尝试和错误,我成功地让它工作起来。我改变的是:
创建一个指针列表(不管它是QList
还是std:list
,经过测试并且两者工作相同)
并使用指向实际对象的指针重新引用所有迭代对象。
感谢@eerorika提供有关(&*it)的信息
QList-clipList;
对于(int i=0;i<2;i++){
QString imageName=QString(“已编辑视频/%1.png”).arg(i);
clipList.push_back(新openshot::Clip(新openshot::QtImageReader(imageName.toUtf8().tostString()));
}
//添加剪辑值并添加到时间轴
QList::迭代器it;
int检验=0;
for(it=clipList.begin();it!=clipList.end();it++){
(*it)->第(1)层;
如果(测试==0){
(*it)->位置(5.9);
(*it)->启动(5.9);
(*it)->End(10.0);
}否则{
(*it)->位置(10.0);
(*it)->启动(10.0);
(*it)->End(15.0);
}
(*it)->scale=openshot::scale\u NONE;
(*it)->gravity=openshot::gravity\u TOP\u LEFT;
(*it)->位置_x=0.0;
(*it)->位置_y=0.2;
测试++;
t、 AddClip(&*(*it));
}
代码使用Qt并不意味着问题与Qt有关。在您的情况下,如果使用std::string更改QString,您仍然会遇到问题。标记的想法是吸引社区对特定主题的关注,在这种情况下,Qt是不相关的。我不确定它是否是不相关的,因为Qt有自己的列表类型,因此,在这种情况下,可能其中一个可以工作。但在您的情况下,错误消息是显而易见的:。。。。std::list::iterator…
错误是由std::list引起的,如果错误出现在QList、QVector等中,那么我会同意,但在您的情况下,这一点太明显了。现在它出现在QList中……而“eerorika”所做的努力毫无价值?嗯,我是说OP(我是指你)提出了一个问题,但是在中间,它改变了代码来保存一个标签,告诉我我不知道什么,然后你把宝贵的时间浪费在另一个用户身上。谢谢,这个代码> T.Adclipse(*it);<代码>解决了指针问题。
cannot initialize a parameter of type 'openshot::Clip *' with an rvalue of type 'std::list<openshot::Clip>::iterator *' (aka '_List_iterator<openshot::Clip> *')
std::list<openshot::Clip>::iterator it;
...
t.AddClip(&it);
t.AddClip(&*it);
QList<openshot::Clip*> clipList;
for (int i = 0; i < 2; i++) {
QString imageName = QString("edited_videos/%1.png").arg(i);
clipList.push_back(new openshot::Clip(new openshot::QtImageReader(imageName.toUtf8().toStdString())));
}
// Add clip values and add to timeline
QList<openshot::Clip*>::iterator it;
int test = 0;
for (it = clipList.begin(); it != clipList.end(); it++) {
(*it)->Layer(1);
if(test == 0) {
(*it)->Position(5.9);
(*it)->Start(5.9);
(*it)->End(10.0);
} else {
(*it)->Position(10.0);
(*it)->Start(10.0);
(*it)->End(15.0);
}
(*it)->scale = openshot::SCALE_NONE;
(*it)->gravity = openshot::GRAVITY_TOP_LEFT;
(*it)->location_x = 0.0;
(*it)->location_y = 0.2;
test++;
t.AddClip(&*(*it));
}