Problem with assignment overloading

Hi everyone,

I'm having a bit of an issue here concerning operator overloading - especially the assignment (=) operator.

I'm trying this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
template<typename TYPE>
struct vectorD
{
TYPE X;
TYPE Y;
TYPE Z;

vectorD& operator= (TYPE right){
X = right;
Y = right;
Z = right;
return *this;
}
}


And later

 
vectorD<int> test = 1;


This attempt gives me the following error:

 
 error: conversion from ‘int’ to non-scalar type ‘vectorD<int>’ requested

My question is: WHY? Shouldn't it work? What am I doing wrong? And finally...is it possible to do the "vector = scalar" assignment at all?

Best,
ZED
closed account (zb0S216C)
Believe it or not, you're calling a constructor that isn't there. You'll have to do one of the following:

1) Define a constructor:

1
2
3
4
5
template < typename TYPE >
struct vectorD
{
    vectorD( TYPE Type );
};

2) Perform the assignment after declaring an instance of vectorD:

1
2
vectorD < int > Vector;
Vector = 1;

Wazzak

Last edited on
Thanks!

ZED
Topic archived. No new replies allowed.