Click here to Skip to main content
15,895,740 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Questionhow to avoid dead-lock if you write multi-thread programming Pin
yu-jian1-Aug-11 2:50
yu-jian1-Aug-11 2:50 
AnswerRe: how to avoid dead-lock if you write multi-thread programming Pin
Albert Holguin1-Aug-11 3:20
professionalAlbert Holguin1-Aug-11 3:20 
QuestionSOCKET : How to gracfully stop application when recv() function is blocking Pin
pandit841-Aug-11 1:05
pandit841-Aug-11 1:05 
AnswerRe: SOCKET : How to gracfully stop application when recv() function is blocking PinPopular
Peter_in_27801-Aug-11 1:25
professionalPeter_in_27801-Aug-11 1:25 
JokeRe: SOCKET : How to gracfully stop application when recv() function is blocking Pin
Code-o-mat1-Aug-11 5:57
Code-o-mat1-Aug-11 5:57 
AnswerRe: SOCKET : How to gracfully stop application when recv() function is blocking Pin
jschell1-Aug-11 8:17
jschell1-Aug-11 8:17 
GeneralRe: SOCKET : How to gracfully stop application when recv() function is blocking Pin
pandit841-Aug-11 19:34
pandit841-Aug-11 19:34 
QuestionCRichEditCtrl Font Formating Pin
yurii_leorda30-Jul-11 15:11
yurii_leorda30-Jul-11 15:11 
AnswerRe: CRichEditCtrl Font Formating Pin
Code-o-mat30-Jul-11 22:21
Code-o-mat30-Jul-11 22:21 
GeneralRe: CRichEditCtrl Font Formating Pin
yurii_leorda31-Jul-11 0:21
yurii_leorda31-Jul-11 0:21 
GeneralRe: CRichEditCtrl Font Formating Pin
Code-o-mat31-Jul-11 0:37
Code-o-mat31-Jul-11 0:37 
QuestionRuntime Error in Visual Studio [modified] Pin
AndrewG123129-Jul-11 12:09
AndrewG123129-Jul-11 12:09 
AnswerRe: Runtime Error in Visual Studio Pin
Richard Andrew x6429-Jul-11 13:07
professionalRichard Andrew x6429-Jul-11 13:07 
GeneralRe: Runtime Error in Visual Studio Pin
AndrewG123129-Jul-11 13:20
AndrewG123129-Jul-11 13:20 
GeneralRe: Runtime Error in Visual Studio Pin
Richard Andrew x6429-Jul-11 13:36
professionalRichard Andrew x6429-Jul-11 13:36 
GeneralRe: Runtime Error in Visual Studio Pin
AndrewG123129-Jul-11 14:24
AndrewG123129-Jul-11 14:24 
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 

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.