PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : Probleme bei der Codetrennung unter Linux



KravenZ
06-12-2006, 21:39
Hi,

Hab eine wichtige Frage bei der ich nicht weiterkomme. Ich habe mit C++ eine Klasse geschrieben und sie schön und Header und Definitionsdatei aufgeteilt. Nun binde ich die Header in die Code datei ein die das mainProgramm enthält. Sobald ich nun compilieren will erhalte ich eine Fehlermeldung und zwar:


OGLWidget.o: In function `SWP0607::OGLWidget::drawBall(SWP0607::MyVector3Df )':
/home/bulczak/Desktop/Softwarepraktikum/OGLWidget.cc:276: undefined reference to `SWP0607::MyVector3Df::z()'
/home/bulczak/Desktop/Softwarepraktikum/OGLWidget.cc:276: undefined reference to `SWP0607::MyVector3Df::y()'
/home/bulczak/Desktop/Softwarepraktikum/OGLWidget.cc:276: undefined reference to `SWP0607::MyVector3Df::x()'
collect2: ld returned 1 exit status
make: *** [Softwarepraktikum] Fehler 1


Hier ist meine Headerdatei:


#ifndef _MY_VECTOR3DF_HH_
#define _MY_VECTOR3DF_HH_

namespace SWP0607 {

class MyVector3Df
{
private:
float m_x, m_y, m_z; // Vektorkomponenten
public:
// Konsturktor(en)
MyVector3Df();
MyVector3Df(const MyVector3Df & v);
MyVector3Df(const float, const float, const float);

// Zugriffsmethoden

inline float x();
inline float y();
inline float z();
inline void setX(float);
inline void setY(float);
inline void setZ(float);


// Operatoren

// arithmetische Operatoren
inline MyVector3Df operator + (const MyVector3Df & v); // Komponentenweise
inline MyVector3Df operator - (const MyVector3Df & v); // Komponentenweise
inline MyVector3Df operator * (const MyVector3Df & v); // Komponentenweise
inline MyVector3Df operator / (const MyVector3Df & v); // Komponentenweise
inline MyVector3Df operator * (const float f); // Skalarmultiplikation
friend MyVector3Df operator * (const float f, const MyVector3Df & v);
inline MyVector3Df operator / (const float f); // Skalardivision

// Zuweisungsoperatoren
MyVector3Df& operator = (const MyVector3Df & v);
MyVector3Df& operator += (const MyVector3Df & v);
MyVector3Df& operator -= (const MyVector3Df & v);
MyVector3Df& operator *= (const MyVector3Df & v);
MyVector3Df& operator /= (const MyVector3Df & v);

// Vergleichsoperatoren
bool operator == (const MyVector3Df & v);
bool operator != (const MyVector3Df & v);


// Methoden
inline float length(); // liefert Vektorlänge
inline float lengthSq(); // liefert Vektorlänge²


};
inline MyVector3Df operator * (const float f, const MyVector3Df & v);
}

#endif


Und hier die cc Datei


#include "myvector3df.hh"
#include <math.h>

namespace SWP0607 {

MyVector3Df::MyVector3Df()
{
}

MyVector3Df::MyVector3Df(const MyVector3Df & v) : m_x(v.m_x), m_y(v.m_y), m_z(v.m_z)
{
}

MyVector3Df::MyVector3Df(const float x,
const float y,
const float z) : m_x(x), m_y(y), m_z(z)
{
}

float MyVector3Df::x()
{
return m_x;
}

float MyVector3Df::y()
{
return m_y;
}

float MyVector3Df::z()
{
return m_z;
}

void MyVector3Df::setX(float nX)
{
m_x = nX;
}

void MyVector3Df::setY(float nY)
{
m_y = nY;
}

void MyVector3Df::setZ(float nZ)
{
m_z = nZ;
}

MyVector3Df MyVector3Df::operator + (const MyVector3Df & v)
{
return MyVector3Df(m_x + v.m_x,
m_y + v.m_y,
m_z + v.m_z);
}

MyVector3Df MyVector3Df::operator - (const MyVector3Df & v)
{
return MyVector3Df(m_x - v.m_x,
m_y - v.m_y,
m_z - v.m_z);
}

MyVector3Df MyVector3Df::operator * (const MyVector3Df & v)
{
return MyVector3Df(m_x * v.m_x,
m_y * v.m_y,
m_z * v.m_z);
}

MyVector3Df MyVector3Df::operator / (const MyVector3Df & v)
{
return MyVector3Df(m_x / v.m_x,
m_y / v.m_y,
m_z / v.m_z);
}

MyVector3Df MyVector3Df::operator * (const float f)
{
return MyVector3Df(m_x * f,
m_y * f,
m_z * f);
}

MyVector3Df operator * (const float f, const MyVector3Df & v)
{
return MyVector3Df(v.m_x * f,
v.m_y * f,
v.m_z * f);
}

MyVector3Df MyVector3Df::operator / (const float f)
{
return MyVector3Df(m_x / f,
m_y / f,
m_z / f);
}

MyVector3Df& MyVector3Df::operator = (const MyVector3Df & v)
{
m_x = v.m_x;
m_y = v.m_y;
m_z = v.m_z;

return *this;
}

MyVector3Df& MyVector3Df::operator += (const MyVector3Df & v)
{
m_x += v.m_x;
m_y += v.m_y;
m_z += v.m_z;

return *this;
}

MyVector3Df& MyVector3Df::operator -= (const MyVector3Df & v)
{
m_x -= v.m_x;
m_y -= v.m_y;
m_z -= v.m_z;

return *this;
}

MyVector3Df& MyVector3Df::operator *= (const MyVector3Df & v)
{
m_x *= v.m_x;
m_y *= v.m_y;
m_z *= v.m_z;

return *this;
}

MyVector3Df& MyVector3Df::operator /= (const MyVector3Df & v)
{
m_x /= v.m_x;
m_y /= v.m_y;
m_z /= v.m_z;

return *this;
}

bool MyVector3Df::operator == (const MyVector3Df & v)
{
return (m_x == v.m_x) && (m_y == v.m_y) && (m_y == v.m_z);
}

bool MyVector3Df::operator != (const MyVector3Df & v)
{
return (m_x != v.m_x) || (m_y != v.m_y) || (m_y != v.m_z);
}

float MyVector3Df::length()
{
return sqrtf(m_x*m_x + m_y*m_y + m_z*m_z);
}

float MyVector3Df::lengthSq()
{
return m_x*m_x + m_y*m_y + m_z*m_z;
}

}


Die Fehlermeldung die ich da erhalte bezieht sich auf die Zugriffsmethoden weil ich nur die in der Hauptdatei benutzt habe. Aber sobald ich eine andere Methode benutze wird auch gemeckert.
Muss ich vielleicht etwas in der Makefile dazuschreiben?

KravenZ
06-12-2006, 22:03
Jetzt habe ich nochmal zum testen ein ganz simples main Programm aufgesetzt:


#include <stdio.h>
#include "myvector3df.hh"

using namespace SWP0607;

int main()
{
MyVector3Df test();
test.length();
return 0;
}


Wenn ich das kompilieren will erhalte ich folgende Meldung:


main.cc: In function ‘int main()’:
main.cc:9: error: request for member ‘length’ in ‘test’, which is of non-class type ‘SWP0607::MyVector3Df ()()’
make: *** [main.o] Fehler 1

peschmae
06-12-2006, 22:23
Ich krieg auch die gleiche Fehlermeldung - sehe wirklich nicht wo die herkommt. Wenn ich das Ding auf den Heap anlege gehts :confused:

MfG Peschmä

KravenZ
06-12-2006, 22:53
Ich hab jetzt einfach mal die ganzen inlines vor den Methoden weggemacht und dann läuft es super :)

peschmae
07-12-2006, 09:30
Achso, genau. Ich glaube bei Inline solltest du auch gleich die Funktionsdefinition mit ins Header nehmen - sonst kann der Compiler ja gar nicht inlinen, wenn du in main.cpp das Header includest, weil er da ja gar nicht weiss, wie die Funktion genau aussieht. Ist aber schon ne Weile her dass ich das benutzt habe - bitte korrigieren wenn ich Blödsinn erzähle ;)

MfG Peschmä

RHBaum
07-12-2006, 10:59
sonst kann der Compiler ja gar nicht inlinen, wenn du in main.cpp das Header includest
komischer weisse geht das bei dem MS Zeugs ... ^^
nen normaler compiler sollt damit eigentlich nicht ins straucheln kommen, er muesst versuchen ob er den code findet (in der cpp datei) nen binaerblock generieren und an der stelle wo es verwendet wird einfuegen.
Was aber ned geht wenn man die cpp's seperat uebersetzt ^^
dann sollt er aber das inline komplett ignorieren ...

Um welchen compiler in welcher version handelt es sich hier ?

Ciao ...

peschmae
07-12-2006, 17:49
g++ 4.1.2 bei mir. Wobei ich das im konkreten Fall nicht mal separat kompiliert habe (zumindest die Anweisung war für in einem Schnauf - was er dann damit macht weiss ich nicht ;))

MfG Peschmä