Click here to Skip to main content
15,887,027 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionTaskbar button text [modified] Pin
kartikdasani29-Jul-11 2:24
kartikdasani29-Jul-11 2:24 
AnswerRe: Taskbar button text Pin
Richard MacCutchan29-Jul-11 2:34
mveRichard MacCutchan29-Jul-11 2:34 
AnswerRe: Taskbar button text Pin
«_Superman_»29-Jul-11 3:19
professional«_Superman_»29-Jul-11 3:19 
AnswerRe: Taskbar button text Pin
Richard MacCutchan29-Jul-11 6:54
mveRichard MacCutchan29-Jul-11 6:54 
Questionfunction header temporary variable Pin
LionAM29-Jul-11 0:37
LionAM29-Jul-11 0:37 
AnswerRe: function header temporary variable Pin
barneyman29-Jul-11 1:29
barneyman29-Jul-11 1:29 
GeneralRe: function header temporary variable Pin
LionAM29-Jul-11 3:03
LionAM29-Jul-11 3:03 
Questionoverloading operator<<() vs. namespaces Pin
Michal Kaut28-Jul-11 23:46
Michal Kaut28-Jul-11 23:46 
Hello, I have a problem with overloading operator<< combined with namespaces. The following code compiles OK:

file test_matrix.hpp:
C++
#ifndef TEST_MATRIX_HPP
#define TEST_MATRIX_HPP

#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/matrix_expression.hpp>
namespace ublas = boost::numeric::ublas; // shortcut name

namespace VecMat {
	typedef ublas::matrix<double> MatrixD; // matrix of doubles

	template<class MT>
	std::ostream & operator<< (std::ostream & os,
	                           const ublas::matrix_expression<MT> & M)
	{
		// Note: the matrix_expression<MT> has only one method "()", which
		// returns "& MT" or "const & MT" - a ref. to the included matrix object.
		typename MT::const_iterator1 it1;
		typename MT::const_iterator2 it2;
		for (it1 = M().begin1(); it1 != M().end1(); ++it1) {
			for (it2 = it1.begin(); it2 != it1.end(); ++it2) {
				os << *it2 << "\t";
			}
			os << std::endl;
		}
		return os;
	}
}; // namespace Math
#endif

file test_oper.cpp:
C++
#include "test_matrix.hpp"
using std::cout;
using std::endl;
using VecMat::MatrixD;
using VecMat::operator<<;

// ---------------------------------------------------------------------------
// would be in a header file
void test1 ();
namespace Main {
	void test2 ();
}
// ---------------------------------------------------------------------------

void test1 ()
{
	MatrixD X(10,3);
	VecMat::operator<<(cout << endl, X) << endl;
	cout << "X =" << endl << X << endl;
}

void Main::test2 ()
{
	MatrixD X(10,3);
	VecMat::operator<<(cout << endl, X) << endl;
	cout << "X =" << endl << X << endl;
}


However, when I add another class in another namespace, with its own operator<<, then the code stops working.

file test_other.hpp:
C++
#ifndef TEST_OTHER_HPP
#define TEST_OTHER_HPP
#include <ostream>
using std::ostream;

namespace Main {
	class Foo {
		int n;
	};

	ostream & operator<< (ostream & os, Foo const & foo);
}
#endif

If I then '#include "test_other.hpp"' from either of the two original files, the .cpp file won't compile with the following message (gcc):
test_oper.cpp||In function 'void Main::test2()':|
test_oper.cpp|29|error: no match for 'operator<<' in '((std::basic_ostream<char>*)std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char>&)(& std::cout)), ((const char*)"X =")))->std::basic_ostream<_CharT, _Traits>::operator<< [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>](std::endl [with _CharT = char, _Traits = std::char_traits<char>]) << X'|
lib\gcc\mingw32\4.5.2\include\c++\ostream|108|note: candidates are: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ostream_type& (*)(std::basic_ostream<_CharT, _Traits>::__ostream_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]|
...
||=== Build finished: 19 errors, 0 warnings (0 minutes, 1 seconds) ===|


The code fails on the last line, i.e. the explicit call of operator<<() on the next-to-last line works OK - as does using the overloaded operator outside the namespace (in test1()).

Can anyone explain what is going on here and/or help me to fix this, please?

Thanks in advance.
Michal
AnswerRe: overloading operator<<() vs. namespaces Pin
Rick York31-Jul-11 7:02
mveRick York31-Jul-11 7:02 
GeneralRe: overloading operator<<() vs. namespaces Pin
Michal Kaut31-Jul-11 22:32
Michal Kaut31-Jul-11 22:32 
GeneralRe: overloading operator<<() vs. namespaces Pin
Rick York3-Aug-11 12:36
mveRick York3-Aug-11 12:36 
QuestionMapping of file Pin
sarfaraznawaz28-Jul-11 21:42
sarfaraznawaz28-Jul-11 21:42 
AnswerRe: Mapping of file Pin
«_Superman_»29-Jul-11 3:37
professional«_Superman_»29-Jul-11 3:37 
GeneralRe: Mapping of file Pin
sarfaraznawaz29-Jul-11 20:30
sarfaraznawaz29-Jul-11 20:30 
GeneralRe: Mapping of file Pin
Richard Andrew x6431-Jul-11 6:30
professionalRichard Andrew x6431-Jul-11 6:30 
QuestionIssue with ShellExecute on Windows 7 in windows service application Pin
Ganesh_T28-Jul-11 21:14
Ganesh_T28-Jul-11 21:14 
AnswerRe: Issue with ShellExecute on Windows 7 in windows service application Pin
«_Superman_»29-Jul-11 3:25
professional«_Superman_»29-Jul-11 3:25 
QuestionGet Active WIndow Name Pin
Anu_Bala28-Jul-11 20:35
Anu_Bala28-Jul-11 20:35 
AnswerRe: Get Active WIndow Name Pin
«_Superman_»29-Jul-11 3:41
professional«_Superman_»29-Jul-11 3:41 
QuestionTypedef struct help Pin
WebDev.ChrisG28-Jul-11 9:13
WebDev.ChrisG28-Jul-11 9:13 
AnswerRe: Typedef struct help Pin
Richard Andrew x6428-Jul-11 9:20
professionalRichard Andrew x6428-Jul-11 9:20 
GeneralRe: Typedef struct help Pin
WebDev.ChrisG28-Jul-11 9:23
WebDev.ChrisG28-Jul-11 9:23 
GeneralRe: Typedef struct help Pin
Richard Andrew x6428-Jul-11 9:25
professionalRichard Andrew x6428-Jul-11 9:25 
GeneralRe: Typedef struct help Pin
Albert Holguin28-Jul-11 9:43
professionalAlbert Holguin28-Jul-11 9:43 
GeneralRe: Typedef struct help Pin
Albert Holguin28-Jul-11 10:11
professionalAlbert Holguin28-Jul-11 10:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.