现在的位置: 首页 > 综合 > 正文

C++ link2005 error 错误 解决方法汇总(一般重复定义,如果都是不就是 函数定义和实现没有分离)

2012年04月16日 ⁄ 综合 ⁄ 共 1973字 ⁄ 字号 评论关闭

一般都是重复定义。

可以按照VS给出的信息去找相关的变量或者宏定义,还有函数。

这里需要注意include,不要重复include,不要重复定义宏。

 

但上述这些,都是很好理解的……

 

如果大家按照上边说的检查了,还是百思不得其解,那么就看看关于类的函数定义和实现分离的问题吧。

一个兄弟的文章方法类似:http://blog.csdn.net/pang040328/archive/2009/07/07/4328270.aspx(不过说得很简单)

大家留意编译器出现的错误,这种情况一般都是类的函数定义重复。但只有一处定义了,为什么呢?

一般这种情况出现,是因为在h文件中,直接写了类函数的定义,虽然定义不在类中,已经分离出类之外,但还在.h文件中。

解决方法很简单,就把那几个函数放到对应的cpp中,如果没有就建一个。

 

我的例子:

Connection.h是一个类,其中四个函数定义在类之外,但还在h文件中。

SocketManager是另外一个类,函数实现分离到cpp中。在SocketManager.h引用Connection.h

然后在main那个cpp中,引用SocketManager.h

结果,报错了,正好就是那4个函数。

 

Connection代码:

 

1 //class connection
2  class Connection
3 {
4  public:
5 Connection(){
6  /////
7   }
8
9 ~Connection(){
10  /////
11   }
12
13 //make connect-socket
14   bool makeConnect(const string &host, const string &service, const string &protocal);
15 bool makeConnect(){
16 return makeConnect(host, service, protocal_type);
17 }
18 //make passive-socket
19   bool makePassive(const string &service, const string &transport, const int qlen);
20 bool makePassive(){
21 return makePassive(service, protocal_type, qlen);
22 }
23
24 //get protocal
25   string getProtocal(){ return protocal_type; }
26
27 //get socket type
28   SOCKET_TYPE getSocketType(){ return sock_type; }
29
30 //get socket
31 SOCKET getSocket(){ return sock; }
32
33 string getHost(){ return host; }
34 string getService(){ return service; }
35 string getPort(){ return service; }
36 int getQueueLength(){ return qlen; }
37
38 protected:
39
40 private:
41 SOCKET passivesock(const string &service, const string &transport, const int qlen);
42 SOCKET connectsock(const string &host, const string &service, const string &transport );
43
44 private:
45 SOCKET sock;
46 SOCKET_TYPE sock_type;
47 string protocal_type;
48 string host;
49 string service;
50 int qlen;
51 u_short portbase;
52 };
53
54 SOCKET Connection::passivesock(const string &serv, const string &tp, const int qlen)
55 {
56 ////
57 }
58
59 SOCKET Connection::connectsock(const string &h, const string &serv, const string &tp)
60 {
61 //////
62 }
63 bool Connection::makePassive(const string &service, const string &transport, const int qlen){
64 //////
65 }
66
67 bool Connection::makeConnect(const string &host, const string &service, const string &protocal){
68 ///////
69 }

 

 

 

 

 

抱歉!评论已关闭.