我有个课,就是
class Agent{
private:
....
....
int tun_alloc(char *dev, int flags);
public:
....
....
}
int Agent::tun_alloc(char *dev, const int flags) {
struct ifreq ifr;
int fd, err;
char clonedev[] = "/dev/net/tun";
if( (fd = open(clonedev , O_RDWR)) < 0 ) {
perror("Opening /dev/net/tun");
exit(1);
}
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = flags;
if (*dev) {
strncpy(ifr.ifr_name, dev, IFNAMSIZ);
}
if( (err = ioctl(fd, TUNSETIFF, (void *)&ifr)) < 0 ) {
perror("ioctl(TUNSETIFF)");
close(fd);
exit(1);
}
strcpy(dev, ifr.ifr_name);
return fd;
}在eclipse上构建项目时,我总是得到int tun_alloc(char *dev, int flags);的错误int tun_alloc(char *dev, int flags);,潜在的问题是什么?
如果我添加一个const
int tun_alloc(const char *dev, int flags);然后没有错误,但我需要更改dev的内容。
发布于 2015-11-10 20:28:15
您还没有提供完整的错误和实际调用函数的代码,但是当您向一个const char*参数提供char*参数时,您可能会得到这样的错误,即:
std::string s;
tun_alloc(s.c_str(),0); c_str()返回const char*,因此您希望:
int tun_alloc(const char *dev, int flags);
^^^^^ ~~~ !!编辑
但我需要更改dev的内容。
看到完整的源代码之后,我将用std::string& dev替换char。现在,您可以将字符串传递给函数,也可以从函数返回字符串。如果您想继续使用char,则必须将dev的长度传递给tun_alloc,并返回具有正确长度的错误,以防其太短。
https://stackoverflow.com/questions/33638706
复制相似问题