Here’s my header file Normal.h:
#ifndef NORMAL_H
#define NORMAL_H
#include "Vector3.h"
class Normal
{
public:
Vector3 pos;
Vector3 direction;
Normal(Vector3, Vector3);
};
#endif
Here’s the cpp file Normal.cpp:
#include "Normal.h"
#include "Vector3.h"
Normal::Normal(Vector3 pos, Vector3 direction)
{
this->pos = pos;
this->direction = direction;
}
They are referencing a Vector3 class which does not have a constructor that takes no arguments. The only constructor specified takes 3 ints.
But I get an error when I try to run a test:
g++ Normal.cpp -o NormalTest.cpp
/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
/tmp/ccxgbatN.o: In function `Normal::Normal(Vector3, Vector3)':
Normal.cpp:(.text+0xd): undefined reference to `Vector3::Vector3()'
I don’t understand the error that says I have an undefined reference to ‘Vector3::Vector3()’ It looks like it’s referring to this line: Normal::Normal(Vector3 pos, Vector3 direction)
I’m not fluent in C++, so any help would be appreciated.
This is not an uncommon C++ issue.
To fix it try
The reason is that the way you defined your constructor, C++ says:
But what are the default values of
posanddirection? They are found by calling the default constructor ofVector3… but none is defined! Hence the error.The alternative version of the constructor, using initializers instead of assignment, works as follows:
Assuming you have a copy constructor for
Vector3, you should be okay.In general writing constructors with initializers instead of assignment statements is a good idea, for this very reason.