-
Class problem
Hello i have come here to this forum to ask for help as i have run into a problem while writing a program.
I have a class who is supposed to update the simulation with a declaration of two instances of a data type class called vektor. When the linker arrives to the update class's constructor, GCC give the following error messages:
undefined reference to `vektor::vektor()'
undefined reference to `vektor::vektor()'
undefined reference to `vektor::vektor()'
undefined reference to `vektor::vektor()'
undefined reference to `vektor::vektor()'
What is wrong?
If i comment out "vektor ForceNy;" and "vektor ForceNy;" in UpdateSim and all references to them in the update function it compiles.
Here are the relevant parts of UpdateSim and vektor.
Code:
class UpdateSim
{
public:
UpdateSim();
~UpdateSim();
int update();
__int64 SimTime; //How long time has the simulation simulated
private:
//Temp variabler till updaterings functionen
vektor ForceNy; //The newly calculated force, is going to be added to Fres of the curent object
vektor ForceNy; //Temporary variable distance used in calculations
long double G; //The gravitational constant.
unsigned int Tids_steg; //Hur lång tid går det för varje uppdatering i sekunder
};
UpdateSim::UpdateSim()
{
//Initialize Simulation variabels
G=1064407456253852709838460.8053915;
Tids_steg=3600;
SimTime=0;
}
Code:
class vektor
{
public:
//Defult Constructor
vektor();
//Constructor from 4 cordinats
vektor(long double sX, long double sY, long double eX, long double eY);
//Constructor from 2 cordinats a magnitude and a heading
vektor(long double sX, long double sY, long double Mag, float Hed);
//Destructorn
~vektor();
//Negering
vektor& operator-();
//Tilldelning
vektor operator=(vektor const& envek);
//Jämförelser
bool operator!=(vektor const& envek);
bool operator==(vektor const& envek);
bool operator<(vektor const& envek);
bool operator>(vektor const& envek);
bool operator<=(vektor const& envek);
bool operator>=(vektor const& envek);
//Operatorer
vektor operator+(vektor const& envek);
vektor operator-(vektor const& envek);
vektor operator*(long double const& tal);
vektor operator/(long double const& tal);
// vektor operator%(long double const& tal);
//Return funktioner
long double retStartX() { return startX; };
long double retStartY() { return startY; };
long double retEndX() { return endX; };
long double retEndY() { return endY; };
long double retMag() { return Magnetude; };
float retHed() { return Heading; };
private:
long double startX, startY, endX, endY, Magnetude;
float Heading;
void updateHedOMag();
void updateeXOeY();
void fixavinkel();
};
You have to excuse that i comment my code with a mix of Swedish and English.
-
It may be something relatively simple, like: since UpdateSim class contains a vektor object, the vektor class needs to be declared before the UpdateSim class, OR you need to have a forward declaration telling the compiler/linker that the vektor class is really there, even if it hasn't been seen already.
-
You need to implement the default constructor. Did you implement any of the vektor functions in a source file? If yes, then you might have forgotten the default constructor, or you might not be compiling that source file, or you might not be linking to the compiled object file. If no, then you need to implement those functions to get it to link.
-
Thanks i got it. I had forgotten to declare the default constructor.
-
Good. By the way, technically speaking, you didn't forget to declare the default constructor (it's declared in the header you posted), you just forgot to define it.