-
January 10th, 2016, 05:19 PM
#16
Re: Problem in templates in C++
Now this error!
Error 1 error C2662: 'S<T>::get' : cannot convert 'this' pointer from 'const S<T>' to 'S<T> &'
-
January 10th, 2016, 05:35 PM
#17
Re: Problem in templates in C++
because your get() returns a ref and not a const ref and operator<<() requires a const ref. Try
Code:
const T& get() const { return val; }
PS Your write_val() should be defined as
Code:
template <class T> void write_val(const T& t) {
Last edited by 2kaud; January 10th, 2016 at 05:37 PM.
Reason: PS
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 11th, 2016, 05:55 AM
#18
Re: Problem in templates in C++
Thank you again for the helps.
I read the posts from beginning and I understood the matter, but not 100%!
I have some questions here. Since the subject is complex please answer them using a simple language. That way I can get the matter completely.
1- In post #3 you said: You need to provide specific stream insertion/extraction operators for non-standard supported types.
OK, that overloaded method had problems but why wasn't it a specific stream extraction operator for the type T or the class S?
2- Do you refer to both of T and the class S by saying non-standard supported types?
3- In post #9 Victor said: you have to overload it for each class/structure/UDT you are going to use.
Did he mean: "Since we here use only the type T so only for it we define an overloaded method and we would do for the class S if we used that class somewhere in the code"? In the code apparently we use classes of T type. That way can't we say we have used both of the class and also the type T? If we could say yes, so we should define one overloaded method for the type T (as we did it) and another one for the class!
4- In post #13 you said: This because when operator<< is used within a class definition it only takes one parameter (ostream&) as it operates upon the class instantiated data.
What is the class instantiated data? Is it the val? What if we had an int n; as a private data just like val inside the class? Then could the operator defined within the class get two parameters (as we have two ones here)?
5- In post #14 we have const S<T>& to) { return os << to.get(); } in the code.
Here the returning type of get() is a kind of T, so the result of to.get() is a T& (the val) but in the operator<< definition, we should have gotten a const S<T>& not a T&. That way the code should not work, but in practice it does! Why please?
6- In post #17 we have: const T& get() const { return val; }. The first const (in it) specifies the returning type is not changeable. The second const specifies that, this method (the get()) doesn't change anything in the body of its caller. Are these right? If so, why the second const is needed?
The compiler gives this error.
Error 1 error C2662: 'S<T>::get' : cannot convert 'this' pointer from 'const S<T>' to 'S<T> &'
This is the code so far.
Code:
#include <iostream>
using namespace std;
template <class T> class S {
T val;
public:
S(T v) { val = v; }
const T& get() const { return val; }
T& set() { return val; }
T& operator[](int);
const T& operator[](int) const;
};
//-----------------------------------------
template<class T> T& S<T>::operator[](int n) { //Useless for now
return val[n];
}
//---------------------------------------------------
template<class T> const T& S<T>::operator[](int n) const { //Useless for now
return val[n];
}
//--------------------------------------------------------
template <class T> istream& operator>>(istream& i_s, S<T>& v_i) {
return i_s >> v_i.set();
}
//--------------------------------------------------
template <class T> ostream& operator<<(ostream& o_s, const S<T>& v_o) {
return o_s << v_o.get();
}
//---------------------------------------
template<class T> void read_val(T& v) {
cin >> v;
}
//-------------------------------
template<class T> void write_val(const T& v) {
cout << v << endl;
}
//-----------------------------------------
int main()
{
S<int> s1(5);
read_val(s1);
write_val(s1);
keep_window_open();
return 0;
}
I'm sorry for asking those many questions. The matter was very complicated and subtle for me.
Last edited by tomy12; January 11th, 2016 at 06:00 AM.
-
January 11th, 2016, 07:16 AM
#19
Re: Problem in templates in C++
Code:
template <class T> ostream& operator<<(ostream& os, T& to) {
os << to;
return os;
}
Lets say that T is of type myType, so to is of the type myType. So
tries to find a function operator<<(ostream&, myType). There isn't a specific one but the templated one could match if T was of type myType, so the function calls itself - repeatedly!
Effectively, there is no class S. There are only instances of classes S<T>
Code:
S<int> s1;
S<double> s2;
s1 and s2 are specific instances of the class. non-static data stored in a class declaration is specific to a class instance.
The compiler gives this error.
Error 1 error C2662: 'S<T>::get' : cannot convert 'this' pointer from 'const S<T>' to 'S<T> &'
Is this from the code in post #18, as this compiles for me using VS2015.
For more info about some of these points, have a look at some of the sections on http://www.learncpp.com/
What book are you using?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 11th, 2016, 09:15 AM
#20
Re: Problem in templates in C++
No. That error occurs if I remove the second const from the method const T& get() const { return val; }
The book I read is programming principle and practice using C++ by the creator of C++.
Anyway, thanks for your comments.
I think I should seek answers for the rest of my questions some else where.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|