Luke
11-22-2005, 10:52 AM
Hi everyone,
I want to use the +operator with vectors in my program. Therefore, I am trying to add an overloaded operator+ to the NRVec class, by adding the following to the class declaration:
NRVec & operator+(const NRVec &rhs)
Outside the class declaration I write how C++ should interpret use of the + operator with two NRVec's:
template <class T>
NRVec<T> & NRVec<T> :: operator+(const vec &rhs)
{
NRVec<T> result(rhs.size());
for(int i=0;i<rhs.size();i++)
result[i]=v[i]+rhs[i];
return result;
}
However, this does not work; With this code I return an address to a temporary variable "result". I also get a warning from the compiler (VC++ 6).
I have tried declaring a private pointer to a block of dynamic memory in the class declaration, i.e.
T *result;
This does not work either, because upon "return result" it can not assign a block of dynamic memory to an NRVec. Anyone got any ideas on how to solve such a problem? I am sure that anyone who has written their own matrix/vector classes have dealt with this problem...
Thanks,
Luke
A cool quote I found, thought up by and for programmers:
If I asked you to go out with me, would you give me the same answer as the answer to this question?
I want to use the +operator with vectors in my program. Therefore, I am trying to add an overloaded operator+ to the NRVec class, by adding the following to the class declaration:
NRVec & operator+(const NRVec &rhs)
Outside the class declaration I write how C++ should interpret use of the + operator with two NRVec's:
template <class T>
NRVec<T> & NRVec<T> :: operator+(const vec &rhs)
{
NRVec<T> result(rhs.size());
for(int i=0;i<rhs.size();i++)
result[i]=v[i]+rhs[i];
return result;
}
However, this does not work; With this code I return an address to a temporary variable "result". I also get a warning from the compiler (VC++ 6).
I have tried declaring a private pointer to a block of dynamic memory in the class declaration, i.e.
T *result;
This does not work either, because upon "return result" it can not assign a block of dynamic memory to an NRVec. Anyone got any ideas on how to solve such a problem? I am sure that anyone who has written their own matrix/vector classes have dealt with this problem...
Thanks,
Luke
A cool quote I found, thought up by and for programmers:
If I asked you to go out with me, would you give me the same answer as the answer to this question?