|
Hi, everyone!
I think my knowledge about interface of C++ is not enough.
So, I want to learn some online tutorials. I have used
Google to search "interface tutorial", but fail to find
any useful materials.
Can anyone tell me where can I find some tutorials on how to
use interface of C++?
I want to know basic ideas and experience and tricks about
interface of C++.
Thanks in advance,
George
|
|
|
|
|
Hello,
I have a class and I want to override its [] operator. So I did like this:
int operator [] (LPCTSTR x)
{
.....
}
Now I want my operator to return long, doubles, strings etc. I don't want to manually write dozen of operators, but handle it with templates:
template <typename T>
T operator [] (LPCTSTR x)
{
T t;
....
return t;
}
This complies fine. My problem is that I don't know how to call my template operator. I tried:
MyClass m;
m["str"]<int>;
m<int>["str"];
m.operator <int>[] ("str");
but all I got were compiler errors.
Any ideas?
Thanks
Best regards,
Alexandru Savescu
P.S. Interested in art? Visit this!
|
|
|
|
|
The syntax for calling your templatized operator is as convoluted as it can get:
m.template operator[]<int>("str"); Unfortunately, VC++ 6.0sp5 does not swallow this (don't know for other MS compilers). An alternative, that also simplifies notation, is as follows:
template <typename T>
struct Type2Type
{
typedef T type;
};
struct MyClass
{
template <typename T> T operator () (const char * x,Type2Type<T>)
{
T t;
...
return t;
}
};
int main()
{
MyClass m;
m("str",Type2Type<int>());
m("str",Type2Type<double>());
return 0;
} This solution alas drops the nice [] notation in favor of simpler parentheses, but I don't think you have a better alternative.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
I have written a dll (which suppose to be used by VB) to wrap/hook to
another third party dll (which cannot be used by VB - source code is not
available). Inside my dll program, I use run-time linking (loadLibrary) to
load the third party dll. When my dll program use the 'GetProcAddress' for a
function, the OS detect an unknown error, and the program is closed
automatically.
example:
typedef int (*ReadInfo)(LPSTR,UINT);
HMODULE hinstLib = LoadLibrary("C:\thirdparty.dll");
if (hinstLib != NULL)
{
ReadInfo proc_Query;
proc_Query = (ReadInfo) GetProcAddress(hinstLib,
"ThirdPart_ReadInfo");
.......
}
Finally, I found out that if I add one line 'Sleep(50)' before the
'GetProcAddress', the program run smoothly.
typedef int (*ReadInfo)(LPSTR,UINT);
HMODULE hinstLib = LoadLibrary("C:\thirdparty.dll");
if (hinstLib != NULL)
{
ReadInfo proc_Query;
Sleep(50);
proc_Query = (ReadInfo) GetProcAddress(hinstLib,
"ThirdPart_ReadInfo");
.......
}
Does any one know the cause of the problem? and how do I solve it in a
better way? (Giving a delay 'Sleep(50)' is not a good soultion)
Thanks!
|
|
|
|
|
Hi all,
When I choose both Auto Arrange & Ascending Sort styles, the List control seem to be not sorted right?
What's wrong? I want my list control is auto arranged and ascending sort based on Item text? How can I do?
Thanks in advanced.
|
|
|
|
|
I want to change the color and the shapes of the scroll bar in the edit box! how should i do! Please help!
If you have the similar sample about it . Please send me!
|
|
|
|
|
You can change the color with the SetSysColors method.
-Nick Parker
|
|
|
|
|
|
QUESTION 1: We are convinced that those students who do well on quizzes will do well in the class. Therefore, we want to compute a final average that is limited to those students who did well on the quizzes. Suppose there is a file named grades.txt in the current directory containing grades for all of the students in ECE 1331. Each line in the file has 9 values, separated by spaces: the first 8 values are the quiz grades for a student, while the 9th value is that same student's final grade in the class. There is one line in the file for each student. Give the MATLAB commands you could put in a script file to read in the file and then compute and display, properly labeled, the average of the final grades made by only those students who had a grade of 85 or more on at least 6 of the 8 quizzes. For example, if only 30 of the students made 85 or more on at least 6 of the 8 quizzes, then you would compute the average of the final grades of just those 30 students and display it. Remember to use loops and not array operations! .
QUESTION 2: Write the Matlab code segment that uses a while loop to determine how long it will take to accumulate $1,000,000 in a bank account if you deposit $5,000 initially and $5,000 at the end of each year; the account pays 8 percent annual interest. The annuity formula is given as follows:
FV = PMT(1+i){(((1+i)^N)-1)/i}
where FV is the final value, PMT is the payment amount you add every period, i is the interest rate in percent per period, and N is the number of periods.
Your code should output the result to the command window. Label which parts of your code are:
pre-loop initialization
loop control
loop body
post-loop cleanup
|
|
|
|
|
Looks like homework.
-Nick Parker
|
|
|
|
|
Ya thanks man. that is really what i needed. I thought may be one of you guys could explain these problems. not tell me what i already know.
|
|
|
|
|
First, you won't get any help here with that kind of attitude. Secondly, this is not a MATLAB forum.
// Afterall I realized that even my comment lines have bugs
|
|
|
|
|
Hi,
I am using the following code to talk to the 'Com1':
int CommPort::CreatePort(_Port port , HANDLE *hCom)
{
DCB dcb;
bool fSuccess;
char *pcCommPort;
CString buffer;
buffer.Format("\\\\.\\%s", port.com);
pcCommPort = new char[buffer.GetLength() + 1];
for(int i = 0; i <= buffer.GetLength(); i++)
*(pcCommPort + i) = buffer.GetAt(i);
*hCom = CreateFile(pcCommPort,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);
delete[] pcCommPort;
if(*hCom == INVALID_HANDLE_VALUE)
return -1;
fSuccess = GetCommState(*hCom, &dcb); //Portkonfiguration mit GetCommState
if(!fSuccess)
return -1;
dcb.BaudRate = port.dcb.BaudRate;//Konfigurationswerte des Ports
dcb.ByteSize = port.dcb.ByteSize;
dcb.Parity = port.dcb.Parity;
dcb.StopBits = port.dcb.StopBits;
dcb.fAbortOnError = true;
dcb.fTXContinueOnXoff = true;
fSuccess = SetCommState(*hCom, &dcb);//Portkonfiguration mit SetCommState
if(!fSuccess)
return -1;
return 1;
}
So far so good. But if I try to read the port by using :
bool CommPort::ReadPort(HANDLE hCom, CString *ScannText)
{
char buf[128];
DWORD lpNumberOfBytesRead;
bool bReturn;
bReturn = ReadFile(hCom, &buf, 128, &lpNumberOfBytesRead, NULL);
buf[lpNumberOfBytesRead] = '\0';
*ScannText = buf;
return bReturn;
}
I don't get the information of the port. I am able to read the port only if I connect to COM1 with hyperterminal. After connecting with hyperterminal the program runs without problems. But if I shut down my PC and start it again I will not be able to run my program. I must always start hyperterminal first.
Is there anybody who can help?
|
|
|
|
|
just try like this
CreateFile(szPortNumber,// Pointer to the name of the port
GENERIC_READ|GENERIC_WRITE , // Access (read-write) mode
0, // Share mode
NULL, // Pointer to the security attribute
CREATE_ALWAYS,//ow to open the serial port
0, // Port attributes
NULL);// Handle to port with attribute to copy
anju
|
|
|
|
|
I had the exact same problem 3 years ago. You can't just read by calling ReadFile. I cann't really remember how I solved it but I will give you a part of an article that I downloaded back then and that luckily for you I still have it. The article is called "Serial Communications in Win32".
Reading
The ReadFile function issues a read operation. ReadFileEx also issues a read operation, but since it is not available on Windows 95, it is not discussed in this article. Here is a code snippet that details how to issue a read request. Notice that the function calls a function to process the data if the ReadFile returns TRUE. This is the same function called if the operation becomes overlapped. Note the fWaitingOnRead flag that is defined by the code; it indicates whether or not a read operation is overlapped. It is used to prevent the creation of a new read operation if one is outstanding.
DWORD dwRead;
BOOL fWaitingOnRead = FALSE;
OVERLAPPED osReader = {0};
// Create the overlapped event. Must be closed before exiting
// to avoid a handle leak.
osReader.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (osReader.hEvent == NULL)
// Error creating overlapped event; abort.
if (!fWaitingOnRead) {
// Issue read operation.
if (!ReadFile(hComm, lpBuf, READ_BUF_SIZE, &dwRead, &osReader)) {
if (GetLastError() != ERROR_IO_PENDING) // read not delayed?
// Error in communications; report it.
else
fWaitingOnRead = TRUE;
}
else {
// read completed immediately
HandleASuccessfulRead(lpBuf, dwRead);
}
}
The second part of the overlapped operation is the detection of its completion. The event handle in the OVERLAPPED structure is passed to the WaitForSingleObject function, which will wait until the object is signaled. Once the event is signaled, the operation is complete. This does not mean that it was completed successfully, just that it was completed. The GetOverlappedResult function reports the result of the operation. If an error occurred, GetOverlappedResult returns FALSE and GetLastError returns the error code. If the operation was completed successfully, GetOverlappedResult will return TRUE.
Note GetOverlappedResult can detect completion of the operation, as well as return the operation’s failure status. GetOverlappedResult returns FALSE and GetLastError returns ERROR_IO_INCOMPLETE when the operation is not completed. In addition, GetOverlappedResult can be made to block until the operation completes. This effectively turns the overlapped operation into a nonoverlapped operation and is accomplished by passing TRUE as the bWait parameter.
Here is a code snippet that shows one way to detect the completion of an overlapped read operation. Note that the code calls the same function to process the data that was called when the operation completed immediately. Also note the use of the fWaitingOnRead flag. Here it controls entry into the detection code, since it should be called only when an operation is outstanding.
#define READ_TIMEOUT 500 // milliseconds
DWORD dwRes;
if (fWaitingOnRead) {
dwRes = WaitForSingleObject(osReader.hEvent, READ_TIMEOUT);
switch(dwRes)
{
// Read completed.
case WAIT_OBJECT_0:
if (!GetOverlappedResult(hComm, &osReader, &dwRead, FALSE))
// Error in communications; report it.
else
// Read completed successfully.
HandleASuccessfulRead(lpBuf, dwRead);
// Reset flag so that another opertion can be issued.
fWaitingOnRead = FALSE;
break;
case WAIT_TIMEOUT:
// Operation isn't complete yet. fWaitingOnRead flag isn't
// changed since I'll loop back around, and I don't want
// to issue another read until the first one finishes.
//
// This is a good time to do some background work.
break;
default:
// Error in the WaitForSingleObject; abort.
// This indicates a problem with the OVERLAPPED structure's
// event handle.
break;
}
}
// Afterall I realized that even my comment lines have bugs
|
|
|
|
|
Hi:
I have recently created a "Win32 Application" project or, more accurately "A Simple Win32 Application".
but i need to use a Worker-Thread in this app so i used AfxBeginThread()
function,but when i want to compile project,i receive the following error:
error C2065: 'AfxBeginThread' : undeclared identifier
Am i doing somethimg wrong? or Is it possible to use this function in
Win32 Application ?
Please Help me.
Best Regards.
|
|
|
|
|
AfxBeginThread() is a method of CWnd , which is only available if you create an MFC application. Use CreateThread() (and related Win32 functions) if you're not using MFC.
/ravi
Let's put "civil" back in "civilization"
http://www.ravib.com
ravib@ravib.com
|
|
|
|
|
Ravi Bhavnani wrote:
AfxBeginThread() is a method of CWnd
No, but of MFC.
Ravi Bhavnani wrote:
Use CreateThread() (and related Win32 functions) if you're not using MFC.
But preferably _beginthreadex() from the C runtime.
--
Human beings, who are almost unique in having the ability to learn from the experience of others, are also remarkable for their apparent disinclination to do so. (Douglas Adams)
|
|
|
|
|
Johann Gerell wrote:
No, but of MFC.
Ooops, you're so right!
/ravi
Let's put "civil" back in "civilization"
http://www.ravib.com
ravib@ravib.com
|
|
|
|
|
I am using a Listview-based window for an application, in which the List ctrl takes up the whole window. I would actually like to put other controls above the ListCtrl portion, but am not sure how to do this. The ListView creates the controls itself, i.e. there is not a resource that I can play with. Is there some place I can adjust and add things to this?
[I am actually using the code from the sample rowlist app that Microsoft supplies. This subclasses CListViewEx from CListView)
|
|
|
|
|
Derive your view class from CFormView instead and add the list control (and other controls) to the view's dialog resource.
/ravi
Let's put "civil" back in "civilization"
http://www.ravib.com
ravib@ravib.com
|
|
|
|
|
Thanks for the response.
I know I could do that, but I would lose all the added functionality of the CListViewEx class, which is why I wanted to use it in the first place.
Tel
|
|
|
|
|
Hi, everyone!
I meet with troubles when reading integers from a file.
Such is my output and source code. I want to know why
the last character can not be read.
Can anyone help?
Output:
--------
10
Error!
--------
Source Code:
--------
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
int a, b;
char space;
ifstream ifs ("c:\\test.txt"); //10 and 100 in this file
istream is = ifs;
is.exceptions (ios::failbit|ios::eofbit);
if (is.good())
{
try {
is >> a;
cout << a << endl;
is >> b;
cout << b << endl;
}catch (ios::failure e)
{
cout << "Error! " << endl;
}
}
return 1;
}
--------
Thanks in advance,
George
|
|
|
|
|
George2 wrote:
I want to know why
the last character can not be read.
Try this, I am not sure what your problem is, however if there isn't a space between the 10 and 100 the int a will store the whole value of 10100 as one value:
#include <fstream.h>
void main()
{
int a, b;
ifstream infile("C:\\test.txt");
if(infile)
{
while(!infile.eof())
{
infile >> a;
infile >> b;
cout << "A: " << a << endl;
cout << "B: " << b << endl;
}
}
}
-Nick Parker
|
|
|
|
|
Thanks, Nick buddy!
I have tried your sample code and it works correct.
But I think my sample code is the same as yours. Why
my sample code is not working? I simply want to use
exception handle machanism in my case.
Can you help?
regards,
George
|
|
|
|
|