我正在研究一个需要的项目
class MyObj;
map<string, MyObj*> myMap;
Run Code Online (Sandbox Code Playgroud)
这里的逻辑是将文件名映射到MyObj类.
如果我尝试插入以下内容
string strFilename = "MyFile";
MyObj* pObj = new MyObj();
myMap.insert(strFileName, pObj); // This line throwing following error.
Run Code Online (Sandbox Code Playgroud)
没有匹配的呼叫功能
'std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, void*, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, void*> > >::insert(std::string&, void*)'
任何人都可以帮我解决这个问题.是否有更好的方法我们可以使用STL来做到这一点
在下面的代码中,该##怎么办?
#define MAKE_TYPE(myname) \
typedef int myname ## Id; \
Run Code Online (Sandbox Code Playgroud) 我正在关注计划
typedef std::map<std::string, CRTSLogManager*> FileNameToStorageClass;
FileNameToStorageClass m_mapFileNameToLogStorage;
map<string, void*>::iterator iter;
iter =m_mapFileNameToLogStorage.find(cFileName);
if(iter == m_mapFileNameToLogStorage.end())
{
typedef std::pair<std::string, CRTSLogManager*> FileNameToStorageClassPair;
string strFilename = "MyFile";
CRTSLogManager *pLogManager = new CRTSLogManager();
m_mapFileNameToLogStorage.insert(
FileNameToStorageClassPair(strFilename, pLogManager));
}
Run Code Online (Sandbox Code Playgroud)
我在编译时遇到以下错误,这与==签入条件有关.
在'iter ==((CRTSLogManagerReal*)this)中没有匹配'operator ==' - > CRTSLogManagerReal :: m_mapFileNameToLogStorage.std :: map,std :: allocator>,CRTSLogManager*,std :: less,std :: allocator >>,std :: allocator,std :: allocator>,CRTSLogManager*>> ::.std :: _ Tree <_Traits> :: end with _Traits = std :: _ Tmap_traits,std :: allocator>,CRTSLogManager*,std: :less,std :: allocator >>,std :: allocator,std :: allocator>,CRTSLogManager*>>,false>'
class LogManager {
private:
mutable mapManagerMutex mapMutex;
typedef std::map<std::string, LogStorage*> FileNameToStorageClass;
FileNameToStorageClass m_mapFileNameToLogStrg;
public:
int write( const string& strFileName, char* text ) const;
};
int LogManager::write( const string &strFileName, char* text ) const
{
mapManagerMutex::scoped_lock lock(mapMutex);
FileNameToStorageClass::iterator iter;
iter = m_mapFileNameToLogStrg.find(strFileName);
if(iter != m_mapFileNameToLogStrg.end())
{
// do some thing.
}
else
{
return -1;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我在写函数结束时没有const,则上面的代码编译.如果我在结束时添加const我会得到以下错误
D:/LogManager.cpp:133: error: no match for 'operator=' in 'iter = ((const RtsInfrastructure::RtsCommon::Diagnostics::LogManager*)this)-
cc: C:/QNX650/host/win32/x86/usr/lib/gcc/i486-pc-nto-qnx6.5.0/4.4.2/cc1plus caught signal 1
Run Code Online (Sandbox Code Playgroud)
有没有人知道我们为什么看到这个?
我有以下程序,崩溃了.有谁知道它为什么会崩溃?
/* writes a, b, c into dst
** dst must have enough space for the result
** assumes all 3 numbers are positive */
void concat3(char *dst, int a, int b, int c) {
sprintf(dst, "%08x%08x%08x", a, b, c);
}
/* usage */
int main(void) {
printf("The size of int is %d \n", sizeof(int));
char n3[3 * sizeof(int) + 1];
concat3(n3, 0xDEADFACE, 0xF00BA4, 42);
printf("result is 0x%s\n", n3);
return 0;
}
Run Code Online (Sandbox Code Playgroud)