C++ compilation issue

Hi, I am receiving 2 kinds of errors from my compiler as shown below. One is for the 'random' that is redeclared and another is for operator-. Can anyone show me the ropes on what do I need to do for this error? NOTE: Because I am not allowed to modify the main() and class RandomFactory and Random permanently, I do not know what to do regarding the errors. Thanks!

error: ‘Random random’ redeclared as different kind of symbol
} random;
^~~~~~

In file included from vector2d.h:28:0,
from Object.h:26,
from renderer.h:1,
from render_test.cpp:7:
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/complex:374:5: note: candidate: template<class _Tp> std::complex<_Tp> std::operator-(const _Tp&, const std::complex<_Tp>&)
operator-(const _Tp& __x, const complex<_Tp>& __y)
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/complex:374:5: note: template argument deduction/substitution failed:
In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/algorithm:62:0,
from render_test.cpp:3:
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/bits/stl_algo.h:1969:22: note: ‘std::_List_iterator<Object*>’ is not derived from ‘const std::complex<_Tp>’
std::__lg(__last - __first) * 2,
~~~~~~~^~~~~~~~~
In file included from vector2d.h:28:0,
from Object.h:26,
from renderer.h:1,
from render_test.cpp:7:
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/complex:451:5: note: candidate: template<class _Tp> std::complex<_Tp> std::operator-(const std::complex<_Tp>&)
operator-(const complex<_Tp>& __x)
^~~~~~~~

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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
class vector2d : public std::complex<float>{
public:

    static const std::complex<float> EX;
    static const std::complex<float> EY;

    vector2d(float x = 0, float y = 0);

    vector2d(const std::complex<float>& copy);

    ~vector2d() = default;
   

    float operator*(const vector2d& lhs) const;
	
	float x() const;
    float y() const;
	
    vector2d& operator/(vector2d& d) = delete;
    vector2d& operator*=(vector2d& d) = delete;
    vector2d& operator/=(vector2d& d) = delete;
};
vector2d::vector2d(float x, float y) : std::complex<float>(x, y){ }
    
float vector2d::x() const{
    return real();
}

float vector2d::y() const{
    return imag();
}


float vector2d::operator*(const vector2d& lhs) const{
    float dotx, doty;
    dotx = real() * lhs.x();
    doty = imag() * lhs.y();
    return (dotx + doty);
}

const std::complex<float> vector2d::EX = std::complex<float>(1,0);
const std::complex<float> vector2d::EY = std::complex<float>(0,1);
class Object: public ICloneable{ 
public:
    Object(float depth = 0);
    
    virtual ~Object() = default;
    
	float depth() const;
	
    virtual void render() const;
	
    using PointType = vector2d;
    using VectorType = vector2d;
    virtual Object* clone() const{return new Object(*this);}
private:
    float _depth;  
    
};
Object::Object(float depth) : _depth(depth){ }


float Object::depth() const{
    return _depth;
}

void Object::render() const{ }
class Factory;

class IRenderingInformation
{
public:
	virtual Factory& getFactory() const = 0;
	virtual unsigned int getColor() const = 0;
	virtual ~IRenderingInformation() = default;
};

template<template<typename, typename> class C, typename T, typename Alloc>
void render(C<T, Alloc>& container)
{
	 
        std::sort(container.begin(), container.end());  
 
}
class ObjectDepthComparer
{
	Object* DepthComparerx;
	Object* DepthComparery;
   public:
     ObjectDepthComparer(Object* x, Object* y) : DepthComparerx(x),DepthComparery(y) {}
     bool operator()(Object* x, Object* y) { 
	 if(x<y)
	 {
		return true;
	 }
	 else{
		 return false;
	 }
	 }
	ObjectDepthComparer& operator-(const std::complex<float>& __x) const
   {
	 
   }
};
 
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);
	Object* createCircle(const PointType& center, float radius,
		unsigned color, float depth = 1);
	Object* createSquare(const PointType& center, const VectorType& half_edge,
		unsigned color, float depth = 1);
	Object* createPolygon(const PointType* vertices, int size,
		unsigned color, float depth = 1);
	Object* createBackground(const char* pixelData, unsigned int width,
		unsigned int height, float depth = 0);

	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;
};
 

class Random
{
public:
	Random() :
		A(32993),
		B(66047),
		_high(683),
		_low(1087)
	{
	}

	unsigned operator()()
	{
		_high = ((A * _high) % B) & 0xff;
		_low = ((A * _low) % B) & 0xff;
		return ((_high << 8) | _low);
	}

	unsigned operator()(int n)
	{
		return operator()() % n;
	}

	float operator()(float a, float b)
	{
		return a + (b - a)*float(operator()()) / float(1 << 16);
	}

private:
	const unsigned A;
	const unsigned B;
	unsigned _high;
	unsigned _low;
} random;


class RandomFactory
{
	Factory& _factory;

public:
	RandomFactory(Factory& factory) :
		_factory(factory)
	{
	}

	static Object::PointType getPoint()
	{
		return Object::PointType(random(0, 1), random(0, 1));
	}

	static Object::VectorType getVector()
	{
		return Object::VectorType(random(0, 0.25f), random(0, 0.25f));
	}

	Object* operator()()
	{
		const float depth = float(random(1000));
		const unsigned color = Factory::getColor(
			static_cast<Factory::Byte>(random(256)),
			static_cast<Factory::Byte>(random(256)),
			static_cast<Factory::Byte>(random(256))
		);
		switch (random(4))
		{
			case 0:
				return _factory.createPoint(getPoint(),
					color, depth);
			case 1:
				return _factory.createCircle(getPoint(), random(0, 0.25f),
					color, depth);
			case 2:
				return _factory.createSquare(getPoint(), getVector(),
					color, depth);
			default:
			{
				const int size = 1 + random(10);
				std::vector<Object::PointType> vertices;
				vertices.resize(size);
				for (int i = 0; i < size; ++i)
				{
					vertices[i] = getPoint();
				}
				return _factory.createPolygon(vertices.data(), size,
					color, depth);
			}
		}
	}
};

class Deleter
{
public:
	void operator()(Object* obj)
	{
		delete obj;
	}
};

int main()
{
	const int WIDTH = 502;
	const int HEIGHT = 502;
	Factory factory(WIDTH, HEIGHT, Object::VectorType(1, 1));
	std::vector<Object*> objects;
	std::list<Object*> objects2;

	// You must self-study the following STL functions:
	// * std::generate_n
	// * back_inserter
	std::generate_n(std::back_inserter(objects), 10, RandomFactory(factory));
	std::generate_n(std::back_inserter(objects2), 10, RandomFactory(factory));

	unsigned int width;
	unsigned int height;
	const char* const data = Factory::readFromBitmap(width, height, "cube.bmp");
	if (data != nullptr)
	{
		objects.push_back(factory.createBackground(data, width, height));
	}

	render(objects);
	std::for_each(objects.begin(), objects.end(), Deleter{});

	render(objects2);
	std::for_each(objects2.begin(), objects2.end(), Deleter{});

	factory.writeToBitmap("render_test.bmp");
	delete[] data;
}
Last edited on
Can you please provide definition of Object type, to be able to compile

and also IRenderingInformation which headers do I need for this simbol?
Last edited on
Hi Malibor, I just edited. Thanks. You may take a look now
Last edited on
Problem is in:
1
2
3
4
5
6
7
template<template<typename, typename> class C, typename T, typename Alloc>
void render(C<T, Alloc>& container)
{

	std::sort(container.begin(), container.end());

}

You can't use std::sort to sort std::list, because std::sort requires iterators to be random access, and std::list iterators are only bidirectional.

However, std::list has a member function sort that will sort it:

Replace following code in your provided main function with following code:

This will not work, so remove it!
render(objects2);

Use this instead:
objects2.sort();

edit:
You may wan't to perform additional work on your render class such as verifying if argument type is std::list by implementing is_list<T> template function, or do a template specialization for std::list
Last edited on
Thanks malibor, however, I am not allowed to tweak the main(). Are there any other ways?
But how do I get rid of the compilation errors above tho? Right now I can't move forward as the errors are holding me back.

I changed the render function to using this instead(Will this work?):

1
2
3
4
5
6
7
8
9
10
11
12
13
template<typename T>
void render(std::vector<T>& vec)
{
   sort(vec.begin(), vec.end()); //Do something with vector
}

template<typename T>
void render(std::list<T>& list)
{
    list.sort() ; //Do something with list
}

Last edited on
Yes this should work as far as sorting and compilation goes, and you don't need to touch the main() function.

but I can't tell you much more than that, your project seems to be larger than example code in your initial post so you might want to perform testing on your code.
Last edited on
Thanks malibor. But changing to this render functions did not help my compilation errors. I am still getting the same errors. Any ideas? Thanks!
It compiles just fine here, I'm using msvc compiler btw.

to debug you issue I think we'll need complete code. maybe sharing complete project on github?
It would be better if you make a copy of your code, strip it of everything that doesn't relate to the error message, but still have it be compileable [or still reproduce the error message if the issue is compilation], and then post your code here. Doing this could help narrow down the issue.
http://sscce.org/
Last edited on
Hi Malibor, mind sharing with me what you did with my code, how u put them together? So I can compare with mine? Thanks.
I just copied your code and added template overload and included headers, also commented out one line because undeclared symbol as you can see in the "NOTE" comment below:

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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include <vector>
#include <complex>
#include <Windows.h>
#include <algorithm>
#include <list>

class vector2d : public std::complex<float> {
public:

	static const std::complex<float> EX;
	static const std::complex<float> EY;

	vector2d(float x = 0, float y = 0);

	vector2d(const std::complex<float>& copy);

	~vector2d() = default;


	float operator*(const vector2d& lhs) const;

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

	vector2d& operator/(vector2d& d) = delete;
	vector2d& operator*=(vector2d& d) = delete;
	vector2d& operator/=(vector2d& d) = delete;
};
vector2d::vector2d(float x, float y) : std::complex<float>(x, y) { }

float vector2d::x() const {
	return real();
}

float vector2d::y() const {
	return imag();
}


float vector2d::operator*(const vector2d& lhs) const {
	float dotx, doty;
	dotx = real() * lhs.x();
	doty = imag() * lhs.y();
	return (dotx + doty);
}

const std::complex<float> vector2d::EX = std::complex<float>(1, 0);
const std::complex<float> vector2d::EY = std::complex<float>(0, 1);

// NOTE: I commented out following because symbol is undefined
class Object /*: public ICloneable*/ {
public:
	Object(float depth = 0);

	virtual ~Object() = default;

	float depth() const;

	virtual void render() const;

	using PointType = vector2d;
	using VectorType = vector2d;
	virtual Object* clone() const { return new Object(*this); }
private:
	float _depth;

};
Object::Object(float depth) : _depth(depth) { }


float Object::depth() const {
	return _depth;
}

void Object::render() const { }
class Factory;

class IRenderingInformation
{
public:
	virtual Factory& getFactory() const = 0;
	virtual unsigned int getColor() const = 0;
	virtual ~IRenderingInformation() = default;
};

template<template<typename, typename> class C, typename T, typename Alloc>
void render(C<T, Alloc>& container)
{

	std::sort(container.begin(), container.end());

}

template<typename T>
void render(std::list<T>& list)
{
	list.sort();
}

class ObjectDepthComparer
{
	Object* DepthComparerx;
	Object* DepthComparery;
public:
	ObjectDepthComparer(Object* x, Object* y) : DepthComparerx(x), DepthComparery(y) {}
	bool operator()(Object* x, Object* y) {
		if (x < y)
		{
			return true;
		}
		else {
			return false;
		}
	}
	ObjectDepthComparer& operator-(const std::complex<float>& __x) const
	{

	}
};

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);
	Object* createCircle(const PointType& center, float radius,
		unsigned color, float depth = 1);
	Object* createSquare(const PointType& center, const VectorType& half_edge,
		unsigned color, float depth = 1);
	Object* createPolygon(const PointType* vertices, int size,
		unsigned color, float depth = 1);
	Object* createBackground(const char* pixelData, unsigned int width,
		unsigned int height, float depth = 0);

	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;
};


class Random
{
public:
	Random() :
		A(32993),
		B(66047),
		_high(683),
		_low(1087)
	{
	}

	unsigned operator()()
	{
		_high = ((A * _high) % B) & 0xff;
		_low = ((A * _low) % B) & 0xff;
		return ((_high << 8) | _low);
	}

	unsigned operator()(int n)
	{
		return operator()() % n;
	}

	float operator()(float a, float b)
	{
		return a + (b - a) * float(operator()()) / float(1 << 16);
	}

private:
	const unsigned A;
	const unsigned B;
	unsigned _high;
	unsigned _low;
} random;


class RandomFactory
{
	Factory& _factory;

public:
	RandomFactory(Factory& factory) :
		_factory(factory)
	{
	}

	static Object::PointType getPoint()
	{
		return Object::PointType(random(0, 1), random(0, 1));
	}

	static Object::VectorType getVector()
	{
		return Object::VectorType(random(0, 0.25f), random(0, 0.25f));
	}

	Object* operator()()
	{
		const float depth = float(random(1000));
		const unsigned color = Factory::getColor(
			static_cast<Factory::Byte>(random(256)),
			static_cast<Factory::Byte>(random(256)),
			static_cast<Factory::Byte>(random(256))
		);
		switch (random(4))
		{
		case 0:
			return _factory.createPoint(getPoint(),
				color, depth);
		case 1:
			return _factory.createCircle(getPoint(), random(0, 0.25f),
				color, depth);
		case 2:
			return _factory.createSquare(getPoint(), getVector(),
				color, depth);
		default:
		{
			const int size = 1 + random(10);
			std::vector<Object::PointType> vertices;
			vertices.resize(size);
			for (int i = 0; i < size; ++i)
			{
				vertices[i] = getPoint();
			}
			return _factory.createPolygon(vertices.data(), size,
				color, depth);
		}
		}
	}
};

class Deleter
{
public:
	void operator()(Object* obj)
	{
		delete obj;
	}
};

int main()
{
	const int WIDTH = 502;
	const int HEIGHT = 502;
	Factory factory(WIDTH, HEIGHT, Object::VectorType(1, 1));
	std::vector<Object*> objects;
	std::list<Object*> objects2;

	// You must self-study the following STL functions:
	// * std::generate_n
	// * back_inserter
	std::generate_n(std::back_inserter(objects), 10, RandomFactory(factory));
	std::generate_n(std::back_inserter(objects2), 10, RandomFactory(factory));

	unsigned int width;
	unsigned int height;
	const char* const data = Factory::readFromBitmap(width, height, "cube.bmp");
	if (data != nullptr)
	{
		objects.push_back(factory.createBackground(data, width, height));
	}

	render(objects);
	std::for_each(objects.begin(), objects.end(), Deleter{});

	//objects2.sort();
	render(objects2);
	std::for_each(objects2.begin(), objects2.end(), Deleter{});

	factory.writeToBitmap("render_test.bmp");
	delete[] data;
}

Topic archived. No new replies allowed.