Thread: What's wrong with my funtion :(

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    241

    What's wrong with my funtion :(

    I am building a server via Johnnie's Winsock Tutorial and I am trying to get this function to work
    Code:
    #include <vector>
    char *readLine(SOCKET TheSocket)
    {
    	vector<char> TheVector;
    	char buffer;
    	char *pChar;
    	int BytesRecieved;
    	while(true)
    	{
    		BytesRecieved=recv(TheSocket,&buffer,1,0);
    		if(BytesRecieved<=0)
    		{
    			MessageBox(0,"recv() returned nothing.","SocketIndication",MB_OK);
    			return NULL;
    		}
    		switch(buffer)
    		{
    			case backKey:
    				if(TheVector.size()<0)
    					TheVector.pop_back();
    			break;
    			case endStr:
    				pChar=new char[TheVector.size()+1];
    				memset(pChar,0,TheVector.size()+1);
    				for(int f=0;f<TheVector.size();f++)
    				{
    					pChar[f]=TheVector[f];
    				}
    			break;
    			default:
    				TheVector.push_back(buffer);
    			break;
    		}
    	}
    }
    But for some reason it won't compile, why? It seems fine to me. Any suggestions?

  2. #2
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    Oh yeah, I am using Dev-C++ if that is any help.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > But for some reason it won't compile, why?
    Well how about posting your error messages?

    > if(TheVector.size()<0)
    My guess is you meant > 0

    Also, where is the
    return pChar;

    > memset(pChar,0,TheVector.size()+1);
    This is a very expensive way of setting the last byte to '\0', given that the loop which follows is just going to overwrite them all anyway.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    It's saying vector undeclared and stuff like that, I am making that since the tutorial said that was a good way to know how large the item is that is being sent

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    29
    using namespace std;

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    use std::vector
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    Thanks I'll go check that

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    alright, now I'm getting [Build Error] [My"] Error 1

  9. #9
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    Link to the libws2_32.a file. If you have, do it again. I don't know why you have to keep linking sometimes, but it seems you do a lot of the time.
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM