#define LOG(format,...) Logger::Log(format,__VA_ARGS__)
#define STRIP(netIp) GeneralUtils::inet_ntop_(netIp)
string GeneralUtils::inet_ntop_(unsigned int netIp){
char strIP[INET_ADDRSTRLEN];
in_addr sin_addr;
sin_addr.s_addr = netIp;
inet_ntop(AF_INET, &sin_addr.s_addr, strIP, sizeof strIP);
return string(strIP);
}
Run Code Online (Sandbox Code Playgroud)
致电:
LOG("src %s dst %s" ,STRIP(src_ip_));
Run Code Online (Sandbox Code Playgroud)
我得到编译错误:
cannot pass objects of non-trivially-copyable type ‘std::string {aka struct std::basic_string<char>}’ through ‘...’
Run Code Online (Sandbox Code Playgroud)
我知道varargs是c兼容的,所以我不能发送字符串.有没有一种简单的方法可以绕过它?修复它是否正确如下:
#define STRIP(netIp) GeneralUtils::inet_ntop_(netIp).data()
Run Code Online (Sandbox Code Playgroud) 我来自Java,所以我熟悉同步而不是互斥.我想知道pthread_mutex_t是否也是重入.如果没有,还有另一种机制吗?
谢谢
套接字映射def:
map<unsigned char[6],RawSocket> sockets_;
Run Code Online (Sandbox Code Playgroud)
将值设置为套接字映射:
RawSocket tmp(ifcName,newSocketsIt->src_mac_);
sockets_[newSocketsIt->src_mac_] = tmp;
Run Code Online (Sandbox Code Playgroud)
RawSocket def:
class RawSocket{
public:
RawSocket(string ifcName,const unsigned char dstMac[6]);
}
Run Code Online (Sandbox Code Playgroud)
我不想拥有RawSocket的默认构造函数. 错误是:
/usr/include/c++/4.6/bits/stl_map.h: In member function ‘std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = unsigned char [6], _Tp = RawSocket, _Compare = std::less<unsigned char [6]>, _Alloc = std::allocator<std::pair<const unsigned char [6], RawSocket> >, std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = RawSocket, std::map<_Key, _Tp, _Compare, _Alloc>::key_type = unsigned char [6]]’:
../sockets_container.cpp:18:34: instantiated from here
/usr/include/c++/4.6/bits/stl_map.h:453:11: error: no matching …Run Code Online (Sandbox Code Playgroud) 在Ubuntu上运行c ++.
在configuration.h文件中我有这个(全局):
static const string APP_CONFIG_FILE_NAME = "cfg";
Run Code Online (Sandbox Code Playgroud)
在我的configuration.cpp(顺便说一句是单例,所以配置的构造函数调用加载配置)我这样做:
void Configuration::loadConfiguration() {
cout<< "config file name " << APP_CONFIG_FILE_NAME.data();
load();
}
Run Code Online (Sandbox Code Playgroud)
将从另一个全局调用负载配置:(这是失败的关键点)
Timer t(Configuration::Instance()->timeout);
Run Code Online (Sandbox Code Playgroud)
我看到cost字符串未初始化(空).如果我用char*替换字符串,它按照我预期的顺序初始化.
但是在另一个程序上配置相同的类工作,所以我不知道问题.
还有另外一个问题,但不是因为同样有它不是一个全球性的.
class Manager {
public:
list<Employee> getEmployees() {
// Do I need to lock here?
return emps_;
}
void addEmp(Employee emp); //Here I have lock
private:
list<Employee> emps_;
};
Run Code Online (Sandbox Code Playgroud)
实例Manager在多个线程之间共享.我是否需要为getEmployees成员函数添加锁定?
我很确定我需要一个锁,因为复制了完整的列表,所以在此期间进行的任何修改(直到复制完成)都可能会破坏复制操作.
我只是问这个,因为我很少有人认为没有必要锁定.
编辑:
由于现在需要锁定,我的问题是如何以最小的开销来做到这一点.通过以下解决方案,您可以将列表复制两次:
list<Employee> getEmployees() {
pthread_mutex_lock( &mutex1 );
list<Emp> tmp = emps_; //Copy 1
pthread_mutex_unlock( &mutex1 );
return tmp;//Copy 2
}
Run Code Online (Sandbox Code Playgroud) 新的cpp.当我调用擦除迭代器而不增加它时,我的程序立即退出而没有任何符号或崩溃.在下面的示例中,仅打印'After if condition'而不是任何内容.没有显示错误.但是,如果我正确调用 list.erase(it ++); 比打印全部. 我的问题是当我没有把它称为正确时发生的事情.为什么我没有看到任何崩溃或退出?我担心,我问这个问题的原因是为了捕捉这些崩溃,为什么我没有在捕获区中捕获它?有没有办法抓住它?
int main(int argc, char *argv[]) {
try {
list<int>::iterator it;
list<int> list;
list.push_back(1);
for (it = list.begin(); it != list.end(); ++it) {
if ((*it) == 1) {
list.erase(it);
}
cerr << "After if condition " << endl;
}
cerr << "After for condition" << endl;
} catch (...) {
cerr << "catch exception" << endl;
}
cerr << "Finish" << endl;
}
Run Code Online (Sandbox Code Playgroud) 有没有任何代码的简单,空项目.只是包括.包含linux/netfilter.h导致编译错误:
In file included from ../src/main.cpp:2:0:
/usr/include/linux/netfilter.h:65:17: error: field ‘in’ has incomplete type
/usr/include/linux/netfilter.h:66:18: error: field ‘in6’ has incomplete type
Run Code Online (Sandbox Code Playgroud)
正如你在简单的项目中看到我有2包括,如果我删除iostream包括我将得到额外的错误 - '/usr/include/linux/sysctl.h:40:2:错误:'size_t'没有命名类型"
#include <iostream>
#include <linux/netfilter.h>
int main() {
}
Run Code Online (Sandbox Code Playgroud)
如果我在netfilter.h之前添加netinet/in.h的包含而不是没有错误.
为什么我的简单主程序不能有这些编译错误?
谢谢
我想知道是否可以从stl:map中删除与find同时删除?并发意味着 - 两个线程同时尝试一个擦除,一个同时调用查找.
如果我调用函数inet_ntoa返回char*.谁负责释放分配的内存?
我的方法看起来像这样:
char* inet_aton_(unsigned int atonIp){
in_addr sin_addr;
sin_addr.s_addr = atonIp;
return inet_ntoa(sin_addr);
}
Run Code Online (Sandbox Code Playgroud) class Monitor {
public:
Monitor(string someData);
Person person_;
}
class Person {
public:
Person(string personId);
}
Run Code Online (Sandbox Code Playgroud)
Person没有默认构造函数.它有一个参数构造函数.我只能在 Monitor构造函数中初始化它.我来自java,其中声明只是指针,因此您在声明时不需要初始化新实例.
对于没有默认构造函数的类成员,最佳做法是什么?我应该添加默认构造函数(缺点 - 没用它)或使用指针.