如何正确地将参数转换为const char *?

时间:2014-07-07 19:27:24

标签: c++ vector const

编译时出现以下错误,这是代码中的问题部分。请帮我理解错误是什么?

for (std::vector<std::string>::iterator i = files.begin(), end = files.end(); i != end; ++i)

   {
       printf("%s%s\n", parm.c_str(), i->c_str());

       TiXmlElement *fileElement = new TiXmlElement("File");

       fileElement->SetAttribute("name", *i); // error C2664

       parentElement->LinkEndChild(fileElement);

   }

   files.clear();



   for (std::vector<std::string>::iterator i = subFolders.begin(), end = subFolders.end(); i != end; ++i)

   {
       printf("%s%s\n", parm.c_str(), i->c_str());

       TiXmlElement *fileElement = new TiXmlElement("Folder");

       folderElement->SetAttribute("name", *i); //error C2227

       parentElement->LinkEndChild(folderElement);

       PrintDir(parm + *i + "\\", folderElement);

   } 

错误C2664 :void TiXmlElement :: SetAttribute(const char *,const char *):无法从&#39; std :: basic_string&lt; _Elem,_Traits,_Ax&gt;&#转换参数2 39; to&#39; const char *&#39 ;; 错误c2065 folderElement未声明的标识符; 错误C2227 左边的&#39; - &gt; SetAttribute&#39;必须指向class / struct / union;

1 个答案:

答案 0 :(得分:0)

首先,在你的第二个for循环中,folderElement是未声明的。我认为你的意思是将其命名为folderElement而不是fileElement。所以改变这一行:

TiXmlElement *fileElement = new TiXmlElement("Folder");

TiXmlElement *folderElement = new TiXmlElement("Folder");