Welcome! Log In Create A New Profile

Advanced

error: expected initializer before 'FYL_Socket'

Posted by One2Remember 
error: expected initializer before 'FYL_Socket'
March 11, 2011 10:12PM
Here is the code



Quote



#define BUFFER_SIZE 4096

class FYL_Socket
{

private:
s32 fd_socket;

public:
FYL_Socket(std::string*, u32);

s32 createSocket();
bool connect(std::string*, u32);
void close();
u32 write(std::string*);
u32 write(char*);
std::string read();



};







FYL_Socket::FYL_Socket(std::string *host, u32 port)
{

createSocket();
connect(host, port);

}





s32 FYL_Socket::createSocket()
{

fd_socket = net_socket(AF_INET, SOCK_STREAM, 0);
return (fd_socket);

}







bool FYL_Socket::connect(std::string *host, u32 port)
{

if (fd_socket < 0)
return (false);

if ((port < 0) || (port > 65535))
return (false);

struct hostent *hostinfo = net_gethostbyname((char *) host->c_str());

if (hostinfo == NULL)
return (false);

struct sockaddr_in serv_addr;

bzero((char *) &serv_addr, sizeof(serv_addr));

serv_addr.sin_family = AF_INET;
bcopy((char *) hostinfo->h_addr_list[0], (char *) &serv_addr.sin_addr.s_addr, hostinfo->h_length);
serv_addr.sin_port = htons(port);

if (net_connect(fd_socket, (sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
return (false);

return (true);

}









void FYL_Socket::close()
{

net_close(fd_socket);

}









u32 FYL_Socket::write(std::string* message)
{

return (write((char *) message->c_str()));

}









u32 FYL_Socket::write(char *message)
{

if (fd_socket < 0)
return (0);

if (message == NULL)
return (0);

char *buffer = new char [BUFFER_SIZE];
u32 count = 0, size = strlen(message);

while (count < size)
{

bzero(buffer, BUFFER_SIZE);
strncpy(buffer, message, BUFFER_SIZE);

if (net_write(fd_socket, buffer, BUFFER_SIZE) < 0)
return (count);

count += BUFFER_SIZE;
message += BUFFER_SIZE;

}

return (size);

}










void std::string FYL_Socket::read()
{

std::string message;

if (fd_socket < 0)
return (message);

char *buffer = new char [BUFFER_SIZE];
bzero(buffer, BUFFER_SIZE);

while (net_read(fd_socket, buffer, BUFFER_SIZE) != 0)
{

message.append(buffer);
bzero(buffer, BUFFER_SIZE);
}

message.append(buffer);
return (message);

}





Here is my compile output



Quote


> "make"
fmylife.cpp
In file included from c:/Users/fdfdg/Desktop/fmylife/FMyLife/source/fmylife.cpp:9:0:
c:/Users/fdfdg/Desktop/fmylife/FMyLife/source/fyl_network.h:114:18: error: expected initializer before 'FYL_Socket'
make[1]: *** [fmylife.o] Error 1
"make": *** [build] Error 2

> Process Exit Code: 2
> Time Taken: 00:01






the error is on this line


Quote



void std::string FYL_Socket::read()



What am I missing?



Edited 5 time(s). Last edit at 03/11/2011 11:05PM by One2Remember.
Re: error: expected initializer before 'FYL_Socket'
March 12, 2011 12:23AM
void std::string FYL_Socket::read()
std::string read()
u32 FYL_Socket::write(char *message)
u32 write(char*);
See the difference? (hint, it's the void, doesn't belong there, it's like saying "int int main();")

Also, normally you don't place implementation of functions in the header file.
Re: error: expected initializer before 'FYL_Socket'
March 12, 2011 12:40AM
Thanks for your help Daid, it's much appreciated, so I tried this, is it right now?

Quote


u32 std::string FYL_Socket::read()
{

std::string message;

if (fd_socket < 0)
return (message);

char *buffer = new char [BUFFER_SIZE];
bzero(buffer, BUFFER_SIZE);

while (net_read(fd_socket, buffer, BUFFER_SIZE) != 0)
{

message.append(buffer);
bzero(buffer, BUFFER_SIZE);
}

message.append(buffer);
return (message);

}






But my output is still

Quote



> "make"
fmylife.cpp
In file included from c:/Users/fdfdg/Desktop/fmylife/FMyLife/source/fmylife.cpp:9:0:
c:/Users/fdfdg/Desktop/fmylife/FMyLife/source/fyl_network.h:114:17: error: expected initializer before 'FYL_Socket'
make[1]: *** [fmylife.o] Error 1
"make": *** [build] Error 2


initializer is still expected before 'FYL_Socket' :(

I don't get it, I have googled this error a million times.... I won't quit though....



Edited 9 time(s). Last edit at 03/13/2011 06:54PM by One2Remember.
Re: error: expected initializer before 'FYL_Socket'
March 13, 2011 03:13PM
Daid, Tantric, giantpune, tueidj ..... anybody....


Am I missing a semicolon somewhere? I read that this is a common cause for this type of error. If it's a missing semicolon it's invisible to me... I've checked and rechecked...



Edited 4 time(s). Last edit at 03/13/2011 07:00PM by One2Remember.
Sorry, only registered users may post in this forum.

Click here to login