|
Hello,
In the past I have always used CArray.. I want to get away from it and start using STL's vector.. I bought a book “STL Programming from the ground up” by Herbert Schildt. After reading the first few chapters I realize that it't pretty easy to use vector.. My question is how do I create an array/vector of structures (The book doesn't have any examples that I have found so far). Here’s what I've tried.. and obviously it doesn't work.
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
// My ports struct..
struct s_Ports
{
int iPort; // Default Port #.
int iFilePort; // File Transfer Port #.
};
int main(int argc, char* argv[])
{
s_Ports p;
p.iPort = 20;
p.iFilePort = 21;
// How do I do this??
vector <p,&p> vec(10);
return 0;
}
Whoever said nothing's impossible never tried slamming a revolving door!
|
|
|
|
|
you can define a vector multiple ways, one way (the way i use) is:
say you want a vector of doubles, called 'values'
vector<double> values; //
to add values to the vector, use
<br />
double number = 34.657;<br />
values.pushback(number);
your book should include a lost of STL commands to do, popback, erase, size, etc.
*.*
|
|
|
|
|
try
vector<s_Ports> vec(10);
John
|
|
|
|
|
Doh..
I knew it would be something stupid..
Thanks!
Rob
Whoever said nothing's impossible never tried slamming a revolving door!
|
|
|
|
|
I have the same book. STL will take a while to get used to but once you grasp the concepts it becomes a very valuable programming tool. However, I still do have to look up a thing or two in that book and this seems to happen only when I leave it at home...
John
|
|
|
|
|
I am having problems declairing a vector in my header.. I want it to be a member variable of my dialog class.. I have included <vector> in the header.. but it gives me 18 errors when I try to compile.
in my test dialog header I have..
#include <vector>
class CTestDlg : public CDialog
{
// Construction
public:
CTestDlg(CWnd* pParent = NULL); // standard constructor
vector<int> v_test;
vector<int>::iterator iter_test;
.... etc....
error C2143: syntax error : missing ';' before '<'
error C2501: 'vector' : missing storage-class or type specifiers
etc......
Any idea why it wont compile?
Whoever said nothing's impossible never tried slamming a revolving door!
|
|
|
|
|
At first I did not have a clue but I tried your application and it failed for me in the same way. Then I figured out the problem. Either put
using namespace std;
before your class definition or preface vector with std::
std::vector<int> v_test;
std::vector<int>::iterator iter_test;
John
|
|
|
|
|
Thanks,
Shortly after I posted the problem.. I figured it out..
THanks again for all your help!
Rob
Whoever said nothing's impossible never tried slamming a revolving door!
|
|
|
|
|
You apparently forgot to specify STL namespace try adding
using namespace std;
or
using namespace std::vector;
before the class declaration or changing your vector variables to
std::vector v_test;
std::vector::iterator iter_test;
Regards,
Denis
|
|
|
|
|
Hello
I got a client/server solution where I use IOPORTS in the server.
Sometimes I get 10053 on WSASend/WSARecieve.
Why do I get 10053 in the server, and how do I resolve it?
Im not asking what the exact problem is, but to give me possible reasons that will help me to pinpoint the problem.
"An established connection was aborted by the software in your host computer, possibly due to a data transmission time-out or protocol error. " Doesnt help me that much.
|
|
|
|
|
check out this article, it may have a remedy for you.
*.*
|
|
|
|
|
Well. I think that the problem is that I do not disconnect correctly.
This is my code for disconnect:
if (m_nState == STATE_CONNECTED)
{
struct linger li = {0, 0}; // Default: SO_DONTLINGER
shutdown(m_sdClient, SD_BOTH);
setsockopt(m_sdClient, SOL_SOCKET, SO_LINGER, (char *)&li, sizeof(li));
}
closesocket(m_sdClient);
then i call acceptex:
if (!AcceptEx(*m_psdListen,
m_sdClient,
&m_wsaInBuf.buf[0],
SERVER_WORK_BUFFER_SIZE-(sizeof(sockaddr_in)+16)*2,
sizeof(sockaddr_in) + 16,
sizeof(sockaddr_in) + 16,
&dwBytesRecvd,
&m_ovIn))
{
if (GetLastError() != ERROR_IO_PENDING)
{
m_nState = STATE_DEAD;
closesocket(m_sdClient);
return;
}
}
Comments please.
|
|
|
|
|
oops. I do this before acceptex:
m_sdClient = socket(AF_INET, SOCK_STREAM, 0);
if (m_sdClient == INVALID_SOCKET)
{
m_nState = STATE_DEAD;
WriteLog(LI_HIGH, "Couldnt reinit a socket\n");
return;
}
DWORD dwBytesRecvd;
|
|
|
|
|
The client that connects to you needs to set their own linger options before they connect to you. Same with your server app (you should set linger before you bind the socket)..
I'm not sure if you can set that option after the socket has already been connected or after it accepts a connection..
One other thing (and im not 100% sure) but I dont think you need to call shutdown on a socket when it doesn't linger.. I think you can close the socket right away to free up the socket resoruces..
linger linger;
linger.l_onoff = TRUE;
linger.l_linger = 0;
setsockopt(sock, SOL_SOCKET, SO_LINGER, (char *)&linger, sizeof(struct linger));
if(bind(server,(sockaddr*)&local,sizeof(local))!=0)
{
if(server != INVALID_SOCKET)
closesocket(server);
MessageBox("Error Binding socket", "Socket Error", MB_OK);
}
else
{
if(listen(server,10) != 0)
{
if(server != INVALID_SOCKET)
closesocket(server);
etc.....
}
Hope this gets you pointed in the right direction..
Rob
Whoever said nothing's impossible never tried slamming a revolving door!
|
|
|
|
|
Hi,
Is it possible to use different schema (version number) in the CDocument class ?
I know that I can do this with a different class with IMPLEMENT_SERIAL(), but CDocument is generate with IMPLEMENT_DYNCREATE() who do not use the argument for a different schema.
In fact can I change IMPLEMENT_DYNCREATE() to IMPLEMENT_SERIAL() in CDocument generate class ?
I utilize Visual C++ 6.0.
Thanks,
Claude Gagnon
|
|
|
|
|
The schema numbers in the serialization stuff is basically useless. What I have always done, and has worked well, is to write out my own version number in each class that is serialized. Then I can control things myself. EX:
void SomeClass::Serialize(CArchive & archive)
{
WORD wMagicNum;
WORD wVersion;
CObject::Serialize(archive);
if (archive.IsStoring())
{
archive << (WORD)0x444D;
archive << (WORD)1;
archive << m_cAlias << m_cDLLFileName << m_cDescription;
}
else
{
archive >> (WORD&)wMagicNum;
if (wMagicNum != 0x444D)
{
TRACE("Bad id number in front of a DLLModule item\n");
AfxThrowArchiveException(CArchiveException::generic);
}
archive >> (WORD&)wVersion;
archive >> m_cAlias >> m_cDLLFileName;
if (wVersion > 0)
archive >> m_cDescription;
m_bLoaded = FALSE;
m_hDLL = NULL;
m_pfnCallEntryPoint = NULL;
}
}
onwards and upwards...
|
|
|
|
|
I know I've been on the same problem for a day now, but I just can't get the numbers generated between a min and max value.
I really need to produce a double.
I have changed my function around 20 times and this is my latest try:
I had the function generating a number in the range of (min + 0.0~0.99999), but I need the range to sometimes reach close to the max or at least up to the middle.
the numbers that will be sent to the function are in the range of;
createRandNum(-2.8,4.8);
createRandNum(-15.0,126.0);
As you can see the range varies.
You guys always have the best solutions.
Thanks for the help,
Sj
<br />
double createRandNum(double min, double max){ <br />
<br />
<br />
double randomNum= rand();<br />
<br />
double p = ((max - min) + randomNum / (max-min)); <br />
double d = min + (double)rand() / RAND_MAX;<br />
<br />
<br />
while(count < 100){<br />
printf("%d\nRandomNum is: ", randomNum);<br />
printf("%d\nP is: ", p);<br />
printf("%d\nD is: ", d);<br />
count++;<br />
}<br />
<br />
return d; <br />
<br />
}
|
|
|
|
|
Try:
double result = ((double)rand() / RAND_MAX) * (max - min) + min;
Ryan
Being little and getting pushed around by big guys all my life I guess I compensate by pushing electrons and holes around. What a bully I am, but I do enjoy making subatomic particles hop at my bidding - Roger Wright (2nd April 2003, The Lounge)
Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late - John Nichol "Point Of Impact"
|
|
|
|
|
I get the same number each time.
When I try to cast the rand() function to a double I always get a 0.
I'm about to give up.
I sould probably spend my time writing a class that generates floats or doubles.
It seems like all the random generating classes only generate ints.
thanks for the try,
sj
|
|
|
|
|
Did you set the seed for rand()?
srand( (unsigned)time( NULL ) );
does this pretty well.
--
Ian Darling
|
|
|
|
|
johnstonsk wrote:
When I try to cast the rand() function to a double I always get a 0.
Which indicates something is off kilter. Mixing int and double in this fashion is not likely to produce the desired effect. I suggest rolling your own pseudo random generator.
A simple algorithm looks like:
static unsigned long ulSeed = 1;
#define A 48271L
#define M 2147483647L
double Random( void )
{
ulSeed = (A * ulSeed) % M;
return (double) ulSeed / M;
}
void Init( unsigned long ulVal )
{
ulSeed = ulVal;
}
Note that this could overflow. A fix by L. Schrage for 32-bit machines adds:
#define Q (M / A)
#define R (M % A)
double Random( void )
{
long lTempSeed;
lTempSeed = A * (ulSeed % Q) - R * (ulSeed / Q);
if (lTempSeed >= 0)
ulSeed = lTempSeed;
else
ulSeed = lTempSeed + M;
return (double) ulSeed / M;
}
|
|
|
|
|
Hmmm. That worked perfectly for me. This is the entire program I used to test it:
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int __stdcall WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow)
{
srand((unsigned)time(NULL));
double max = 100.0f;
double min = 0.0f;
for(int i=0; i<20; i++)
{
char buf[128];
sprintf(buf, "%lf", ((double)rand() / RAND_MAX) * (max - min) + min);
MessageBox(NULL, buf, "test", MB_ICONINFORMATION | MB_OK);
}
return 0;
}
Ryan
Being little and getting pushed around by big guys all my life I guess I compensate by pushing electrons and holes around. What a bully I am, but I do enjoy making subatomic particles hop at my bidding - Roger Wright (2nd April 2003, The Lounge)
Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late - John Nichol "Point Of Impact"
|
|
|
|
|
You are correct, it works great.
cheers,
sj
|
|
|
|
|
johnstonsk wrote:
it works great
Good
Ryan
Being little and getting pushed around by big guys all my life I guess I compensate by pushing electrons and holes around. What a bully I am, but I do enjoy making subatomic particles hop at my bidding - Roger Wright (2nd April 2003, The Lounge)
Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late - John Nichol "Point Of Impact"
|
|
|
|
|
Err...
while(count < 100){
printf("%d\nRandomNum is: ", randomNum);
printf("%d\nP is: ", p);
printf("%d\nD is: ", d);
count++;
}
This part of your sample code does nothing other than printing 100 times the same number 'randomNum'... you know that or?
-Dominik
_outp(0x64, 0xAD);
and
__asm mov al, 0xAD __asm out 0x64, al
do the same... but what do they do??
|
|
|
|