|
Hi. I'm trying to create a C++ class with a const static member variable of type valarray, but i do not know how to declare and initialize it. Can anyone help me?
|
|
|
|
|
class foo
{
public:
static const std::valarray < int > m_array;
};
const std::valarray< int > foo::m_array(-1, 5);
int main(int argc, char* argv[])
{
for (int i=0;i<5;i++)
{
printf("%d,", foo::m_array[i]);
}
return 0;
}
Image Toolkits | Image Processing | Cleek
|
|
|
|
|
Thanks for your answer. Two more questions...:
1) I'm trying to define a non static valarray member, stores 15 elements of type double, but without success. All help i found defines valarrays within main(). I tried the same definitions for my class but dose not work. Actually i tried:
class myClass
{
private:
std::valarray<double> m_array;
}
With this the compiler is ok, but when i tryied to asing values to m_array with [] operator, program crashes (actually this is logical because the m_array is of size 1, i think...).
class myClass
{
private:
std::valarray<double> m_array(15);
}
VC++ 2003 comliler generates the following error :
error C2059: syntax error : 'constant'
2) The valarray allocates memory dynamically on the heap or statically? So if i want a huge valarray (say 10000 elements) is this ok:
std::valarray<double> hugeArray(10000);
or i have to allocate the valarray dynamically using a pointer?
Thanks again.
|
|
|
|
|
for #1, you can use C++'s "initialization list" feature on your constructor:
#include < valarray >
class myClass
{
public:
myClass() : m_array(15)
{
}
private:
std::valarray< double > m_array;
};
you can add simple constructor 'calls' to your member ojects right to your own objects c'tor. very handy.
for #2, the elements are allocated dynamically by the valarray's ctor, via the "new" operator.
Image Toolkits | Image Processing | Cleek
|
|
|
|
|
Thanks again. That was very helpfull for me.
while(living)
{
i'm learning;
}
|
|
|
|
|
I am having a problem with CFileDialog. Have a look at this code:
CString strFirst = "C:\\1.txt";
CString strSecond = "C:\\aa\\1.txt";
CopyFile(strFirst, strSecond, FALSE);
This code is simply copying a file 1.txt to a folder named aa present at C root. After I do this and I try to delete folder aa(while my application is still running), it lets me delete it and that's OK. Now have a look at the problem code:
CFileDialog dlg(false);
CString strPathName;
if(dlg.DoModal() == IDOK)
{
strPathName = dlg.GetPathName();
CopyFile(strFirst, strPathName, FALSE);
}
Now, if I select the same folder in CFileDialog and create a file there, it copies it quite well thru CopyFile function. The problem is that If I try to delete folder aa now (with my application still open), it wont let me delete it although I can delete the newly created file inside this folder but deletion of folder is not possible. It gives an error message saying that this folder is in use by some other program.
Am I doing something wrong or is it a bug in CFileDialog. I think it is a bug. Looks like CFileDialog is not releasing the handle of the folder. What is your opinion.
I hated going to weddings. All the grandmas would poke me saying "You're next". They stopped that when I started doing it to them at funerals.
My Articles
|
|
|
|
|
Sometimes CFileDialog will switch directories.
You can call GetCurrentDirectory to see if it has switched to the directory the first file is in (the file you can not delete).
Also, I think it has a slow release cache built into the dialog or some code it utilitizes. If you were to wait a moment, you would find you might be able to delete the files. I say this because, if you watch your debug output after calling a CFileDialog, you will notice information about a secondary thread you did not directly create that terminates after a few seconds. This might be where the caching is occurring. I don't know how to force it to flush immediately.
I think it is a 'design feature' of CFileDialog that can sometimes get in your way.
|
|
|
|
|
What about:
CFileDialog dlg(FALSE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_NOCHANGEDIR);
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
DavidCrow wrote:
What about:
CFileDialog dlg(FALSE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_NOCHANGEDIR);
That doesn't help. This can be reproduced even with this.
CFileDialog dlg(false);
dlg.DoModal();
When Dialog appears, You dont even need to switch the directory. Just enter any filename and then u will not be able to delete that directory. I am stuck here....
I hated going to weddings. All the grandmas would poke me saying "You're next". They stopped that when I started doing it to them at funerals.
My Articles
|
|
|
|
|
This works fine for me:
CFileDialog dlg(FALSE);
dlg.m_ofn.Flags |= OFN_NOCHANGEDIR;
if (dlg.DoModal() == IDOK)
{
CString strPath = dlg.GetPathName();
CopyFile("c:\\somefile.txt", strPath, FALSE);
} Whether I create the folder through Explorer or via CFileDialog , I can remove the folder while the application is running.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
Thanx David, that worked. I was thinking on wrong lines I guess. I thought that OFN_ENABLEHOOK was creating some sort of problem bcoz it was hooking it with a global method and the scope of that global method was program-level. So, I was trying to play with OFN_ENABLEHOOK flag and that was not helping.
Anyways, thanx a lot again.
I hated going to weddings. All the grandmas would poke me saying "You're next". They stopped that when I started doing it to them at funerals.
My Articles
|
|
|
|
|
I had developed the client program under Visual C++ 6.0. I use the Windows socket programming. I compile and build my program using the Win32 Console application.There is no errors and warnings. But after I executing the program, I found that if(argc < 3) {
printf("usage: %s <server> <data1> <data2> ... <datan>\n",argv[0]);
exit(1);
}
the output is usage: %s <server> <data1> <data2> ... <datan> and stop with executing the rest of the program.My work is to send data to the server PC which is under the Linux environment.
The following is my program:Thanks for helping.
#include <stdio.h>
#include <winsock2.h>
#include <windows.h>
#define SERVER_PORT 1500
#define MAX_MSG 100
//int CreatFile(char *szServer, short nPort);
//*CreatFile();
int main (int argc, char *argv[]) {
WORD wVersionRequested = MAKEWORD(1,1);
WSADATA wsaData;
int nRet;
//
// Initialize WinSock and check the version
//
nRet = WSAStartup(wVersionRequested, &wsaData);
if (wsaData.wVersion != wVersionRequested)
{
fprintf(stderr,"\n Wrong version\n");
return 0;
}
int sd, rc, i;
struct sockaddr_in localAddr, servAddr;
struct hostent *h;
if(argc < 3) {
printf("usage: %s <server> <data1> <data2> ... <datan>\n",argv[0]);
exit(1);
}
// The gethostbyname() function is used when we have the DNS name of our server
h = gethostbyname(argv[1]);
if(h==NULL) {
printf("%s: unknown host '%s'\n",argv[0],argv[1]);
exit(1);
}
//
// Fill in the address structure
//
servAddr.sin_family = h->h_addrtype;
memcpy((char *) &servAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
servAddr.sin_port = htons(SERVER_PORT);
/* create socket */
sd = socket(AF_INET, SOCK_STREAM, 0);
if(sd<0) {
perror("cannot open socket ");
exit(1);
}
/* bind any port number */
localAddr.sin_family = AF_INET;
localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
localAddr.sin_port = htons(0);
rc = bind(sd, (struct sockaddr *) &localAddr, sizeof(localAddr));
if(rc<0) {
printf("%s: cannot bind port TCP %u\n",argv[0],SERVER_PORT);
perror("error ");
exit(1);
}
/* connect to server */
rc = connect(sd, (struct sockaddr *) &servAddr, sizeof(servAddr));
if(rc<0) {
perror("cannot connect ");
exit(1);
}
for(i=2;i
|
|
|
|
|
I had developed the client program under Visual C++ 6.0. I use the Windows socket programming. I compile and build my program using the Win32 Console application.There is no errors and warnings. But after I executing the program, I found that if(argc < 3) {
printf("usage: %s <server> <data1> <data2> ... <datan>\n",argv[0]);
exit(1);
}
the output is usage: %s <server> <data1> <data2> ... <datan> and stop with executing the rest of the program.My work is to send data to the server PC which is under the Linux environment.
The following is my program:Thanks for helping.
#include <stdio.h>
#include <winsock.h>
#include <windows.h>
#define SERVER_PORT 1500
#define MAX_MSG 100
//int CreatFile(char *szServer, short nPort);
//*CreatFile();
int main (int argc, char *argv[]) {
WORD wVersionRequested = MAKEWORD(1,1);
WSADATA wsaData;
int nRet;
//
// Initialize WinSock and check the version
//
nRet = WSAStartup(wVersionRequested, &wsaData);
if (wsaData.wVersion != wVersionRequested)
{
fprintf(stderr,"\n Wrong version\n");
return 0;
}
int sd, rc, i;
struct sockaddr_in localAddr, servAddr;
struct hostent *h;
if(argc < 3) {
printf("usage: %s <server> <data1> <data2> ... <datan>\n",argv[0]);
exit(1);
}
// The gethostbyname() function is used when we have the DNS name of our server
h = gethostbyname(argv[1]);
if(h==NULL) {
printf("%s: unknown host '%s'\n",argv[0],argv[1]);
exit(1);
}
//
// Fill in the address structure
//
servAddr.sin_family = h->h_addrtype;
memcpy((char *) &servAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
servAddr.sin_port = htons(SERVER_PORT);
/* create socket */
sd = socket(AF_INET, SOCK_STREAM, 0);
if(sd<0) {
perror("cannot open socket ");
exit(1);
}
/* bind any port number */
localAddr.sin_family = AF_INET;
localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
localAddr.sin_port = htons(0);
rc = bind(sd, (struct sockaddr *) &localAddr, sizeof(localAddr));
if(rc<0) {
printf("%s: cannot bind port TCP %u\n",argv[0],SERVER_PORT);
perror("error ");
exit(1);
}
/* connect to server */
rc = connect(sd, (struct sockaddr *) &servAddr, sizeof(servAddr));
if(rc<0) {
perror("cannot connect ");
exit(1);
}
for(i=2;i
|
|
|
|
|
So does this reproduce the problem:
int main( int argc, char *argv[] )
{
if (argc < 3)
{
printf("usage: %s ... \n", argv[0]);
exit(1);
}
return 0;
}
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
Thanks for your reply. Now I try to execute this program to send the the data to the Linux computer server which using the BSD socket.Can I doing this just on the console application mode in VC++6.0 without doing the graphical User interface first? What of the steps of doing this?
Anyway, can I know why is its execution stop at usage: %s ... without doing the send()command?
Hoping for your help.Thank you.
|
|
|
|
|
yanyu81 wrote:
Anyway, can I know why is its execution stop at usage: %s ... without doing the send()command?
Because that's the way you've written the code. If you do not want it to stop, you must remove the call to exit() .
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
Question1:
After I remove all the exit(), I found that the output becomes
"usage: %s ... "
cannot connect:No error
Press any key to continue_
How can I fix this code to execute the whole program?
Question2: Can I just execute this program in the Win32 Console Application under the VC++6.0 to send the data to the Linux computer server which using the BSD socket.Can I doing this just on the console application mode in VC++6.0 without doing the graphical User interface first?Like using DOS to type the location of the executable file? What of the steps of doing this?
Thanks.
|
|
|
|
|
If the printf() statement is being called, it's because the if condition that precedes it is evaluating to true.
When sending data via sockets, it matters not what the interface is. Whether it's a console application or a GUI application, those are merely for the user's benefit, not the programs.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
Hi. I was wondering if anyone could help me on this: I have to figure out what is the encryption algorithm used to encrypt several words. I have the aplication which can encrypt for me any word I want, so I can build up a dictionary, (clear text <==> encrypted text). This is my project for my encryption course. If anyone could help on this I would be greatful ...
|
|
|
|
|
Hi,
I store texts in file in following unicode representation :
XXX;YYYY;
This string
& # 1058; & #1077; & #1089; & #1090;
,for example , means "Test" in russian.
My project is built as unicode and i can successfully show this string if i do following:
CString strMsg;
strMsg = (unsigned short) 1058;
strMsg += (unsigned short) 1077;
strMsg += (unsigned short) 1089;
strMsg += (unsigned short) 1090;
TextOutW(....,..,..,..,strMsg,..,..);
My question is : what should i do in order to init string like this :
Code:
CString strText = "& # 1058; & # 1077; & #1089; & #1090;";
so that it will be "treated" as unicode string ? I tried
_T("& # 1058; & # 1077; & #1089; & #1090;") , but it doesn't help..
NOTE - I USED SPACES IN ABOVE POST INSIDE UNICODE STRINGS ON PURPOSE TO PREVENT THEM TO APPEAR AS RUSSIAN CHARS
|
|
|
|
|
unclear question:
CString strText = "& # 1058; & # 1077; & #1089; & #1090;";
do you mean values in the string:
first byte is: '&'
second byte is: '#'
next 2 bytes are: 1058
etc?
includeh10
|
|
|
|
|
No, I mean unicode representation of string.
Т = 'T' , е = 'e' , с = 's' ; т = 't' (in russian)
|
|
|
|
|
I'm quiet a beginner and it's kind of difficult to discribe my problem. Let's try. I'm programming a SDI application, with the possibilty to run this programm on the console. Now my question: How can i output text to the console? I've already tried WriteConsole, but this seems to work only if you allocate a new console. But i want to write to the console i'm starting the programm from. Can anyone give me a hint?
|
|
|
|
|
Yes, you're right, you need to create a console for output if you're using WriteConsole().
If you want to output it to the caller's console, then you need to create the output console HANDLE in the caller, starting program, thread, fibre, whatever, and pass the output console HANDLE to whatever you're trying start up (How you do that is up to you - commandline args, thread args - etc, etc). Then you should be able to use WriteConsole() using the handle passed to it.
Now, if I may comment.
The reason that SDI, MDI, and any other windowing programmes exist is that they expect user input, even if it is only a mouse click, or there are special circumstances for output - just have a look at all the processes running on your computer - guaranteed that most are background tasks with no user interface or the user interface is mediated by a second programme.
If you want to write a 'console' programme then you don't need all the baggage that comes with windowing.
If you want to write a programme that needs even moderately complicated user interaction, then think about the model you're using.
|
|
|
|
|
Thanks first! I'll try to follow your advice with the handle.
The reason for using a SDI is, that in the end there shall be a programm, that can be run either in console mode by passing the parameters directly or in GUI mode, when there are no parameters given in the command line. But i'm just beginning this project.
|
|
|
|
|