2017-05-17 27 views
-1

我主要的代码看起来像这样:“[类别]不名执行文件类型错误

#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 
    ^

张贴的东西似乎是最有意义的。我试过在实现文件中包含头文件,它什么都不做。我试过从头文件中删除包含,并在实现文件中包含头文件,但是这会用未定义的引用错误替换'不命名类型'错误。代码中是否存在阻止编译的内容?

+2

永远不要包含cpp文件。 –

+2

永远不要在这种形式下使用头文件'_CLIENTSOCKET_H' - 只需使用'CLIENTSOCKET_H'。并将标题文件中的标头守卫放在_everything_附近。 –

+1

以下划线后跟大写字母开头的名称将保留用于实施。 *永远不要*在你自己的代码中使用这样的名字(甚至不用于头部守卫)。 –

回答

0

你不应该试图包括我认为ClientSocket.hClientSocket.cpp。请注意,'include'仅仅是一个测试替换,在这种情况下,在定义类本身之前,您正在有效地替换ClientSocket.cpp的内容(包括未声明的ClientSocket类的方法定义)。

此外,ClientSocket.cpp不包括ClientSocket.h,因此或者不访问ClientSocket类定义在所有,或经由其它标题有它,后者的情况下被高度气馁。

1

头文件看起来像这样:

// Definition of the ClientSocket class 

#include "ClientSocket.cpp" <== this definitely looks wrong. 

通常不包括cpp文件。这是其他方式,你ClientSocket.cpp应该包括你的ClientSocket.h

// Implementation of the ClientSocket class 
#include "ClientSocket.h" 
#include "SocketException.h" 

ClientSocket::ClientSocket(std::string host, int port) 
{ 
    ... 

页眉后卫_CLIENTSOCKET_H在ClientSocket.h应该来你有Socket.h之前,也不要使用与一个或两个下划线开头的名称(它们是保留的标识符):

#ifndef CLIENTSOCKET_H 
#define CLIENTSOCKET_H 

#include "Socket.h" 

class ClientSocket : private Socket 
{ 
    ... 
}; 

#endif /* CLIENTSOCKET_H */ 

ClientSocket.h引用std::string,也许这是包括来自Socket.h。如果没有,你应该包括它ClientSocket.h

+0

我可能不清楚,但在.cpp simple中包含.h时,会在编译主代码时给出未定义的引用错误。 – user3564783

+0

@ user3564783你能用这个错误信息更新你的问题吗? – Pavel

+0

@ user3564783您的意思是未定义的链接器引用?您还必须单独编译您的ClientSocket.cpp,并链接生成的ClientSocket.o – Pavel