gpt4 book ai didi

c++ - Eclipse 中类的多重定义错误

转载 作者:太空宇宙 更新时间:2023-11-04 06:05:17 24 4
gpt4 key购买 nike

尝试包含名为 sniffer.h 的文件时,Eclipse 中出现“多重定义”错误。

嗅探器.h:

#ifndef SNIFFER_H_
#define SNIFFER_H_
#include "sys/types.h"
#include "sys/socket.h"
#include "netinet/in.h"
#include "netdb.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <queue>
#include <pthread.h>
#include <sys/timeb.h>
#include <map>
#include <algorithm>
#include <math.h>
#include <syslog.h>
#include <signal.h>
#include <asm-generic/errno-base.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>
//Syslog libraries.
#include <sys/wait.h>
#include <ctime>
using namespace std;


string gconfigfilename = "config"; //Holds the config file name, dynamic because of the add_interface option.
int pipehandler; //pipe handler for the pipe that the gulp writes to
string pipeFullName; //absolute path for the pipe that the gulp writes to
FILE* pipe_fd;

并且有sniffer.cpp中所有函数的语句:

#include "../h/sniffer.h"

void OpenPipeRead(/*string sinterface*/)
{
int ret_val;
pipeFullName = "";
pipeFullName.append(mconfig[C_PIPEPATH]);
//Its not empty only when there is argument for parallal telepath_sniff instances.
pipeFullName.append(mconfig[C_PIPENAME]);
//if(strcmp(sinterface.c_str(), "") != 0)
// lpipename.append("_" + sinterface);
printf("Try to open Pipe for reading\n");
syslog(LOG_INFO, "Try to open Pipe for reading\n");
/* Create the named - pipe */
ret_val = mkfifo(pipeFullName.c_str(), 0666);
if ((ret_val == -1) && (errno != EEXIST)) {
perror("Error creating the pipe");
syslog(LOG_ERR, "Error creating the pipe");
exit(1);
}
if((pipehandler = open(pipeFullName.c_str(), O_RDWR)) < 1) /* Open the pipe for reading and writing , in append mode */
{
perror("Failed to open pipe");
syslog(LOG_ERR, "Failed to open pipe");
exit(1);
}
printf("Pipe opened.\n");
syslog(LOG_INFO, "Pipe opened.\n");
}

但是,在第一个函数中,它再次表示 pipeFullName 有多个定义。

我只包含它一次,并且该字符串在 sniffer.h 中定义。

最佳答案

您不应包含 .cpp 文件!!!

这样做最终会违反 One definition Rule

通常的方法是将声明放在头文件中,将定义放在cpp文件中。
您可以根据需要多次声明某个内容,但只能定义一次。

所以你应该只包含头文件。如果包含 cpp 文件,则 cpp 文件中符号定义的拷贝将添加到包含头文件的每个翻译单元中,从而违反了 ODR。

关于c++ - Eclipse 中类的多重定义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14038521/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com