CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2015
    Posts
    175

    Defining operators for vectors of templates

    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 in the chapter! So I don't know how to solve it.

    Code:
    #include <iostream>
    using namespace std;
    
    template <class T> class S {
    public:
    	T val;
    	S(T v) { val = v; }
    	T& operator[](int);
    	const T& operator[](int) const;
    };
    
    //-----------------------------------------
    
    template<class T> T& S<T>::operator[](int n) {  
    	return val[n];
    }
    
    //---------------------------------------------------
    
    template<class T> const T& S<T>::operator[](int n) const {   
    	return val[n];
    }
    
    //--------------------------------------------------------
    
    template <class T> istream& operator>>(istream& i_s, S<T>& v_i) {
    	return i_s >> v_i.val;
    }
    
    //--------------------------------------------------
    
    template <class T> ostream& operator<<(ostream& o_s, const S<T>& v_o) {
    	return o_s << v_o.val;
    }
    
    //---------------------------------------
    
    template <class T> ostream& operator<<(ostream& o_s, const S<vector<T>>& v_o) {
    	return o_s << v_o.val;
    }
    
    //---------------------------------------
    
    template<class T> void read_val(T& v) {
    	cin >> v;
    }
    
    //-------------------------------
    
    template<class T> void write_val(const T& v) {
    	cout << v << '\n';
    }
    
    //-----------------------------------------
    
    template<class T> void write_val(const vector <T>& v) {
    	for(int i=0; i<v.size(); i++)
    		cout << v[i] << ' ';
    	cout << endl;
    }
    
    //------------------------------------
    
    int main()  
    {
    	vector<int> vs;
    	for(int i=0; i<5; i++)
    		vs.push_back(i*i);
    
    	S<int> s1(5); 
    	S<char> s2('c');
    	S<double> s3(5.25);
    	S<string> s4("Template");
    	S<vector<int>> s5(vs);
        
    	write_val(s1);
    	write_val(s5);
    	
    	return 0;
    
    }
    The program runs successfully until write_val(s1); but facing write_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 so far and likely I will learn it the next chapters of the book.
    Thanks in advance.
    Last edited by tomy12; January 12th, 2016 at 02:45 AM.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Defining operators for vectors of templates

    You should have #include <vector> at the start if you are using vectors.

    You haven't defined an operator<< for type const vector<T>. You have
    Code:
    template<class T> void write_val(const vector <T>& v) {
    but this is not called by the write_val(s5) call in main() as s5 is of type S<vector<int>>. Change this write_val() function to be operator<<() function for type const vector<T>&

    PS Step 14 from the book tells you what to do - define template<class T> ostream& operator<<(ostream&, vector<T>&)

    PPS You don't need
    Code:
    template <class T> ostream& operator<<(ostream& o_s, const S<vector<T>>& v_o) {
    	return o_s << v_o.val;
    }
    as this does the same as
    Code:
    template <class T> ostream& operator<<(ostream& o_s, const S<T>& v_o) {
    	return o_s << v_o.val;
    }

    Also note that if you have a choice to use pre-increment or post-increment operator (eg in a for loop), then for performance reasons you should use pre-increment. This makes hardly any difference for a POD type (int etc) but for some classes this can make quite a difference.
    Last edited by 2kaud; January 12th, 2016 at 08:13 AM. Reason: PPS
    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)

  3. #3
    Join Date
    Jun 2015
    Posts
    175

    Re: Defining operators for vectors of templates

    Thank you.
    What about the operator[]()s? Even if I remove them, the code runs fine! So how can they have a role in that code, or, why the author has wanted us to add them in the code please?

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Defining operators for vectors of templates

    What about the operator[]()s? Even if I remove them, the code runs fine!
    For your code, these are not used so removing them has no effect. The operator[] is used when accessing an element. eg used for [] access to a vector element. For cout << v[i] << ' '; operator[] for class vector is used to provide element access.

    why the author has wanted us to add them in the code
    ??

    This book is not one of my favourites from which to learn c++ - even though it is written by the original developer of c++.
    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)

  5. #5
    Join Date
    Jun 2015
    Posts
    175

    Re: Defining operators for vectors of templates

    Thank you.
    I want to study a book on Qt after finishing that book. Do you suggest me to go directly on a Qt book or to read also another book on C++ before going on Qt?

    PS: This is many times that I want to give you reputations but I faced the error to spread some ones to other members before giving it to you. I, too, gave Victor one but even after it I cannot give you your votes that you deserve fully, because of that error message. I'm sorry.

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Defining operators for vectors of templates

    Between giving reputations to the same person you have to give to at least 6 other different people - see http://forums.codeguru.com/showthrea...utation-Around

    As I don't use/know Qt I can't advise.

    PS Qt has it's own support forums at http://forum.qt.io/ for questions regarding qt.
    Last edited by 2kaud; January 13th, 2016 at 05:22 AM. 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)

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Defining operators for vectors of templates

    Please don't use friend feature. I haven't learnt it so far and likely I will learn it the next chapters of the book.
    friend functions are only mentioned in Appendix A12. For interest, one way of coding this example having class private data and using friend functions is
    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    template <class T> class S {
    private:
    	T val;
    
    public:
    	S(T v) { val = v; }
    
    	friend ostream& operator<< <>(ostream& o_s, const S<T>& v_o);
    };
    
    //--------------------------------------------------
    
    template <class T> ostream& operator<<(ostream& o_s, const S<T>& v_o) {
    	return o_s << v_o.val;
    }
    
    //------------------------------------
    template<class T> ostream& operator<<(ostream& os, const vector <T>& v) {
    	for (size_t i = 0; i<v.size(); i++)
    		os << v[i] << ' ';
    
    	return os << endl;
    }
    
    //-------------------------------
    
    template<class T> void write_val(const T& v) {
    	cout << v << '\n';
    }
    
    //-----------------------------------------
    
    int main()
    {
    	vector<int> vs;
    	for (int i = 0; i<5; i++)
    		vs.push_back(i*i);
    
    	S<int> s1(5);
    	S<char> s2('c');
    	S<double> s3(5.25);
    	S<string> s4("Template");
    	S<vector<int>> s5(vs);
    
    	write_val(s1);
    	write_val(s5);
    
    	return 0;
    
    }
    The highlighted line is the one that declares the function as a friend of the class. Simplicity, this means that the function has access to the private data members of the class.

    PS Note that the type of .size() is size_t (which is unsigned) and not int (which is signed) - so variables that use .size() should be of type size_t otherwise you'll probably get compiler warnings about signed/unsigned.
    Last edited by 2kaud; January 13th, 2016 at 04:41 AM. 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)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured