problem in assignment operator, with class array

hello,

i´ve been having this problem with a assignment operator in a class, and i´ve been searching around and could not find a strait answer...maybe someone can help me out, and explain the reason why this problem exists....

this is the code of the 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
25
26
class STriple
{

public:

	double x, y, z;
	
	STriple(void);
	STriple(double x, double y, double z);
	~STriple(void);

	STriple & operator=(const STriple &rhs);
};

// the operator code
STriple & STriple::operator=(const STriple &other)
{
	if(this != &other){
		
		this->x = other.x;
		this->y = other.y;
		this->z = other.z;
	}

	return *this; 	
}


now the problem is that, any time i tried to do this :
1
2
3
4
5
6
7
// the array..
STriple *vertexData = new STriple[divs + 1];
for (int i = 0; i < divs; i++) {
           
    vertexData[i] = new STriple(0, 0, 0);
}


i get the error :

error C2679: binary '=' : no operator found which takes a right-hand operand of type 'STriple *' (or there is no acceptable conversion)

could be 'STriple &STriple::operator =(const STriple &)'


can anyone help me with this? i don´t know why this is happening...




Try vertexData[i] = STriple(0, 0, 0);
ALRIGHT IT´S WORKING!!!!! =)

is it to much to ask why i can´t do what i was trying to do ? i would like to understand :)

many thanks 4 your time and answer
Last edited on
Operator new allocates space for a new STriple and returns its address, which is of type STriple * (STriple pointer). vertexData is a dynamic array of STriples, not STriple pointers. Your compiler complained because you tried to assign a STriple pointer to a STriple object. If you remove the new keyword from that line, what happens is that a temporary, unnamed STriple object is created, used as an argument for your assignment operator, and then destroyed.
Last edited on
yes, that makes sence, i understand... now i can continue with my work.

many thanks !!!! ^^
Topic archived. No new replies allowed.