-1

I define on main 2 variables m,n from new class S. Then, I want to swap them with the template function swap.. The question is: what are the methods that S use to run this code and how it look like? :

template <class T>
    void swap(T &a ,T &b)
    {
        T tmp= a; 
        a=b;
        b=tmp; 
    }

template <class T>
class S{ 
public:
    S:();  
    S:(const S& data);
    ~S();
    S &operator=(const S&g); 

};

int main(){
    S m,n; 
    swap(m,n);
    cout<< "m is "<<m<< "n is "<<n<<endl; 
    return 0;
 }
user3322858
  • 43
  • 1
  • 7
  • Can you clarify what you're asking about? I will call the copy ctor once and copy assignment op twice, if that's what you're after. – Angew is no longer proud of SO Feb 18 '14 at 10:23
  • Well, the type must have a copy c'tor and assignment operator... – StoryTeller - Unslander Monica Feb 18 '14 at 10:24
  • Yes, that's my intention. I know I need a default constractor and destractor, and I know I need a copy constructor and assignment operator, but how the class should look like. My visual means that an error: template class Myclass{ public: Myclass:(); Myclass:(const Myclass& data); ~Myclass(); Myclass&operator=(const Myclass&g); }; – user3322858 Feb 18 '14 at 10:29
  • it's not clear what you are trying to do here...how do you use the template? – Pandrei Feb 18 '14 at 10:32
  • I try to define the class S with it.. – user3322858 Feb 18 '14 at 10:33
  • If you're asking what a copy-constructor, assignment-operator, etc.. "look like" there are plenty of examples in almost any C++ text. You know your class, you know what is in it, you know what needs to be copied, calculated, etc. Ultimately only you can craft the correct code to ensure it is right. – WhozCraig Feb 18 '14 at 10:36
  • Yes but I can not compile it. Something is not right even though I do everything right. – user3322858 Feb 18 '14 at 10:37
  • Those two statements are by definition a contradiction. And without your code, we cannot tell you what you did *wrong*. Post it. – WhozCraig Feb 18 '14 at 10:40
  • You might want to pick up a [good C++ book](http://stackoverflow.com/q/388242/1782465). – Angew is no longer proud of SO Feb 18 '14 at 10:41
  • visual say : argument list for class template "S" is missing – user3322858 Feb 18 '14 at 10:41
  • Without code, it didn't happen. *Post it*. – WhozCraig Feb 18 '14 at 10:43
  • How I add code to comment? – user3322858 Feb 18 '14 at 10:44
  • You don't. Add it to the *question*. – WhozCraig Feb 18 '14 at 10:45
  • Thanks for the post. Now where does `S` come into this? There is no `S` type that I can see. And there is no code for any of those members, those are all decls without any implementations. Edit: You're (a) not providing the required template parameter for your `S` class, and (b) have no code (at least none posted) for any of the S members. I concur with Angew. – WhozCraig Feb 18 '14 at 10:47

3 Answers3

0

You don't need to implement your own class for your template function example, because you very certainly just want to swap numbers. However if you decide to make your own (whatever) template class, then you may have to overload the assignment operator and maybe define a proper constructor to use your swap function properly. For the standard output in your example you also have to overload this operator http://www.cplusplus.com/reference/ostream/ostream/operator%3C%3C/. Unfortunately the other features you have to implement (and how) depend on what your (whatever) class should do.. Your question is not very certain in that.

dgrat
  • 2,214
  • 4
  • 24
  • 46
0

This is a good article from cplusplus.com. Note that, A& and const A& are two different types. Therefore, your swap function would only use the copy constructor ( assignment operator) of the parameter type A&, not const A&. Play with the following code.

template <class T>
void myswap(T &a, T &b) {
    T temp = a;
    a = b;
    b = temp;
}

template <class T>
class A {
public:
    T t;
    A( T t_ ) : t(t_){}
    A( A& a){
        cout<<"In A's copy ctor A&a"<<endl;
        t = a.t;
    }    
    A& operator = (A& a){
        cout<<"In A's assignment operator A&a"<<endl;
        t = a.t;
    }
};

int main() {
    A<int> a1(3);
    A<int> a2(4);
    myswap(a1, a2);
    cout<<a1.t << "  " << a2.t <<endl;
    return 0;
}

The output is

In A's copy ctor A&a
In A's assignment operator A&a
In A's assignment operator A&a
4  3
Peng Zhang
  • 3,475
  • 4
  • 33
  • 41
0
template <class T>
class S
...

This code means that S is a class template. That is, S is not a class but a template.

S m,n; 

This declares two variables of class S. Here is your problem: the syntax requires a class, and you gave it a template.

Solution: "instantiate" your template. To do it, append the template parameter to the name of the template:

S<int> m,n;

Another solution: get rid of the template:

// Removed the line "template <class T>" here
class S {
    ...
}
...
S m,n;

If you don't know which solution to use, use the second one, because it simplifies your code.

anatolyg
  • 26,506
  • 9
  • 60
  • 134