c++ Struct mit constructors als rückgabetyp einer funktion

cycovery

Erfahrenes Mitglied
Hi!

habe zwei probleme mit struct constructoren.

hier mein struct im header (was da genau gerechnet wird ist eigentlich egal):

Code:
struct MyLine{
	CvPoint pt1,pt2,directionpoint; //pt1 and pt2 points on the line, directionpoint: a point on a circle that represents the lines orientation (used for clustering)
	float rho, theta; //rho distance to zero and theta angle of the line
	int cluster; //to which cluster does the line belong
	MyLine(){};
	MyLine(float rho, float theta): rho(rho), theta(theta), cluster(0){
		double a = cos(theta), b = sin(theta);
		double x0 = a*rho, y0 = b*rho;
		pt1.x = cvRound(x0 + 1000*(-b));
		pt1.y = cvRound(y0 + 1000*(a));
		pt2.x = cvRound(x0 - 1000*(-b));
		pt2.y = cvRound(y0 - 1000*(a));
	};
	MyLine(CvPoint pt1, CvPoint pt2): pt1(pt1), pt2(pt2), cluster(0){
		double dirX = pt2.x-pt1.x, dirY=pt2.y-pt1.y;
		float tmptheta = atan2(dirX,dirY)-M_PI_2;
		double a = cos(tmptheta), b = sin(tmptheta);
		double tmpx = a*2000, tmpy = b*2000;
		CvPoint zero,tmppt;
		tmppt.x=tmpx;tmppt.y=tmpy;
		MyLine tmpline1 = MyLine(zero,tmppt);
		MyLine tmpline2;
		tmpline2.pt1=pt1;tmpline2.pt2=pt2;
		CvPoint2D32f linepoint = Intersection(tmpline1,tmpline2);
		rho = sqrt(linepoint.x*linepoint.x+linepoint.y*linepoint.y);
		theta = atan2(linepoint.x,linepoint.y);
	};
	MyLine(CvPoint pt1, float theta): pt1(pt1), theta(theta), cluster(0){
		double a = cos(theta), b = sin(theta);
		pt2.x = pt1.x+a*2000; pt2.y = pt1.y*2000;
		a = cos(theta-M_PI_2), b = sin(theta-M_PI_2);
		double tmpx = a*2000, tmpy = b*2000;
		CvPoint zero,tmppt;
		tmppt.x=tmpx;tmppt.y=tmpy;
		MyLine tmpline1 = MyLine(zero,tmppt);
		MyLine tmpline2;
		tmpline2.pt1 = pt1, tmpline2.pt2=pt2;
		CvPoint2D32f linepoint = Intersection(tmpline1,tmpline2);
		rho = sqrt(linepoint.x*linepoint.x+linepoint.y*linepoint.y);
	};
};


Frage 1:
Wie krieg ich die implementation der konstruktoren ins cpp file? ist irgendwie hässlich, wenn das im header steht.

Frage2:
Nachdem ich die konstruktoren ins struct eingefügt habe krieg ich bei folgender funktion auf linie "MyLine* CTool...." den compilerror: Error 22 error C4430: missing type specifier - int assumed. Note: C++ does not support default-in

Code:
MyLine* CToolDePerspective::FindLines(IplImage* input )
{
...
MyLine* linien = (MyLine *) malloc (sizeof(MyLine)*line_count);
...
return linien;
}


was geht da schief?
Ja ich weiss - wenn ich ne klasse statt nem struct verwenden würde hätte ich diese Probleme wohl nicht - aber ich würde es trotzdem gerne wissen.

Dankeschön :)
 
1. Wie bei Klassen auch ;).
Deklarier die Konstruktoren einfach im Header in der Struktur und definiere sie dann in
einer Quellcode-Datei.

Zum 2.: Hast du die Headerdatei, die deinen Typ deklariert, mit in die Quellcode-Datei eingebunden?
 
hi

und du solltest new anstatt malloc nutzen,damit dein konstructor auch aufgerufen wird

Code:
MyLine *lines = new MyLine[line_count](rho, theta);

lg
 
hi hightarget, teambeta

@teambeta:
zur implementation: wie sollte das genau aussehen? das struct ist übrigens innerhalb einer klasse definiert
ja der header ist eingebunden. alle anderen funktionen, die in dieser cpp implementiert werden funktionieren. nur die funktion, die als rückgabewert einen Array von MyLine zurückgeben soll wirft diesen Fehler.

@hightarget:
stimmt du hast recht, aber an der stelle will ich eigentlich lediglich genügend platz allozieren. die einzelnen elemente werden dann stück für stück eingefüllt.
 
Hi

wenn du die struct in einer Klasse deklariert ist, musst du den Klassennamen auch davor setzen

myclass.h
Code:
class MyClass {
   public:
      struct MyLine {...};

};

myclass.cpp
Code:
#include "myclass.h"

MyClass::MyLine* funktion() {
   return new MyClass::MyLine;
}

lg
 
Zurück