Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Template Default parameter in Constructor [Solved]

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Freiza
Grandmaster Cheater
Reputation: 22

Joined: 28 Jun 2010
Posts: 662

PostPosted: Thu Feb 12, 2015 8:29 pm    Post subject: Template Default parameter in Constructor [Solved] Reply with quote

Code:
//template.h using MSVC++ 2010
#pragma  once
#include <iostream>
using std::ostream;
template <typename T, typename U> class Pair {
  private:
    T first;
    U second;

  public:

    // Pair() ;
    Pair ( T x = T() , U y = U() ) ;
    template<typename T, typename U>
    friend ostream& operator<< ( ostream& thisPair, Pair<T, U>& otherPair );

};
template <typename T, typename U>
Pair<T, U>::Pair ( T x , U y ) : first ( T ( x ) ), second ( U ( y ) )
{cout << x << y;}

template <typename T, typename U>
ostream& operator<< ( ostream& os, Pair<T, U>& otherPair )
{
    os << "First: " << otherPair.first
<< " "<< "Second: " << otherPair.second << endl;
    return os;
}

//template.cpp
int main()
{
    int a = 5, b = 6;
    Pair<int,int> pair4();
    Pair<int, int> pair1 ( a, b );
    cout<<pair4;
    cout<<pair1;
    return 0;
}


How to make a constructor or a member function to take default value? The code above is giving linker error for pair4 when using cout statement. The code works perfectly when cout<<pair4(); is commented. I am trying to mimic a constructor taking 0,1 or 2 argument using a single default constructor in a template class.

[Edit]
use Pair<int,int> pair4; instead of pair4();
Thank You

_________________
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites