C++ 将向量从一个类转换为另一个类向量

C++ 将向量从一个类转换为另一个类向量,c++,c++11,C++,C++11,我尝试使用动态转换将一个类向量的值推送到另一个类向量。但是我有一个分割错误 当我使用gdb调试程序时,我发现dynamic_cast没有发生,因此没有值推送到向量 这里我试图将元素从std::vector*点复制到std::vector*lpoints BPatch_point和point是完全不相关的类 你能帮我一下吗 int main(int argc , char *argv[]) { BPatch bpatch; int pid; if

我尝试使用动态转换将一个类向量的值推送到另一个类向量。但是我有一个分割错误

当我使用gdb调试程序时,我发现
dynamic_cast
没有发生,因此没有值推送到向量

这里我试图将元素从
std::vector*点
复制到
std::vector*lpoints

BPatch_point
point
是完全不相关的类

你能帮我一下吗

int main(int argc , char *argv[])
{

        BPatch bpatch;
        int pid;
        if (argc != 3) {

                exit(1);
        }

        pid=atoi(argv[1]);

        char name[ 40 ];
        cout<<"The attached pid is "<<pid<<endl;


        BPatch_process *appProc = bpatch.processAttach("",pid);
        BPatch_image *img = appProc->getImage();

        std::vector<BPatch_function *> functions;
        std::vector<BPatch_point *> *points;


        img->findFunction(argv[2], functions);
        if(functions.size()==0) {
                cout<<"unable to find the function "<<argv[2]<<endl;
                return -1;
        }
        else {
              cout<<"The "<<argv[2]<<" function is found"<<endl;
        }
        points = functions[0]->findPoint(BPatch_entry);
        if ((*points).size() == 0) {
                cout<<"Not able to find the points"<<endl;
        }
        cout<<"The points is "<<(*points)[0];
        std::vector<ldframework::Point *> *lpoints=NULL;
        for(unsigned int i=0; i<(*points).size();i++)
    {
        lpoints->push_back(dynamic_cast<ldframework::Point *>((*points).at(i)));
    }
}
intmain(intargc,char*argv[])
{
拍拍拍拍拍;
int-pid;
如果(argc!=3){
出口(1);
}
pid=atoi(argv[1]);
字符名[40];
库特
在这里,我试图将元素从std::vector*点复制到std::vector*lpoints。你能帮我一下吗

int main(int argc , char *argv[])
{

        BPatch bpatch;
        int pid;
        if (argc != 3) {

                exit(1);
        }

        pid=atoi(argv[1]);

        char name[ 40 ];
        cout<<"The attached pid is "<<pid<<endl;


        BPatch_process *appProc = bpatch.processAttach("",pid);
        BPatch_image *img = appProc->getImage();

        std::vector<BPatch_function *> functions;
        std::vector<BPatch_point *> *points;


        img->findFunction(argv[2], functions);
        if(functions.size()==0) {
                cout<<"unable to find the function "<<argv[2]<<endl;
                return -1;
        }
        else {
              cout<<"The "<<argv[2]<<" function is found"<<endl;
        }
        points = functions[0]->findPoint(BPatch_entry);
        if ((*points).size() == 0) {
                cout<<"Not able to find the points"<<endl;
        }
        cout<<"The points is "<<(*points)[0];
        std::vector<ldframework::Point *> *lpoints=NULL;
        for(unsigned int i=0; i<(*points).size();i++)
    {
        lpoints->push_back(dynamic_cast<ldframework::Point *>((*points).at(i)));
    }
}
点和点是完全不相关的类

这可以转化为:

我有一个有大象的动物园。你能帮我把这些大象变成橘子并放在一个橘子盒子里吗

当类不相关时,它们唯一常见的是
void*
-“指向某物的指针”。另一种选择是对任何值使用占位符,例如
boost::any

但核心问题是:为什么要将一种类型的类移动到另一种类型的类的容器中。有99.8%的可能性,首先是您做错了什么,这是您应该找到解决方案的地方


编辑:(回应评论)

你能建议一下如何使用boost::any方法或void*方法来实现吗

std::vector
替换为
std::vector
(如果您可以在项目中使用boost库)或
std::vector
。然后您将能够在其中放置任何内容

尽管我仍然非常确信,你在做一些非常错误的事情。但是,如果你真的知道你在做什么,请随意使用所描述的解决方案

在这里,我试图将元素从std::vector*点复制到std::vector*lpoints。你能帮我一下吗

int main(int argc , char *argv[])
{

        BPatch bpatch;
        int pid;
        if (argc != 3) {

                exit(1);
        }

        pid=atoi(argv[1]);

        char name[ 40 ];
        cout<<"The attached pid is "<<pid<<endl;


        BPatch_process *appProc = bpatch.processAttach("",pid);
        BPatch_image *img = appProc->getImage();

        std::vector<BPatch_function *> functions;
        std::vector<BPatch_point *> *points;


        img->findFunction(argv[2], functions);
        if(functions.size()==0) {
                cout<<"unable to find the function "<<argv[2]<<endl;
                return -1;
        }
        else {
              cout<<"The "<<argv[2]<<" function is found"<<endl;
        }
        points = functions[0]->findPoint(BPatch_entry);
        if ((*points).size() == 0) {
                cout<<"Not able to find the points"<<endl;
        }
        cout<<"The points is "<<(*points)[0];
        std::vector<ldframework::Point *> *lpoints=NULL;
        for(unsigned int i=0; i<(*points).size();i++)
    {
        lpoints->push_back(dynamic_cast<ldframework::Point *>((*points).at(i)));
    }
}
点和点是完全不相关的类

这可以转化为:

我有一个有大象的动物园。你能帮我把这些大象变成橘子并放在一个橘子盒子里吗

当类不相关时,它们唯一常见的是
void*
-“指向某物的指针”。另一种选择是对任何值使用占位符,例如
boost::any

但核心问题是:为什么要将一种类型的类移动到另一种类型的类的容器中。有99.8%的可能性,首先是您做错了什么,这是您应该找到解决方案的地方


编辑:(回应评论)

你能建议一下如何使用boost::any方法或void*方法来实现吗

std::vector
替换为
std::vector
(如果您可以在项目中使用boost库)或
std::vector
。然后您将能够在其中放置任何内容


尽管我仍然非常确信,你做了一些非常错误的事情。但是,如果你真的知道你在做什么,请随意使用所描述的解决方案。

你需要做的是逐个变换对象,而不是强制转换对象。幸运的是,标准库使它变得非常简单

#include <vector>
#include <algorithm>
#include <iterator>

ClassB * ConvertAtoB(ClassA * a)
{
    // create a new object of type ClassB here
}

int main()
{
    std::vector<ClassA*> a;

    // fill 'a' with data
    // ...

    // then transform it into 'b'
    std::vector<ClassB*> b;
    std::transform(a.begin(), a.end(), std::back_inserter(b), ConvertAtoB);
}
#包括
#包括
#包括
ClassB*ConvertAtoB(ClassA*a)
{
//在此处创建ClassB类型的新对象
}
int main()
{
std::载体a;
//用数据填充“a”
// ...
//然后将其转换为“b”
std::载体b;
std::transform(a.begin()、a.end()、std::back_插入器(b)、ConvertAtoB);
}

您需要做的是逐个转换对象,而不是强制转换对象。幸运的是,标准库使其变得非常简单

#include <vector>
#include <algorithm>
#include <iterator>

ClassB * ConvertAtoB(ClassA * a)
{
    // create a new object of type ClassB here
}

int main()
{
    std::vector<ClassA*> a;

    // fill 'a' with data
    // ...

    // then transform it into 'b'
    std::vector<ClassB*> b;
    std::transform(a.begin(), a.end(), std::back_inserter(b), ConvertAtoB);
}
#包括
#包括
#包括
ClassB*ConvertAtoB(ClassA*a)
{
//在此处创建ClassB类型的新对象
}
int main()
{
std::载体a;
//用数据填充“a”
// ...
//然后将其转换为“b”
std::载体b;
std::transform(a.begin()、a.end()、std::back_插入器(b)、ConvertAtoB);
}

您必须单独处理每个元素,或者只使用std::vector>点与点之间的关系是什么?那么您希望该cast做什么?如果它们不相关,您将需要某种转换。您需要在
点与
ldframework::点
之间进行转换;即它们被存储的事实in vectors是不相关的。你在问我们如何在不提供任何关于A和B的信息的情况下将一个类A的对象转换为一个不相关的类B的对象。你期望得到什么答案?你必须自己处理每个元素,或者只使用std::vector>BPatch_点和点之间的关系是什么?那么你期望该强制转换做什么?如果它们是不相关的,你需要某种转换。你需要在
BPatch_point
ldframework::point
之间进行转换;它们存储在向量中的事实是不相关的。你在问我们如何在不提供关于a和B的任何信息的情况下将类a的对象转换为不相关的类B的对象。答案是什么你期待吗?对于我的项目要求,它需要完成。我100%确定这需要完成。但我不知道如何才能完成。谢谢你的帮助suggestion@user2659232“但我不知道这是可以做到的。”不,这不能用无关的类和
dynamic\u cast
。好的。那么我真的很抱歉。你能建议我吗如何使用boost::any方法或void*实现method@user2659232请提供一个决定性的原因(在问题中,而不是在评论中),即解释迫使您这样做的要求