gpt4 book ai didi

c++ - vector 的指针问题

转载 作者:太空宇宙 更新时间:2023-11-04 14:59:15 28 4
gpt4 key购买 nike

我在尝试将自定义类的对象 push_back 为类型为自定义类的指针 vector 时遇到了很多麻烦。请查看下面的代码以及收到的错误。我在 Windows xp 上使用带有 CDT 插件和 OpenCV 的 Eclipse。

我花了很多时间试图找到答案,但无济于事!ps 我是学生,指导等不是我的事!

    std:: vector<RoadLine>* LaneChangeDetector::roadLines(IplImage* img_8uc1, IplImage* img_8uc3, IplImage* img_edge, std::vector <RoadLine>* roadVector){

CvMemStorage* storage = cvCreateMemStorage(0);
CvSeq* lines = 0;
CvMemStorage* roadStorage = cvCreateMemStorage(0);
CvSeq* roadLines = 0;

// Probabalistic Hough transform returns line segments from edge detected image
lines = cvHoughLines2( img_edge, storage, CV_HOUGH_PROBABILISTIC, 1, CV_PI/180, 50, 200, 200 );

// Sequence roadlines, lines with correct slope are added to this sequence
roadLines = cvCreateSeq(0, lines->header_size, lines->elem_size, roadStorage);

// slope
double m = 0.0;

// Point of intersection
CvPoint poi;

for(int i = 0; i < lines->total; i++ ){
CvPoint* line = (CvPoint*)cvGetSeqElem(lines,i);
CvPoint pt1 = line[0];
CvPoint pt2 = line[1];

double x1 = double(pt1.x);
double y1 = double(pt1.y);
double x2 = double(pt2.x);
double y2 = double(pt2.y);

if(pt1.x == pt2.x){
m = 1.0;
}
else{
m = (double(y2 - y1)/(double(x2 - x1)));
}

if( ((m>0.45) && (m<0.75)) || ((m<-0.45) && (m>-0.75)) ){

// If the slope is between measured parameters add to roadLines sequence for further analysis
cvSeqPush(roadLines, line);
}
}

// otherRoadLine used for comparison
CvPoint* otherRoadLine;

for(int a=0; a<roadLines->total; a++){

CvPoint* roadLine = (CvPoint*)cvGetSeqElem(roadLines,a);
CvPoint rl1 = roadLine[0];
CvPoint rl2 = roadLine[1];
int lineCount = 0;

if(a>0){

// Test the current line against all the previous lines in the sequence.
// If the current line is far enough away from all other lines then draw it
for(int b=0; b<a; b++){
otherRoadLine = (CvPoint*)cvGetSeqElem(roadLines,b);
if((roadLine->x > ((otherRoadLine->x) + 200)) || (roadLine->x < ((otherRoadLine->x) - 200)) ){
lineCount++;
}
}
if(lineCount == a){
cvLine(img_final, roadLine[0], roadLine[1], CV_RGB(0,0,255), 3, CV_AA, 0 );
RoadLine myLine = RoadLine(roadLine, 1);
roadVector->push_back(myLine); //ERROR OCCURS HERE
cvShowImage("Plate Detection", img_final);
cvWaitKey(0);
}
}
else{
cvLine(img_final, roadLine[0], roadLine[1], CV_RGB(0,0,255), 3, CV_AA, 0 );
RoadLine myLine = RoadLine(roadLine, 1);
roadVector->push_back(myLine //ERROR OCCURS HERE
cvShowImage("Plate Detection", img_final);
cvWaitKey(0);
}
}

if(roadVector->size() >= 2){
int pos = 0;
RoadLine line1 = roadVector->at(pos);
RoadLine line2 = roadVector->at(pos + 1);

CvPoint* A = line1.line;
CvPoint p1 = A[0];
CvPoint p2 = A[1];

int A1 = p1.y - p2.y;
int B1 = p1.x - p2.x;
int C1 = (p1.x*p2.y) - (p1.y*p2.x);

CvPoint* B = line2.line;
CvPoint p3 = B[0];
CvPoint p4 = B[1];

int A2 = p3.y - p4.y;
int B2 = p3.x - p4.x;
int C2 = (p3.x*p4.y) - (p3.y*p4.x);

int det = A2*B1 - A1*B2;

if(det == 0){
printf("Lines are parallel");
}
else{
int x = ( C1*(p3.x - p4.x) - (p1.x - p2.x)*C2 )/det;
int y = ( C1*(p3.y - p4.y) - (p1.y - p2.y)*C2 )/det;

poi.x = x;
poi.y = y;

horizon = poi.x;

cvCircle(img_final, poi, 10, CV_RGB(255, 0, 0), 2, CV_AA, 0);
}
}

cvShowImage("Plate Detection", img_final);
cvWaitKey(0);

return roadVector;
}

可以在这里看到自定义类 RoadLine

    #include <cv.h>
class RoadLine{
private:
CvPoint* line;
int lane;
public:
RoadLine(CvPoint*, int);
};
RoadLine::RoadLine(CvPoint* aLine, int aLane){
line = aLine;
lane = aLane;
}

通过调试我可以看到“std::vector * roadVector”被正确初始化。

这是 Eclipse 告诉我的内容:

3 std::vector<RoadLine, std::allocator<RoadLine> >::push_back() F:\MinGW\include\c++\3.4.5\bits\stl_vector.h:560 0x0043e3f9

4 void std::_Construct<RoadLine, RoadLine>() F:\MinGW\include\c++\3.4.5\bits\stl_construct.h:81 0x0044015d

然后程序跳转到STL_construct.h中的这段代码

  template<typename _T1, typename _T2>
inline void
_Construct(_T1* __p, const _T2& __value)
{
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 402. wrong new expression in [some_]allocator::construct
::new(static_cast<void*>(__p)) _T1(__value); //DEBUG THROWS ME TO THIS LINE
}

再次感谢您的帮助。

干杯

帕特

最佳答案

您不使用指针 vector ,而是使用对象 vector 。在这种情况下,您的类需要有一个复制构造函数,因为 push_back 存储对象的拷贝。

作为一般调试建议,尝试通过删除尽可能多的代码来归结问题,但仍然会看到不正确的行为。尝试找到失败的最简单示例。

关于c++ - vector 的指针问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/692779/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com