I’m getting a compile error
“conversion from ‘int*’ to non-scalar type ‘foo< int>’ requested”
What am i doing wrong?
template <typename T>
struct foo {
T *ptr;
foo(void){}
foo<T>& operator =(const T &point) {
if (ptr != &point) {
ptr = &point;
}
return *this;
}
T& operator*() {
return *ptr;
}
}
int main(){
int x;
foo<int> f = &x; //error here
*f = 0;
printf("%d\n", *f)
}
The problem on the line indicated is that you have type mismatch between the variable
fand its initializer:The variable has type
foo<int>while the initializer is of typeint*andfoo<int>doesn’t have a constructor taking anint*as argument. Note that the equal sign in variable definition indicates initialization, not assignment.Note that the code has more problems, though. Many of them are related to inconsistent use of the
ptridentifier (sometimes as a member variable, sometimes as a function, sometimes as a type), to invalid use of&and more type mismatch errors.