NEED ASSISTANCE WITH ERRORS

Write your question here.

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
// ImageComponents
#include <iostream>
#include "Position.h"
#include "ArrayQueue.h"

using namespace std;

void labelComponents(int size, int **pixel);
void outputImage(int size, int **pixel);

int main(){

	int ** pixel;
	int size = 0;
	bool comp = 0;

	cout << "Enter image size: ";
	cin >> size;

	pixel = new int *[size + 2];
	for (int i = 1; i <= size; i++)
		pixel[i] = new int[size + 2];

	cout << "Enter the pixel array in row-major order: ";
	for (int i = 1; i <= size; i++)
		for (int j = 1; j <= size; j++){
			cin >> comp;
			pixel[i][j] = comp;
		}

	labelComponents(size, pixel);
	outputImage(size, pixel);

	system("pause");
	return (0);
}


void labelComponents(int size, int **pixel){
	// initialize offsets
	Position * offset = new Position[4];
	offset[0] = Position(0, 1);   // right
	offset[1] = Position(1, 0);   // down
	offset[2] = Position(0, -1);  // left
	offset[3] = Position(-1, 0);  // up


	int numNbr = 4; // neighbors of a pixel position
	ArrayQueue<int> * Q = new ArrayQueue<int>();
	Position * nbr = new Position(0, 0);
	int id = 1;  // component id

	// scan all pixels labeling components
	for (int r = 1; r <= size; r++)      // row r of image
		for (int c = 1; c <= size; c++)   // column c of image
		{
			if (pixel[r][c] == 1)
			{// new component
				pixel[r][c] = ++id; // get next id
				Position * here = new Position(r, c);

				do
				{// find rest of component
					for (int i = 0; i < numNbr; i++)
					{// check all neighbors of here
						int perro = here->getRow() + offset[i].getRow();
						nbr->setRow(perro);


						int gato = here->getCol() + offset[i].getCol();
						nbr->setCol(gato);

						if (pixel[nbr->getRow()][nbr->getCol()] == 1)
						{// pixel is part of current component
							pixel[nbr->getRow()][nbr->getCol()] = id;
							Q->enqueue(new Position(nbr->getRow(), nbr->getCol()));
						}
					}
					// any unexplored pixels in component?
					here = (Position)Q->dequeue(); // a component pixel
				} while (here != NULL);


				/*

					do
				{// find rest of component
					for (int i = 0; i < numNbr; i++)
					{// check all neighbors of here
						nbr->getRow() = here.getRow() + offset[i].getRow();
						nbr.getCol() = here.getCol() + offset[i].getCol();
						if (pixel[nbr.getRow()][nbr.getCol()] == 1)
						{// pixel is part of current component
							pixel[nbr.getRow()][nbr.getCol()] = id;
							Q.enqueue(new Position(nbr.getRow(), nbr.getCol()));
						}
					}
					// any unexplored pixels in component?
					here = (Position)Q.dequeue(); // a component pixel
				} while (here != NULL);
				
				
				
				*/




			} // end of if, for c, and for r
		}
} // end of labelComponents

void outputImage(int size, int **pixel){
	cout << "The labeled image is: ";
	for (int i = 1; i <= size; i++)
	{
		cout << endl;

		for (int j = 1; j <= size; j++)
			cout << pixel[i][j] << ' ';
	}
	cout << endl;
} // end of outputImage 


Error here: red mark below the Q here = (Position)Q->dequeue(); // a component pixel
ERROR DESCRIPTION :
ArrayQueue<int> *Q
Error : no suitable constructor exist to convert from "int" to "Position"

The other error here : red mark below the new:
Q->enqueue(new Position(nbr->getRow(), nbr->getCol()));
ERROR DESCRIPTION :
Error: Argument of type "Position*" is incompatible with parameter of type "int&"
Last edited on
Where is your Position.h" and "ArrayQueue.h" files? From your question the problem may be in your classes.

no suitable constructor exist
Last edited on
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
#ifndef POSITION_H
#define POSITION_H

class Position 
{
private:
	
	int row;   // row number of the position
	int col;
	// column number of the position
public:
	Position(); // default
	Position( int theRow, int theCol); // parameter
	Position(const Position & aPosition); // copy
	Position & operator = (const Position & aPosition); // overload =
	// mutators
	void setRow (int r);
	void setCol (int c);
	//accessors
	int getRow() const;
	int getCol() const;
}; // end Position

Position::Position()
{
	setRow(0);
	setCol(0);
}

Position::Position(int r, int c)
{
	setRow(r);
	setCol(c);
}

Position::Position(const Position & aPosition)
{
	setRow(aPosition.row);
	setCol(aPosition.col);
}

Position & Position::operator=(const Position & aPosition)
{
	this->row=aPosition.row;
	this->col=aPosition.col;
	return(*this);
}

void Position::setRow(int r)
{
	this->row = r;
}
void Position::setCol(int c)
{
	this->col = c;
}
int Position::getRow() const
{
	return this->row;
}
int Position::getCol() const
{
	return this->col;
}
#endif 
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
 #ifndef ARRAYQUEUE_H
#define ARRAYQUEUE_H

#include "QueueInterface.h"
#include "PrecondViolatedExcep.h"
#include"Position.h"

const int MAX_QUEUE = 50;

template<class ItemType>
class ArrayQueue : public QueueInterface<ItemType>
{
private:
   ItemType items[MAX_QUEUE]; // Array of queue items
   int front;                 // Index to front of queue
   int back;                  // Index to back of queue
   int count;                 // Number of items currently in the queue
public:
   ArrayQueue();
   // Copy constructor and destructor supplied by compiler
   bool isEmpty() const;
   ItemType enqueue(ItemType& newEntry);
   ItemType dequeue();
}; // end ArrayQueue
template<class ItemType>
ArrayQueue<ItemType>::ArrayQueue() : front(0), back(MAX_QUEUE - 1), count(0){} // end default constructor

template<class ItemType>
bool ArrayQueue<ItemType>::isEmpty() const
{
   return count == 0;
} // end isEmpty

template<class ItemType>
ItemType ArrayQueue<ItemType>::enqueue(ItemType & newEntry)
{
   bool result = false;
   if (count < MAX_QUEUE)
   {
      // Queue has room for another item
      back = (back + 1) % MAX_QUEUE;
      items[back] = newEntry;
      count++;
      result = true;
   } // end if
   return result;
} // end enqueue
template<class ItemType>
ItemType ArrayQueue<ItemType>::dequeue()
{
   bool result = false;
   if (!isEmpty())
   {
      front = (front + 1) % MAX_QUEUE;
      count--;
      result = true;
   } // end if
   return front;
} // end dequeue
// End of implementation file.
#endif
 
You've made Q templated on int, so Q->dequeue() is going to return an int.

You're trying to cast that int to a Position:

(Position)Q->dequeue();

but you've provided no way for the compiler to know how to do that.

What does it mean to cast an int to a Position?

Topic archived. No new replies allowed.