Skip to main content
gamedev.net
🔒 Locked

.h and .cpp problem [SOLVED]

Started by yukit0 4 replies 1.1k views
Original Post
yukit0
yukit0
I've made a class and it's split up into a .h and a .cpp file. However, when I include the .h, the .cpp does not get used for the function impementations, and I have to add all the code in the .h to make it work. Does anyone know how i can solve this? Im using Dev-C++, my friend tried on his system (using Anjuta) and it worked fine. [Edited by - yukit0 on March 21, 2005 4:46:04 PM]
Fruny
Fruny
a) Are you compiling and linking the cpp file?
b) Show your code.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
yukit0
yukit0
Linking? Doesn't Dev-C++ do that automaticaly? Worked fine in the past...


(note that theres a problem with this code: the socket doesnt seem to close, the adress remains "in use" after shutting down, if you could solve that one aswell, id be most gratefull)

heres sockity.h:
#ifdef _WIN32#include "winsock.h"#endif#ifdef _UNIX#include "sys/socket.h"#include "netinet/in.h"#include "netdb.h"#endif#include "stdio.h"#include "stdlib.h"using namespace std;/* Status constants */const int CLOSED = 0, CONNECTED = 1, LISTENING =2; class sockity {      public:            /* Almighty error handling, ripped from Rip7's code */            class ErrorHandling	        {                public:        		enum TypeOfError                    { WinsockInit, Create, Bind, UnknownHost, Connect,                      Linger };                ErrorHandling (TypeOfError S):Error (S)		        {		        }		        TypeOfError GiveError ()                {                return Error;		        }	            private:                TypeOfError Error;            };             /* Constructor */             sockity(unsigned short int port);             /* Destructor */             ~sockity();             /* Public Functions */             void conn(const char *hostname, unsigned short int port);             int getstatus() { return status; }      private:             /* Socket name */             sockaddr_in name;             /* Host socket name */             sockaddr_in hostname;             /* The almighty socket */             int sock;             /* Socket status */             int status;             /* Error code from socket */             //extern int h_errno;};


and sockity.cpp:
#ifdef _WIN32#include "winsock.h"#endif#ifdef _UNIX#include "sys/socket.h"#include "netinet/in.h"#include "netdb.h"#endif#include "stdio.h"#include "stdlib.h"#include "sockity.h"#include <iostream>using namespace std;sockity::sockity(unsigned short int port) {    #ifdef _WIN32    /* WIN32 socket initialization code */    WSADATA wsadata;    int rc = WSAStartup(2, &wsadata);    if(rc) {        throw ErrorHandling (ErrorHandling::WinsockInit);    }    #endif    /* Create the socket */    sock = socket(PF_INET, SOCK_STREAM, 0);    if (sock < 0)    {        throw ErrorHandling (ErrorHandling::Create);    }    /* Give the socket a name */    name.sin_family = AF_INET;    name.sin_port = htons(port);    name.sin_addr.s_addr = htonl(INADDR_ANY);    if (bind(sock, (sockaddr*) &name, sizeof(name)) < 0)    {        throw ErrorHandling (ErrorHandling::Bind);    }    linger *lngr;    lngr->l_onoff = 0;    lngr->l_linger = 0;    if (0 > setsockopt(sock, SOL_SOCKET, SO_LINGER, (char *)lngr, sizeof(*lngr)))    {        throw ErrorHandling (ErrorHandling::Linger);    }    status = CLOSED;}sockity::~sockity() {    #ifdef _WIN32        closesocket(sock);    #endif    #ifdef _UNIX        close(sock);    #endif}void sockity::conn(const char *host, unsigned short int port) {    hostent *hostinfo;    hostname.sin_family = AF_INET;    hostname.sin_port = htons(port);    unsigned long int ip;    ip = inet_addr(host);    hostname.sin_addr.s_addr = ip;    if (0 > connect(sock, (sockaddr *) &hostname, sizeof(hostname)))    {        if ( h_errno == 10049 ) {   // Host not found, try resolving it as a hostname            hostinfo = gethostbyname(host);            if (hostinfo == NULL)            {                throw ErrorHandling (ErrorHandling::UnknownHost);            }            hostname.sin_addr.S_un.S_un_b.s_b1 = (unsigned char)hostinfo->h_addr_list[0][0];            hostname.sin_addr.S_un.S_un_b.s_b2 = (unsigned char)hostinfo->h_addr_list[0][1];            hostname.sin_addr.S_un.S_un_b.s_b3 = (unsigned char)hostinfo->h_addr_list[0][2];            hostname.sin_addr.S_un.S_un_b.s_b4 = (unsigned char)hostinfo->h_addr_list[0][3];            if (0 > connect(sock, (sockaddr *) &hostname, sizeof(hostname)))            {                printf("Connection error: %d\n", h_errno);                throw ErrorHandling (ErrorHandling::Connect);            }        } else {            printf("Connection error: %d\n", h_errno);            throw ErrorHandling (ErrorHandling::Connect);        }    }    status = CONNECTED;}
Drew_Benton
Drew_Benton
Quote:
Original post by yukit0
Linking? Doesn't Dev-C++ do that automaticaly? Worked fine in the past...


Only if you add the .cpp file to the project workspace. After just adding in:
int WINAPI WinMain (HINSTANCE hThisInstance,                    HINSTANCE hPrevInstance,                    LPSTR lpszArgument,                    int nFunsterStil){        return 0;}
To the .cpp file, everything compiled fine. So, as Fruny suggested, check to make sure the .cpp file is part of your work space in Dev-CPP. It should look something like this:



Of course your files are named different.
Zahlman
Zahlman
This is one of the reasons I don't like IDEs in general. :s
yukit0
yukit0
Thank you so much :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.