![]() |
runtime error
Can somebody tell me why this code does not work and how to fix it? It compiles but runtime errors appear:
Code:
#include <iostream> |
Re: runtime error
What's it meant to do?
What output do you expect from this code? (In particular, why do you expect THREE values, and what are they?) What are the errors? |
Re: runtime error
the code above is just a portion of another code that does some operations with vectors. At the beggining I created it as a child of std::vector, but i read that this is not a good idea, so i decided to do a wrapper of std::vector. The ouput to identify the error is just what i have in main(). The error is becuase "d" is not being compute, so d(0), d(1) and d(2). The problem is fixed when i remove the copy constructor, so the problem is there. I don't see why it is wrong? can anyone tell me how to rewrite the copy constructor? in principle, i don't need it since the default created by the compiler will be ok. But for generality i would like to know how to do it.
thanks. |
Re: runtime error
Are you sure the copy constructor is being invoked? For example have you tried putting a printf or a cout into it?
TYPE var=<expr>; can sometimes invoke the copy constructor or operator=(), hence the question. |
Re: runtime error
Yes, I tried. It is being invoked.
|
Re: runtime error
The correct constructor is:
Code:
template<class T> Code:
template<class T> Also, is there any way I can do the following in the main() function: Code:
int main() |
Re: runtime error
I still don't see what numbers you're expecting this to output. I can guess that d(0) will be 6 and d(1) will be 8, but what do you think d(2) will be?
|
Re: runtime error
For 2*x to work you need operator*(int,Vec).
|
Re: runtime error
No, d(0)=d(1)=d(2) = 8. There is an easy way to solve all posibilities no matter if it is integer or double. I found it today in the morning:
Code:
template<class T, class U> |
| All times are GMT +5.5. The time now is 14:47. |