I'm reading Programming Principles and Practice Using C++ first edition first printing book. This book is really hard for beginners but I have studied it until chapter 19 and should finish it!
I have a problem with a drill in chapter 19. Please look at it in the link below. http://uploads.im/S0mun.png
I have written the following code for questions number 1 through 13 of that drill but I have problems with the question number 14 of it.
There is no guide/sample on/of it until this section of the book! So I don't know how to solve it. :(
The program runs successfully until write_val(s1);but facingwrite_val(s5); I get this error:
Error 1 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const Vector<T>' (or there is no acceptable conversion) c:\users\me\documents\visual studio 2012\projects\test1\test1\test1.cpp 39
Please don't use friend feature. I haven't learnt it and likely I will learn it in next chapters of the book. :)
Thanks in advance.
As the error says, there is no version of operator<< which knows what to do with a vector<T>. Question 14 suggests that you define it (not for you to define it for const S<vector<T>>, although that would work with a different definition.)
As the error says, there is no version of operator<< which knows what to do with a vector<T>. Question 14 suggests that you define it (not for you to define it for const S<vector<T>>, although that would work with a different definition.)
Now at this stage, some other new question comes up in my mind! (I saw somewhere that one was reading a whole book on how to use templates in C++. Now I better figure out how massive that subject — the templates in C++ — can be.)
When I wrote this code (as I talked about it in my previous post):
It caused an Stack overflow run-time error.
But when I wrote the above code (the one I have used the for loop), it does not cause an error! It's very odd for me! What is the reason please?
It permanently calls itself. Hence due to it recursive nature it generates a stack overflow. Nothing stops it from calling itself again and again.
While the second one is not recursive. It can be written as:
1 2 3 4 5
template <class T> ostream& operator<<(ostream& o_s, const vector<T>& v_o) {
for(int i=0; i<v_o.size(); i++)
operator<<(o_s, v_o[i]).operator<<(o_s, ' '); // The first operator is called with the type T (which cannot be the vector again)
return o_s;
}
operator<<(o_s, v_o[i]).operator<<(o_s, ' '); // The first operator is called with the type T (which cannot be the vector again)
So since v_o[i] is not another vector (it's an int value and the output operator is pre-defined for int values) therefore, that overloaded method will not be called anymore. Is it right?
Another question: How to make those operator[]()s useful for that code? Whether I remove them or not, the code runs successfully!
You don't use this operators hence they are ignored.
I have used the operator[] in: template <class T> ostream& operator<<(ostream& o_s, const vector<T>& v_o)
It makes me confused. I don't know the above use belongs to them or it's the built-in use. I meant since the author has wanted us to add them, someway we should apply them.
Yes and what is question for me is that why the author (the creator of C++) has wanted us to add them while adding them is useless!
I Thank both of you coder and cire very much for your help. Apparently this webside doesn't offer the rep feature so I don't know how to appreciate you guys for your useful helps :)
Yes and what is question for me is that why the author (the creator of C++) has wanted us to add them while adding them is useless!
From your image, it was intended to replace getter/setters which, indeed, doesn't make a whole lot of sense as you don't have a container to index. On the other hand, if he meant operator() that might be more reasonable.