how do i use the operator+ in class template in my program

This is a discussion on how do i use the operator+ in class template in my program within the C++ Programming forums, part of the General Programming Boards category; say i have a list of numbers in an array created by a class template Code: #include <iostream> #include <fstream> ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    14

    how do i use the operator+ in class template in my program

    say i have a list of numbers in an array created by a class template
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <iomanip>
    
    
    using namespace std;
    
    const int CAPACITY = 20;
    
    
    template <class T>
    
    
    class A
    {
    public:
                A();
                ~A();
                 operator+(dont know what goes here)
    private:
    int *number;
    int count;
    
    }
    
    template<class T>
    
    A<T>::A(){
     number=new string[CAPACITY];
    
    //more code that fills in the array here
    //count increases as array is filled
    }
    
    template<class T>
    
    A<T>::operator+(?){
    
    
    }
    
    int main(){
    
    A<T> object;
    
    object+454556+5644;//adds 454556 and 5644 to the array
    or can you not do this

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,397
    Suppose it were a class rather than a template class. Would you know how to write that?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,193
    You might want to read this link. People are not mindreaders, and asking them random meaningless questions is not an effective way to get advice.

    What do you mean by "add 454556 and 5644 to the array"? If you can't specify that, you cannot hope to implement code to do it - whether template based or not.

    For example, one interpretation is to add 454556 to 5644 (to get the value 460200) and then add that single value to the array. Another interpretation is, for both values, to add them each as a single value to the array. Both of those interpretations rely on a specification of what it means to "add a single value to the array". You have not specified that.

    Either way, you probably would not do it with operator+(). The purpose of operator+() is generally to create a new value, based on its operands, without changing those operands. Or, to provide an example, the statement "c = a + b;" gives c a new value without changing either a or b.

    The fact that you initialise a pointer to int using "number = new string[CAPACITY];" means your constructor will not compile. It also just highlights the fact your question is imprecise, ambiguous, and therefore probably meaningless.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using new operator for template class
    By wildcard_seven in forum C++ Programming
    Replies: 3
    Last Post: 10-11-2011, 02:39 AM
  2. Operator overload with template class
    By Micko in forum C++ Programming
    Replies: 8
    Last Post: 07-05-2011, 04:57 AM
  3. operator+ for template class
    By MarkZWEERS in forum C++ Programming
    Replies: 5
    Last Post: 05-07-2008, 03:55 PM
  4. Replies: 7
    Last Post: 11-10-2007, 04:17 AM
  5. class template and operator=()
    By ichijoji in forum C++ Programming
    Replies: 4
    Last Post: 10-04-2003, 11:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21