2

我有一个非常简单的类存根 - 我刚刚开始制作它:

class CJSScript
{
public:
    CJSScript(std::string scriptfile);
    ~CJSScript(void);
private:
    std::string scriptname;
};
CJSScript::CJSScript(std::string scriptfile)
{
    size_t found = scriptfile.find_last_of("/\\");
    scriptname = scriptfile.substr(found+1);
    printf("should load %s now...", scriptname);

}

但是在那个构造函数中我得到一个异常,this显然设置为0x7ffffffe

主程序是

int _tmain(int argc, _TCHAR* argv[])
{
    CJSScript* test=new CJSScript("./script/test.js");
  system("pause");
    return 0;
}

这到底是怎么回事。我以为我很久以前就有基础知识了,但这是一种妥协。我或编译器:)

调试器转储:

Win32Project3.exe!_output_l(_iobuf * stream, const char * format, localeinfo_struct * plocinfo, char * argptr) Line 1649    C++
Win32Project3.exe!printf(const char * format, ...) Line 62  C
Win32Project3.exe!CJSScript::CJSScript(std::basic_string<char,std::char_traits<char>,std::allocator<char> > scriptfile) Line 11 C++
Win32Project3.exe!wmain(int argc, wchar_t * * argv) Line 38 C++
Win32Project3.exe!__tmainCRTStartup() Line 240  C
4

1 回答 1

6

printf不知道如何处理string对象。你需要通过一个const char*

 printf("should load %s now...", scriptname.c_str());

这是类型安全的问题。出于这个原因,我更喜欢使用流。

cout << "should load " << scriptname << " now...";
于 2012-10-17T18:18:35.073 回答