小编Ven*_*ta 的帖子

如何在地图中存储指针

我正在研究一个需要的项目

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来做到这一点

c++ stl map

17
推荐指数
3
解决办法
4万
查看次数

宏中的双哈希(##)是什么意思?

在下面的代码中,该##怎么办?

 #define MAKE_TYPE(myname) \
 typedef int myname ## Id; \
Run Code Online (Sandbox Code Playgroud)

c c++

12
推荐指数
2
解决办法
1888
查看次数

cpp单元设置和拆卸功能

任何人都可以告诉我如何以及何时在cppunit中调用setup和teardown函数.谢谢

c++ cppunit

2
推荐指数
1
解决办法
3593
查看次数

使用find重新生成STL Map检查条目

我正在关注计划

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>'

c++ stl

1
推荐指数
1
解决办法
222
查看次数

关于C++中的iterator和const问题

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)

有没有人知道我们为什么看到这个?

c++

1
推荐指数
2
解决办法
2712
查看次数

在C中使用sizeof

我有以下程序,崩溃了.有谁知道它为什么会崩溃?

/* 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)

c c++

0
推荐指数
1
解决办法
421
查看次数

标签 统计

c++ ×6

c ×2

stl ×2

cppunit ×1

map ×1