define operator<< for std::pair<string, int>

This is a discussion on define operator<< for std::pair<string, int> within the C++ Programming forums, part of the General Programming Boards category; The following code seems working for the built in type, but not for the pair. Can anybody help me out? ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    48

    define operator<< for std::pair<string, int>

    The following code seems working for the built in type, but not for the pair. Can anybody help me out? Thanks.

    Code:
    #include <iostream>
    #include <iterator>
    #include <map>
    using namespace std;
    template<class T>
    void print2darray(const int m, const int n, const T& a) {
    	for (int i = 0; i < m; i++) {
    		for (int j = 0; j < n; j++) {
    			cout << a[i][j] << " ";
    		}
    		cout << endl;
    	}
    }
    
    typedef pair<string, int> mypair;
    
    ostream& operator<<(ostream& s, const mypair& p) const{
    	return s << "(" << p.first << ", " << p.second;
    }
    
    int main() {
    	int a[2][2] = { { 1, 2, }, { 3, 4 } };
    	double b[2][2] = { { 1.0, 2.0 }, { 3.0, 4.0 } };
    	string c[2][2] = { { "as", "it" }, { "mine", "your" } };
    	pair<string, int> d[2][2];
    	for (int i = 0; i < 2; i++) {
    		for (int j = 0; j < 2; j++) {
    			d[i][j] = make_pair(c[i][j], a[i][j]);
    		}
    	}
    cout << "array a[2][2]:" << endl;
    print2darray(2,2,a);
    
    cout << "array  b[2][2]:" << endl;
    print2darray(2,2,b);
    
    cout << "array c[2][2]:" << endl;
    print2darray(2,2,c);
    
    cout << "array d[2][2]:" << endl;
    print2darray(2,2,d);
    
    }

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    What does the error message say?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,657
    Your problem is the definition of operator<<() for your mypair type. It cannot be a const-qualified function, as it is not a member of a class.

    It is generally a good idea not to rely on "using namespace std;" for resolving types of arguments in function definitions (potential for ambiguity if other namespaces and using directives are in play). Declare ostream as std::ostream explicitly.

    You probably also need to add a closing ")" to the end of the output.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why?!?
    By p3rry in forum C Programming
    Replies: 3
    Last Post: 01-08-2009, 12:52 PM
  2. size of an integer pointer
    By onebrother in forum C Programming
    Replies: 5
    Last Post: 07-09-2008, 12:49 PM
  3. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 09:22 AM
  4. Error check problem again
    By rtransfi in forum C Programming
    Replies: 6
    Last Post: 02-27-2003, 04:55 PM
  5. float toolbar!
    By c-- in forum Windows Programming
    Replies: 5
    Last Post: 02-04-2003, 09:44 AM

Tags for this Thread


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