C++ compilation error(Underfined ref)

Hi, I am trying to get my member function "virtual Factory& getFactory() const" in class RenderedPoint to work but it doesn't. I am receiving an undefined reference below. Can anyone point out to me the mistake that I am making? Thanks!

Compilation output:
/tmp/ccronZVA.o:factory.cpp:(.rdata$.refptr._ZN13RenderedPoint10_myFactoryE[.refptr._ZN13RenderedPoint10_myFactoryE]+0x0): undefined reference to `RenderedPoint::_myFactory'
collect2: error: ld returned 1 exit status

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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
Header file:
#include <Windows.h>
#include <cstdint>

class Factory final
{
private:
	Factory() = delete;
	Factory(const Factory&) = delete;
	Factory(Factory&&) = delete;
	Factory& operator=(const Factory&) = delete;
	Factory& operator=(Factory&&) = delete;
 
public:
	typedef Object::VectorType VectorType;
	typedef Object::PointType PointType;
	typedef IRenderingInformation RenderingInformation;
	typedef uint8_t Byte;

	Factory(unsigned int width, unsigned int height, const VectorType& extents);
	~Factory();

	Object* createPoint(const PointType& center,
		unsigned color, float depth = 1);

	void writeToBitmap(const char* name);

	static char* readFromBitmap(unsigned int& width, unsigned int& height,
		const char* name);

	static unsigned getColor(Byte r, Byte g, Byte b);

	POINT toDevice(const VectorType& v) const;
	HDC getContext() const;

private:
	HBITMAP _bitmap;
	HDC _context;
	unsigned int _width;
	unsigned int _height;
	void* _bitmapData;
	float _xFactor;
	float _yFactor;
};

Main file:
namespace
{
	const unsigned char BYTES_PER_PIXEL = 3;
}

class RenderedPoint : public Point, public IRenderingInformation
{
public:
	void render() const override;
	
	virtual Factory& getFactory() const
	{
		// static Factory _myFactory;
		return _myFactory;
		
	}	
	virtual unsigned int getColor() const 
	{
		return color;
	}
	RenderedPoint(const PointType& center, unsigned c,
	float d) : Point(center,d),color(c) {}
	
	RenderedPoint* clone() const { return new RenderedPoint(*this); }
private:
	static Factory _myFactory;
	int color;
 };

Factory::Factory(
	unsigned int width,
	unsigned int height,
	const VectorType& extents
) :
	_width{width},
	_height{height},
	_xFactor{static_cast<float>(width) / extents.x()},
	_yFactor{static_cast<float>(height) / extents.y()}
{
	_context = CreateCompatibleDC(0);
	BITMAPINFO bitmapInfo;
	ZeroMemory(&bitmapInfo, sizeof(bitmapInfo));
	bitmapInfo.bmiHeader.biSize = sizeof(bitmapInfo);
	bitmapInfo.bmiHeader.biWidth = width;
	bitmapInfo.bmiHeader.biHeight = height;
	bitmapInfo.bmiHeader.biPlanes = 1;
	bitmapInfo.bmiHeader.biBitCount = 24;
	bitmapInfo.bmiHeader.biCompression = BI_RGB;
	_bitmap = CreateDIBSection(_context, &bitmapInfo, DIB_RGB_COLORS,
		&_bitmapData, 0, 0
	);
	SelectObject(_context, _bitmap);
}

Factory::~Factory()
{
	HGDIOBJ old = SelectObject(_context, GetStockObject(NULL_BRUSH));
	DeleteObject(old);
	DeleteDC(_context);
	DeleteObject(_bitmap);
}

HDC Factory::getContext() const
{
	return _context;
}

unsigned Factory::getColor(Byte r, Byte g, Byte b)
{
	return RGB(r, g, b);
}

void Factory::writeToBitmap(const char* name)
{
	const unsigned int rowData = BYTES_PER_PIXEL * _width;
	const unsigned int rowPadding = (rowData % 4 != 0)
		? (4 - rowData % 4)
		: 0;
	const unsigned int stride = rowData + rowPadding;
	std::fstream out{name, std::ios_base::binary | std::ios_base::out};
	if (out)
	{
		BITMAPFILEHEADER bmfh;
		BITMAPINFOHEADER bmih;

		ZeroMemory(&bmfh, sizeof(bmfh));
		bmfh.bfType = 'B' + ('M' << 8);
		bmfh.bfSize = static_cast<unsigned int>(
			sizeof(bmfh) +
			sizeof(bmih) +
			stride * _height
		);
		bmfh.bfOffBits = sizeof(bmfh) + sizeof(bmih);
		out.write(reinterpret_cast<char*>(&bmfh), sizeof(bmfh));

		ZeroMemory(&bmih, sizeof(bmih));
		bmih.biSize = sizeof(bmih);
		bmih.biWidth = _width;
		bmih.biHeight = _height;
		bmih.biPlanes = 1;
		bmih.biBitCount = 8 * BYTES_PER_PIXEL;
		bmih.biCompression = BI_RGB;
		out.write(reinterpret_cast<char*>(&bmih), sizeof(bmih));

		for (unsigned int h = 0; h < _height; ++h)
		{
			out.write(
				reinterpret_cast<char*>(_bitmapData) + h * stride,
				rowData
			);
			if (stride > rowData)
			{
				out.write("000", stride - rowData);
			}
		}
	}
}

char* Factory::readFromBitmap(unsigned int& width, unsigned int& height,
	const char* name
)
{
	std::fstream in{name, std::ios_base::in | std::ios_base::binary};
	if (in)
	{
		char header[54];
		in.read(header, 54);
		width = *reinterpret_cast<unsigned int*>(header + 18);
		const int signedHeight = *reinterpret_cast<int*>(header + 22);
		height = static_cast<unsigned int>(std::abs(signedHeight));
		const unsigned int size = *reinterpret_cast<unsigned int*>(header + 34);
		const unsigned int stride = size / height;
		const unsigned int rowData = BYTES_PER_PIXEL * width;
		char* const data = new char[rowData * height];
		for (unsigned int i = 0; i < height; ++i)
		{
			in.read(data + rowData * i, rowData);
			if (stride > rowData)
			{
				char buffer[4];
				in.read(buffer, stride - rowData);
			}
		}
		return data;
	}
	else
	{
		return nullptr;
	}
}

Object* Factory::createPoint(const PointType& center, unsigned color,
	float depth
)
{
	return new RenderedPoint(center, color, depth);
}

POINT Factory::toDevice(const VectorType& v) const
{
	return POINT{
		static_cast<int>(_xFactor * v.x() + 0.5f),
		static_cast<int>(static_cast<float>(_height) - _yFactor * v.y() + 0.5f)
	};
}

void RenderedPoint::render() const
{
	POINT p = getFactory().toDevice(center());
	SetPixel(getFactory().getContext(), p.x, p.y, getColor());
}
You've declared a static Factory member, _myFactory.

Before I get to why that's a problem, I want to point out that it is a mistake to use leading underscores in your classes. That's reserved "for the implementation" - that's not your implementation, but that of the compiler's.

No application code should use leading underscores, at all, in C++ or C.

That said, while you declared the member, it is not defined. It doesn't actually exist.

You must define in elsewhere in a CPP file to give it "existence". Something like

Factor Factory::_myFactory(...);

The elipses are the parameters required for the constructor, which would have to be constants available at compile time.

The static definition is otherwise similar to the definition of a global variable.


Last edited on
it is a mistake to use leading underscores in your classes. That's reserved "for the implementation" - that's not your implementation, but that of the compiler's.

No application code should use leading underscores, at all, in C++ or C.

This is inaccurate. The names that are reserved for the C++ implementation are:

- identifiers beginning with an underscore followed immediately by an uppercase letter
- identifiers containing adjacent underscores (or "double underscore")
- identifiers beginning with an underscore in the global namespace

The OP's underscore prefix for data members violates none of these rules, and is fine. While it's not the most popular convention for indicating data members, it's one I've seen in several places before.
Experience shows that no one ever bothers to check that list.

Once they start the habit of using the underscore, they use it everywhere in ways that violate these rules.

Without consequence from the compiler, they never realize it's wrong.

The only working policy I've seen in decades of consulting is to refuse all use of the leading underscore. That, everyone can remember.

This list, they never will.
Experience shows that no one ever bothers to check that list.

Your experience does not match my experience.

In any case, it's one thing to advise people not to do it (which I don't have a problem with, and would certainly agree with as a stylistic preference ;) ), but it's quite another to misleadingly express it as a rule of the language.

Especially not in the Beginners forum, where readers are less likely to have the knowledge and experience to know what the real rules are.
Last edited on
Topic archived. No new replies allowed.