我主要的代码看起来像这样:“[类别]不名执行文件类型错误
#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;
}
头文件看起来像这样:
// 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
和实施文件看起来像这样:
// 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;
}
我发现,编译这些文件会导致编译错误的木筏在implemen特别是,无论我使用的包含和警卫的组合如何。
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
^
张贴的东西似乎是最有意义的。我试过在实现文件中包含头文件,它什么都不做。我试过从头文件中删除包含,并在实现文件中包含头文件,但是这会用未定义的引用错误替换'不命名类型'错误。代码中是否存在阻止编译的内容?
永远不要包含cpp文件。 –
永远不要在这种形式下使用头文件'_CLIENTSOCKET_H' - 只需使用'CLIENTSOCKET_H'。并将标题文件中的标头守卫放在_everything_附近。 –
以下划线后跟大写字母开头的名称将保留用于实施。 *永远不要*在你自己的代码中使用这样的名字(甚至不用于头部守卫)。 –