I have main code that looks like this:
#include "ClientSocket.h"
#include "SocketException.h"
#include <iostream>
#include <string>
int main ( int argc, char argv[] )
{
std::cout << "" << std::endl;
try
{
ClientSocket client_socket ( "localhost", 30000 );
std::string reply;
try
{
client_socket << "Test message.";
client_socket >> reply;
}
catch ( SocketException& ) {}
std::cout << "We received this response from the server:\n\"" << reply << "\"\n";;
}
catch ( SocketException& e )
{
std::cout << "Exception was caught:" << e.description() << "\n";
}
return 0;
}
A header file that looks like this:
// Definition of the ClientSocket class
#include "ClientSocket.cpp"
#include "Socket.h"
#ifndef _CLIENTSOCKET_H
#define _CLIENTSOCKET_H
class ClientSocket : private Socket
{
public:
ClientSocket ( std::string host, int port );
virtual ~ClientSocket(){};
const ClientSocket& operator << ( const std::string& ) const;
const ClientSocket& operator >> ( std::string& ) const;
};
#endif
And an implementation file that looks like this:
// Implementation of the ClientSocket class
#include "Socket.h"
#include "SocketException.h"
ClientSocket::ClientSocket ( std::string host, int port )
{
if ( ! Socket::create() )
{
throw SocketException ( "Could not create client socket." );
}
if ( ! Socket::connect ( host, port ) )
{
throw SocketException ( "Could not bind to port." );
}
}
const ClientSocket& ClientSocket::operator << ( const std::string& s ) const
{
if ( ! Socket::send ( s ) )
{
throw SocketException ( "Could not write to socket." );
}
return *this;
}
const ClientSocket& ClientSocket::operator >> ( std::string& s ) const
{
if ( ! Socket::recv ( s ) )
{
throw SocketException ( "Could not read from socket." );
}
return *this;
}
I've found that compiling these files causes a raft of compilation errors in the implementation file specifically, no matter what combination of includes and guards I use.
In file included from ClientSocket.h:3:0,
from simple_client_main.cpp:1:
ClientSocket.cpp:6:1: error: 'ClientSocket' does not name a type
ClientSocket::ClientSocket ( std::string host, int port )
^
ClientSocket.cpp:19:7: error: 'ClientSocket' does not name a type
const ClientSocket& ClientSocket::operator << ( const std::string& s ) const
^
ClientSocket.cpp:29:7: error: 'ClientSocket' does not name a type
const ClientSocket& ClientSocket::operator >> ( std::string& s ) const
^
The ones posted are what seemed to make the most sense. I've tried including the header file in the implementation file, which does nothing. I've tried removing the include from the header file and including the header in the implementation file, but this replaces the 'does not name a type' errors with undefined reference errors. Is there something in the code preventing compilation?