|
Hi all,
This is my first post; I'm new to C++ so go easy on me.
I have two classes; and their functions include code that refer to the other:
i.e: Class X has code that uses a variable/function from Class Y and vice-versa. The classes are completely different, so it would not help to derive one off the other.
In Header.h:
------------------------------------------------------------
class X {
public:
int myint;
void doit(void);
};
void X::doit(void) {
printf("CY.myint = %i", CY.myint);
}
class Y {
public:
int myint;
void doit(void);
};
void Y::doit(void) {
printf("CX.myint = %i", CX.myint);
}
------------------------------------------------------------
In Main.CPP:
------------------------------------------------------------
#include "stdio.h"
#include "Header.h"
int main(void) {
class X CX;
class Y CY;
CX.myint = 100;
CY.myint = 200;
CX.doit();
CY.doit();
return 1;
}
------------------------------------------------------------
How can I get these classes to work together?
I can't put an 'extern class Y' before the 'class X' declaration, so how can I let the compiler know that the other class exists and will be declared?
Thanks!
Mike
|
|
|
|
|
Xds wrote:
int main(void)
{
class X CX;
class Y CY;
CX.myint = 100;
CY.myint = 200;
CX.doit();
CY.doit();
return 1;
}
I'm not sure about this but isn't it "wrong" to write 'class X CX'?
Isn't 'X CX' the right way!?
I can be wrong!!!!!!
------------------------------------
Rickard Andersson, Suza Computing
ICQ#: 50302279
I'm from the winter country SWEDEN!
------------------------------------
|
|
|
|
|
Pretty sure it works either way, but in any case, that's not the problem.
Thanks!
Mike
|
|
|
|
|
You need to use a forward declaration of the class.
class Y ;
class X
{
void DoIt(Y& y) ;
} ;
class Y
{
} ;
You also cannot print out the content of CY in CX unless you pass a reference / pointer / copy of the object you want to print in as a parameter. The CX in main does not know anything about the CY:
#include "stdio.h"
#include "Header.h"
int main(void)
{
X CX;
Y CY;
CX.myint = 100;
CY.myint = 200;
CX.doit(CY);
CY.doit();
return 1;
}
Roger Allen
Sonork 100.10016
If I'm not breathing, I'm either dead or holding my breath.
A fool jabbers, while a wise man listens. But is he so wise to listen to the fool?
|
|
|
|
|
That code wont't compile because in your doit methods you reference hard-coded instances.
Michel
If I am wrong or said something stupid, I apologize in advance
|
|
|
|
|
Do you know an activex control able to show .gif files?
rechi
|
|
|
|
|
first, you need a GIF/LZW license. see www.unisys.com.
-c
"What the hell are you getting so upset about? I thought you didn't believe in God".
"I don't," she sobbed, bursting violently into tears, "but the God I don't believe in is a good God, a just God, a merciful God. He's not the mean and stupid God you make Him out to be".
-- Joseph Heller
Smaller Animals Software, Inc.
|
|
|
|
|
Use image.ocx...
Regards...
A couple of month agou in MSDN magazine, Paul Dilascia wrote a class for display those type of files....
Regards
Carlos Antollini.
Sonork ID 100.10529 cantollini
|
|
|
|
|
I'm pretty sure there is an article here on CP about that!
------------------------------------
Rickard Andersson, Suza Computing
ICQ#: 50302279
I'm from the winter country SWEDEN!
------------------------------------
|
|
|
|
|
Thank you all, the problem is now solved!
Thanks1
rechi
|
|
|
|
|
hi,
I want the arabic character writing in the Edit Text and Static Text in the English Windows (No Arabic Windows) . Arabic fonts is best writing but it is wrong connect the arabic character.. (in the MFC od Embedded VC++)..
I'm not good speak englih.. Sorry all!
Please help me.
best regads..
Mesut
mgencer99@hotmail.com
MFC
|
|
|
|
|
You need to use Unicode Strings, and need to work in a PC with that language. Never but never save the project in a pc, with another idiom.
I worked in a multy language project, And I nedded to save same parts of the project in OS that supports that languajes, like Arabic, Hebrew and Russian...
Carlos Antollini.
Sonork ID 100.10529 cantollini
|
|
|
|
|
You need to set the RTL proprti in the Edit and in the static controls, the same for the combo boxes
Carlos Antollini.
Sonork ID 100.10529 cantollini
|
|
|
|
|
Dose anybody know where I can find demonstration packages programmed in Visual c++, which show you the encryption of eg transposition cipher. I can find alot of help in Java but sadly none in Visual c++
Any suggestions? urls? Or people I can e-mail?
thanks
|
|
|
|
|
I am well and truely stumped. I have created a progress bar within a formview and I am trying to access it via a totally different class and getting all sort of errors.
Here is the code: -
percentage=int(((float)i/(float)(mrecords*4))*100.0);
str.Format("Mailsort is %d%% complete (Coding - %s)", percentage, currentmailsortfile);
m_progress.SetWindowText(str);
m_progress.SetPos(percentage);
and the progress control is created in the IYIView.h header file as follows: -
//{{AFX_DATA(CIYIView) enum { IDD = IDD_IYI_FORM };
CTextProgressCtrl m_progress;
//}}AFX_DATA
I am trying to access the view header file (IYIView.h) in order to create an instance of the class and access the progress control (m_progress) but I am getting the following errors on compliation but don't know how to get round it without effecting the rest of the program.
d:\my projects\iyi\iyiview.h(23) : error C2065: 'IDD_IYI_FORM' : undeclared identifierd:\my projects\iyi\iyiview.h(23) : error C2057: expected constant expression
d:\my projects\iyi\iyiview.h(115) : error C2143: syntax error : missing ';' before '*'
d:\my projects\iyi\iyiview.h(115) : error C2501: 'CIYIDoc' : missing storage-class or type specifiers
d:\my projects\iyi\iyiview.h(115) : error C2501: 'GetDocument' : missing storage-class or type specifiers
MSDN is no help, so do you have any ideas?!?
|
|
|
|
|
Check this.
Mazy
"The path you tread is narrow and the drop is shear and very high,
The ravens all are watching from a vantage point near by,
Apprehension creeping like a choo-train uo your spine,
Will the tightrope reach the end;will the final cuplet rhyme?"Cymbaline-Pink Floyd
|
|
|
|
|
I don't need to access the whole view but simply the progress bar to update from a different class. Is it really necessary to do all of this code just to pass a value to a control?
|
|
|
|
|
James P wrote:
Is it really necessary to do all of this code just to pass a value to a control?
If you wanna use your view you have to do something like that.There are another way exist but I didn't test.Read the messages at the bottom of that article,they are easier.
Mazy
"The path you tread is narrow and the drop is shear and very high,
The ravens all are watching from a vantage point near by,
Apprehension creeping like a choo-train uo your spine,
Will the tightrope reach the end;will the final cuplet rhyme?"Cymbaline-Pink Floyd
|
|
|
|
|
I am driven to the wall. Can anyone help me?
I have a server and a client both use TCP/IP socket. The server runs Vxworks while the client runs Windows 2000. the requrirement is that the server can have one client only at a time, so when I initialize the server socket, I say:
nRet = listen(ListenSocket, 1);
But still, when I run two clients to test it, both can connect()!!!
And when I check the messages received by the server,
if ( FD_ISSET(ListenSocket, &ReadSet) && ClientSocket == 0)
HandleNewConnection();
else
logMsg("don't accept\n", 1, 2, 3, 4, 5, 6);
I will see that: after the first client successfully connected to the server (ClientSocket != 0 then), I let the second client try to connect to the server. The client will tell me its connect() okay, while the server will say "don't accept\n" all the time until the second client close the connection.
What's the use of the MAX_CONNECTION = 1 in my listen() then??
Or if I over look something?
any help is highly appreciated!
|
|
|
|
|
I have come across this before.
First off, I beleive the parameter to listen() is the queue size, so it is valid to be handling 1 call and having one in the queue.
Is there any reason you can only have one client? Typical pattern would be to have a server thread for each client.
One way I have seen your problem solved is to close the accepting socket straight after the first call to accept. The 2nd client will the get a connection refused error. When you server has finished with the 1st client, it can then re-open the accepting socket.
HTH Rob.
|
|
|
|
|
Thank you, Bob, (why your signature is Rob though? )
I tried to shutdown and close the new connection right after accept(). But the client will not receive any feedback from its connect(). But it couldn't send or receive anything either, since there is no connectin at all. Only he (client) doesn't know it. Maybe I should implement some software handshake here to confirm the connection? But there should be some implemented method I could use..
The reason I want only one connection is that the server is running a real-time application, and it should not be disturbed by too many clients, afraid that it would slow down the server's performance. After all, the client's functionality is to record down the server's status for off-line analysis, so one client is enough. I did a version which can accept up to 10 connection, but my collegue said it's a waste of effort.
|
|
|
|
|
You need to close the accepting socket.
eg pseudocode...
SOCKET s1 = socket()
SOCKET s2 = accept(s1,...)
// Now client one is connected to s2
closesocket(s1)
// Now no more clients can connect
// you need to re-open s1 should the s2 connection fail for any reason.
Bob/Rob are both short forms of Robert. At work I am Bob, at Home Rob. I get confused as well sometimes!!
|
|
|
|
|
Bob Groves wrote:
Bob/Rob are both short forms of Robert.
I see. there are so many short forms of English name.
Yeah, I did the same thing as you suggested. The code is :
SOCKET listenSocket;
listen(listenSocket, 1);
while(1)
{
BuildSelectList();
select(HighSocket+1, &ReadSet, NULL, NULL, &timeout);
if ( FD_ISSET(ListenSocket, &ReadSet) )
HandleNewConnection();
else if ( FD_ISSET(ClientSocket, &ReadSet) )
ReadSocket(ClientSocket);
}
in HandleNewConnection():
int newSd;
if ( ClientSocket != 0 )
{
newSd = accept(ListenSocket, NULL, NULL);
shutdown(newSd, 2);
close(newSd);
return;
}
although I closed the second connection, the client running on Windows 2000 didn't receive any error message from its call of connect(). what should I do?
|
|
|
|
|
I've never used the FD_??? macros, but I just tried the following in the 10 minutes before I went home, and it works OK.
I tested it by running "telnet 127.0.0.1 1001" from the DOS prompt twice. The 1st connects, the 2nd doesn't.
I hope it works for you too.
Gotta run now.....
Bob.
#include <winsock.h>
void StartupWinSock()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(2, 0);
err = ::WSAStartup(wVersionRequested, &wsaData);
}
void CSimplesockDlg::OnButton1()
{
StartupWinSock();
SOCKET s1 = socket(PF_INET, SOCK_STREAM, 0);
if (s1 == INVALID_SOCKET)
{
AfxMessageBox("error creating socket");
return;
}
struct sockaddr_in serv_addr;
memset((char *)&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(1001);
if (bind(s1, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0)
{
AfxMessageBox("error binding socket");
return;
}
if (listen(s1, 1) == SOCKET_ERROR)
{
AfxMessageBox("error listening on socket");
return;
}
SOCKET s2 = accept(s1, 0, 0);
if (s2 == INVALID_SOCKET)
{
if (WSAGetLastError() != WSAEWOULDBLOCK)
{
AfxMessageBox("error accepting socket");
return;
}
}
closesocket(s1);
// DO stuff with s2
// No new clients should be able to connect
// The Sleep is just so that my simple server does not immediately close down, in real life, you would do you code here.
Sleep(60000);
}
|
|
|
|
|
I tried this. But what would you do after the connection between server and the first client drop? How do you make s1 listen again? Would it be a must to go through the cycle of
socket, bind, listen?
TIA!
|
|
|
|