Undefined reference to class::class

Hello all. I have a basic understanding of c++. I am doing some learning by following along on some of the classes on MIT OCW, particularly Computer Graphics. Working on Assignment 0 where I inherit a bunch of header code and have to create main.cpp. I inherit several vector class files, which are complete, but am struggling to instantiate an object of the vector class


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <cmath>
#include <iostream>
#include <sstream>
#include <vector>
#include <Matrix2f.h>
#include <Matrix3f.h>
#include <Matrix4f.h>
#include <Quat4f.h>
#include <Vector2f.h>
#include <Vector3f.h>
#include <Vector4f.h>
using namespace std;

int main()
{
    vector<Vector3f> ThisWorks;

    float x = 0.1;
    float y = 0.2;
    float z = 0.3;

    Vector3f V(x,y,z);
}


The Vector3f class looks like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#ifndef VECTOR_3F_H
#define VECTOR_3F_H

class Vector2f;

class Vector3f
{
public:

	static const Vector3f ZERO;
	static const Vector3f UP;
	static const Vector3f RIGHT;
	static const Vector3f FORWARD;

    Vector3f( float f = 0.f );
    Vector3f( float x, float y, float z );

	Vector3f( const Vector2f& xy, float z );
	Vector3f( float x, const Vector2f& yz );

	// copy constructors
    Vector3f( const Vector3f& rv );

	// assignment operators
    Vector3f& operator = ( const Vector3f& rv );

	// no destructor necessary

	// returns the ith element
    const float& operator [] ( int i ) const;
    float& operator [] ( int i );

    float& x();
	float& y();
	float& z();

	float x() const;
	float y() const;
	float z() const;

	Vector2f xy() const;
	Vector2f xz() const;
	Vector2f yz() const;

	Vector3f xyz() const;
	Vector3f yzx() const;
	Vector3f zxy() const;

	float abs() const;
    float absSquared() const;

	void normalize();
	Vector3f normalized() const;

	Vector2f homogenized() const;

	void negate();

	// ---- Utility ----
    operator const float* () const; // automatic type conversion for OpenGL
    operator float* (); // automatic type conversion for OpenGL
	void print() const;

	Vector3f& operator += ( const Vector3f& v );
	Vector3f& operator -= ( const Vector3f& v );
    Vector3f& operator *= ( float f );

    static float dot( const Vector3f& v0, const Vector3f& v1 );
	static Vector3f cross( const Vector3f& v0, const Vector3f& v1 );

    // computes the linear interpolation between v0 and v1 by alpha \in [0,1]
	// returns v0 * ( 1 - alpha ) * v1 * alpha
	static Vector3f lerp( const Vector3f& v0, const Vector3f& v1, float alpha );

	// computes the cubic catmull-rom interpolation between p0, p1, p2, p3
    // by t \in [0,1].  Guarantees that at t = 0, the result is p0 and
    // at p1, the result is p2.
	static Vector3f cubicInterpolate( const Vector3f& p0, const Vector3f& p1, const Vector3f& p2, const Vector3f& p3, float t );

private:

	float m_elements[ 3 ];

};

// component-wise operators
Vector3f operator + ( const Vector3f& v0, const Vector3f& v1 );
Vector3f operator - ( const Vector3f& v0, const Vector3f& v1 );
Vector3f operator * ( const Vector3f& v0, const Vector3f& v1 );
Vector3f operator / ( const Vector3f& v0, const Vector3f& v1 );

// unary negation
Vector3f operator - ( const Vector3f& v );

// multiply and divide by scalar
Vector3f operator * ( float f, const Vector3f& v );
Vector3f operator * ( const Vector3f& v, float f );
Vector3f operator / ( const Vector3f& v, float f );

bool operator == ( const Vector3f& v0, const Vector3f& v1 );
bool operator != ( const Vector3f& v0, const Vector3f& v1 );

#endif // VECTOR_3F_H


My understanding is that there are multiple constructors for this class, one of them being:
 
Vector3f( float x, float y, float z );


I've seen answers to similar questions that say that the cause is that you did not link the header files properly, or you did not use the proper implementation. I'm not sure how Vector3f V(0.1, 0.2, 0.3) is not a proper implementation. I have added the location of the header file to the search directories in Code::blocks, so I don't believe that is the problem.

Here is the compiler error:
D:/Coding/CG/Assignment0/TestVec/main.cpp:22: undefined reference to `Vector3f::Vector3f(float, float, float)'

Note that the compiler does not throw an error at the following line
 
vector<Vector3f> ThisWorks;


I don't fully understand that yet either. A necessary part of the assignment is to create a vector of Vector3f's

Any advice would be appreciated! Sorry in advance if I'm missing something obvious and basic.
Last edited on
Here is the compiler error:
D:/Coding/CG/Assignment0/TestVec/main.cpp:22: undefined reference to `Vector3f::Vector3f(float, float, float)'

Actually that is not compile error, it is a linker error. This error is telling you that the linker can't find the implementation of that function. Did you write the code for that function and did you add that code to your project?
That is not a compiler error. It is a linker error.
It is telling you that you are trying to call Vector3f::Vector3f(float, float, float), but that you have no implementation for that function.

Since you are provided the header files, for vector3f, presumably you are also provided the object files for that library. You need to tell the code:blocks linker where to find the Vector3f library file (object, not header).
Thank you both. You're absolutely correct. When organizing my project folder I had misplaced a set of very important files (the .cpp implementations for those header files). Appreciate your kind responses to such an amateur error.
Topic archived. No new replies allowed.