|
Hello, I have a problem with overloading operator<< combined with namespaces. The following code compiles OK:
file test_matrix.hpp:
#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;
namespace VecMat {
typedef ublas::matrix<double> MatrixD;
template<class MT>
std::ostream & operator<< (std::ostream & os,
const ublas::matrix_expression<MT> & M)
{
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;
}
}; #endif
file test_oper.cpp:
#include "test_matrix.hpp"
using std::cout;
using std::endl;
using VecMat::MatrixD;
using VecMat::operator<<;
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:
#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
|
|
|
|
|
I think the line :
using VecMat::operator<<;
is causing the problem. Try commenting it off and see what happens.
|
|
|
|
|
Sorry, but without it, it won't compile at all (even without the extra file).
|
|
|
|
|
I made the suggestion because I don't believe the using statement is intended for operators. It is for namespaces and I am surprised that it does not result in an error.
|
|
|
|
|
hi every one
i am tried to mapp the large file
SYSTEM_INFO siSysInfo;
DWORD map_data_size;
GetSystemInfo(&siSysInfo);
map_data_size = siSysInfo.dwAllocationGranularity;
HANDLE h_rar_file = CreateFile(filepath,GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN,NULL);
HANDLE h_map_file = CreateFileMapping(h_rar_file,NULL, PAGE_READONLY ,ull64FileSize.HighPart,ull64FileSize.LowPart,NULL);
pbuf = (BYTE*) MapViewOfFile(h_map_file, FILE_MAP_READ ,(DWORD)0,0,map_data_size);
it mapping but problem is that
i am traversing till end of the file
first time it will traverse for second time
it wont show the the position pbuf[off_set] its shows the offset value .
help out from this .
because of this my application crashing
|
|
|
|
|
Are you sure that it is the allocation granularity that becomes the number of bytes to map?
What do you mean by traversing for the second time?
Is the traversing done in a loop?
Please post some code where the error actually happens like the traversal code.
On a side note, please always submit code within the code tags.
|
|
|
|
|
ya its allocation granularity that becomes the number of bytes to map
the code very big so i will put where the error is occur ...
while(map_data_size <ull64FileSize.QuadPart)
{
if(pbuf[off_set] == 0x73 && pbuf[off_set + 3]==0x0D)
{.......
off_set++
}
UnmapViewOfFile(pbuf);
map_data_size+=map_data_size;
}
what is the error is the after traversing the offset value is became 1463028177
when it comes to pbuf[off_set] its showing the the error Expression can't be evaluated
help me out of this
|
|
|
|
|
What happens if off_set is the size of pbuf[] ?
Then pbuf[off_set + 3] would overflow the buffer.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Hi,
I have created the windows service which does the ShellExecute command on noteapad.exe.
When service is installed the notepad.exe is executed and only seen in task manager. The Notepad.exe window is not visible.
Could anyone let me know what may be the issue with this, I have tried this on Windows XP where it works fine.
Following is the code for SheelExecute
<pre lang="vb">::ShellExecute(NULL,
NULL,
_T("notepad.exe"),
_T("C:\\Users\\testuser\\Desktop\\review_comments.txt"),
_T("C:\\Windows\\System32"),
SW_SHOWMAXIMIZED);</pre>
Thanks in advance
"A winner is not one who never fails...but the one who never quits"
|
|
|
|
|
From Vista onwards, services are isolated in session 0 and cannot show any UI.
You will need to use CreateProcessAsUser /CreateProcessWithLogonW /CreateProcessWithTokenW APIs to initiate UI application from services.
|
|
|
|
|
Hi,
In my MDI application im loading one DialogBar in mainframe.But i want to show that dialog bar when some child window invokes(GroupView,TrendView).For other windows it should be in hide(GraphView).So for all functions used to invoking child window from CMainFrame im hiding the toolbae like this.
void CMainFrame::OnGroupview()
{
.
.
.
m_RecentAlarms.ShowWindow(SW_HIDE);
}
For some view
void CMainFrame::OnGroupview()
{
.
.
.m_RecentAlarms.ShowWindow(SW_SHOW);
}
So,when i click TrendView i get the dialog bar,then when i click GraphView the dialgobar get hided.But Again when i click TrendView I didnt get Dialogbar.Because the application is multiple window.The previous TrendView is behind the graphview,when i invoke just it show in front.
So my idea is i have one thread in Mainframe,this function updates some values in all views.In that i will check whether the Active View is TrendView,if it so then dialog bar is in SW_SHOW other wise it is SW_HIDE.
I used this..
CMDIChildWnd* pChild = ((CMainFrame*)AfxGetMainWnd())->MDIGetActive();
But i dont know how to get the active view is TrendView only....
Pls help me in this issue.
Anu
|
|
|
|
|
Override the OnActivateView method in your view classes.
In each view class you can check if the activated view is the current view by comparing the pActivateView parameter with the this pointer -
if (pActivateView == this) ...
|
|
|
|
|
I'm currently working with the CapturePerfect SDK from Canon to create a custom application for a client's scanner. I apologize if the following question seems rather elementary, my background is in web development and I was thrown onto this project with very little knowledge of C++.
The file I'm experiencing the errors on is part of the SDK and remains unchanged, but I'm receiving a series of errors for each variable definition within the typedef struct.
typedef struct tagCEICUSTOMDLGINFOA {
DWORD cbSize;
HWND hWnd;
LPCSTR lpCaption;
DWORD dwDialog;
DWORD dwFlags;
} CEICUSTOMDLGINFOA, *LPCEICUSTOMDLGINFOA;
I'm receiving a series of errors:
error C2146: syntax error : missing ';' before identifier 'cbSize'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
That's just the first 3 errors. Each line in the snippet, excluding the first and last, produces these same three errors for it's respective variable.
There are many more typedef struct functions in the program, each one is returning these errors for their variables.
Any help with this would be greatly appreciated. Thanks.
|
|
|
|
|
try including "windows.h" at the top of the file that includes this SDK file.
The errors are due to the Windows types not being defined.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Thanks for the quick reply. Nearly 200 errors gone with that line. I feel like a bit of a fool now.
|
|
|
|
|
Glad I could help.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Without that little line, most of your structure is undefined. If you're using a pre-compiled header, might as well include it there. If you're working with an MFC app, you shouldn't need to include it explicitly.
|
|
|
|
|
Good and quick catch, +5
|
|
|
|
|
Hi.
I wrote a simiral question before but I feel like I am still confused.
I am compiling in Unicode and I know that in Unicode circumstance CString is allocated with TCHAR as you see below.
typedef ATL::CStringT< TCHAR, StrTraitMFC_DLL< TCHAR > > CString;
So obviously CString has nothing to do with LPCTSTR, which is const char *, or any other type, but TCHAR.
However, when I try to do this code below it occurs an error.
CString _str = TEXT("ABCD");
TCHAR* _tstr = _str;
The correct version of the code above should be like this.
CString _str = TEXT("ABCD");
TCHAR* _tstr = (TCHAR*)(LPCTSTR)_str;
There are two type castings, to LPCTSTR and then to TCHAR*, even though CString is defined with TCHAR type.
I'd like to know why this weird conversion happens from the operator overloading's view of CStringT, and CSimpleStringT and also the template structure of all the classes related to CString.
If I am asking too much to answer in a few sentences, any web pages' link that explain this will be appreciated.
Thanks in advance!
|
|
|
|
|
Dean Seo wrote: CString _str = TEXT("ABCD");
TCHAR* _tstr = _str;
That's not acceptable. _tstr is a pointer to a char (or wchar_t based on your build). But, _str is a CString variable and so, this conversion won't work.
Dean Seo wrote: CString _str = TEXT("ABCD");
TCHAR* _tstr = (TCHAR*)(LPCTSTR)_str;
That's not a good thing to do either. LPCTSTR (with the added C in between means that it's a const ). By explicitly casting it to a TCHAR* , you're casting the const away and then you are assigning it to a TCHAR* later.
Remember that CString is a C++ class. It uses a string (a char* or a wchar_t* ) internally to store the contents, but it does a lot of other things as well. If you really need to acquire a pointer to the location where the class is storing the string, then there's this CString::GetBuffer()[^] method. There are some quirks associated with this method though, and you need to exercise caution while using it. If you call it, you want to make sure that you call CString::ReleaseBuffer()[^] immediately after you're done with the buffer. If you don't know what that means, you don't want to use that method.
I strongly recommend that you read the following essays:
The Complete Guide to C++ Strings, Part I - Win32 Character Encodings[^]
The Complete Guide to C++ Strings, Part II - String Wrapper Classes[^]
CString Management[^]
"Real men drive manual transmission" - Rajesh.
|
|
|
|
|
Hi, good to see you again.
Rajesh R Subramanian wrote: That's not acceptable. _tstr is a pointer to a char (or wchar_t based on your build). But, _str is a CString variable and so, this conversion won't work.
CString is a class using a wchar_t* to store the contents, and TCHAR is wchar_t.
CSimpleStringT has "operator PCXSTR();", and PCXSTR in Unicode circumstance means LPCWSTR which is also wchar_t*.
So if CSTring uses wchar_t and has a perfect method(operator PCXSTR()) to return wchar_t type,
why can't I get it using TCHAR* (which is also wchar_t*)
I think at least this code below has to work, whether it is safe to use or not.
CString _str = TEXT("ABCD");
TCHAR* _tstr = (TCHAR*)_str;
Thank you for helping me this time again by the way.
|
|
|
|
|
Dean Seo wrote: LPCWSTR which is also wchar_t*.
No, it is not.
LPCWSTR is const wchar_t* and the added "const " has its significance!
Dean Seo wrote: I think at least this code below has to work, whether it is safe to use or not.
CString _str = TEXT("ABCD");
TCHAR* _tstr = (TCHAR*)_str;
That code may compile, but it may or may not work depending on what you intend to do with the _tstr variable. If you use the pointer to modify the contents it's pointing to, then you'll be in trouble, because you promised CString not to modify the contents stored in the pointer that it returned ("const ", remember?).
I'm happy to answer your questions, but before you jump into anything further, I recommend that you read the 3 articles I linked you to, and that will clear up a lot of your doubts.
"Real men drive manual transmission" - Rajesh.
|
|
|
|
|
Rajesh R Subramanian wrote: I recommend that you read the 3 articles I linked you to, and that will clear up a lot of your doubts.
I read those 3 articles all the way through and it did help. Thank you.
I will print out those articles and read again anytime I need.
Rajesh R Subramanian wrote: That code may compile, but it may or may not work depending on what you intend to do with the _tstr variable.
By the way, in that case, the thing is that that code doesn't even comfile and it occurs an 'casting failed' error.
So I just made a simple example that claify why this happens.
class A{
public:
const wchar_t* m_WideCharacter;
operator const wchar_t*() const{
return m_WideCharacter;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
A aaa;
wchar_t* _WideCharacter = aaa;
wchar_t* _WideCharacter = (wchar_t*)aaa
wchar_t* _WideCharacter = (wchar_t*)(const wchar_t*)aaa
return 0;
}
I think I just misunderstood how const and operator() that returns const value in C++ works.
To make operator const wchar_t*() const return the value, we definitely need the type casting (const wchar_t*) first, otherwise the compiler wouldn't understand if the operator method in class A can be possibly called.
And that is what (const wchar_t*) is for. That's it.
That is why in that MFC based code I need to write (LPCTSTR*) because CString only has operator PCXSTR(), which is the same as operaotr const wchar_t*(), and the only way that the operator PCXSTR() is called is to write (LPCTSTR*) for type casting.
Am I correct? Or am I not clear enough???
modified on Friday, July 29, 2011 1:52 AM
|
|
|
|
|
Dean Seo wrote: By the way, in that case, the thing is that that code doesn't even comfile and it occurs an 'casting failed' error.
I overlooked it to assume that you did a double casting to take the const away. That is a common thing (a terrible thing, actually) that people do:
CString _str = TEXT("ABCD");
TCHAR* _tstr = (LPTSTR)(LPCTSTR)_str;
Dean Seo wrote: wchar_t* _WideCharacter = (wchar_t*)(const wchar_t*)aaa // works!
You shouldn't say "works!". You should instead say "it compiles!". Yeah, it will compile. But it's a terrible thing to do. How about this instead?
A aaa;
const wchar_t* p = aaa;
There are several APIs (and functions that we may code) that will need a read-only string. For example, you may write a function that takes the path of a file and encrypts it. So, you'll just be accepting the string which will be the path, and you'll not be modifying the string itself. In cases like that (and in MOST cases), you will only need a read-only string. That's why CString gives you a LPCTSTR (const wchar_t* ), which will be pointing to the internal buffer where CString is storing the string.
But there are those edge cases where might want to have direct write access to the internal buffer of CString . For such cases, CString provides the GetBuffer() method (which I already explained).
Conclusively, there is no excuse for abusing the LPCTSTR operator (using it to cast the const away and assigning it to a non-const variable is bad).
"Real men drive manual transmission" - Rajesh.
|
|
|
|
|
Dean, let me know if you need any further explanation.
"Real men drive manual transmission" - Rajesh.
|
|
|
|
|