![]() |
#2
peach54602013-04-16 15:51
|

//--------------------以下是bpn_conn.hpp中的内容----------------
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string>
#include <cstring>
#include <arpa/inet.h>
namespace BpNet{
class bpn_conn;
}
class bpn_conn
{
private:
int _port;
string _addr;
public:
bpn_conn(string addr, int port)
{
_addr = addr;
_port = port;
}
};
-------------------以下是main.cpp中的内容-----------------
#include <iostream>
#include "bpn_conn.hpp"
using namespace BpNet;
int main()
{
bpn_conn con("127.0.0.1", 80);
return 0;
}
以上是一段被我简化过的代码,其中一个是hpp头文件(不是.h头文件),另一个是主函数所在的文件main.cpp
代码中的问题就是把下面的代码注释掉就能通过编译,如果不注释掉会报错

/*
namespace BpNet{
class bpn_conn;
}
*/
...
//using namespace BpNet
...
错误为:

../BpNet/main.cpp: In function 'int main()':
../BpNet/main.cpp:8:5: error: reference to 'bpn_conn' is ambiguous
../BpNet/bpn_conn.hpp:16:7: error: candidates are: class bpn_conn
../BpNet/bpn_conn.hpp:11:11: error: struct BpNet::bpn_conn
../BpNet/main.cpp:8:14: error: expected ';' before 'con'
这是什么原因呢?
namespace的使用方法错在哪里呢?
我是C++新手,不太熟悉C++语法。