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

  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
    28,413
    Suppose it were a class rather than a template class. Would you know how to write that?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

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, 05:17 AM
  5. class template and operator=()
    By ichijoji in forum C++ Programming
    Replies: 4
    Last Post: 10-04-2003, 11:42 PM