2011-04-11 129 views
0

我在QT中声明StringStream时遇到问题。 这里是我的代码:在QT中声明StringStream问题

std::stringstream ss; 
       for (int i = 0; i <= path.length()-1; ++i) 
       { 
        if (path[i] == '\\') 
        { 
         ss << "\\\\"; 
        } 
        else 
        { 
        ss << path[i]; 
        } 
       } 
        path = QString::fromStdString(ss.str());//store the stringstream to a string 
     return path; 

的错误信息是:

aggregate 'std::stringstream ss' has incomplete type and cannot be defined; 
+0

什么行会导致错误,以及您收到什么错误消息? – 2011-04-11 05:50:47

+0

嗯......问题在哪里?它没有编译?请发布错误消息。 – Naveen 2011-04-11 05:50:58

+0

那里我贴出了错误 – 2011-04-11 05:57:24

回答

3

混合QStringstd::string或相关的通常不是好主意。您应该使用QString方法执行此操作,如replace(QChar ch, const QString & after, Qt::CaseSensitivity cs = Qt::CaseSensitive)

path.replace('\\', "\\\\"); 

顺便说一句,你不能直接与任何标准std流的使用QString,目前还没有定义过载。但qPrintable函数可以帮助。并且您需要包含<sstream>以使用std::stringstream

+0

这段代码给出了未知字符的结果。 – 2011-04-11 06:03:34

+1

这段代码有效,我只是测试过它。确保你有正确的'''和'''引号和正确数量的斜杠,并给出确切的编译器错误它不。 – Mat 2011-04-11 06:06:46

+0

好吧谢谢它真的工作.. – 2011-04-11 06:15:00

1

包括<sstream>使用stringstream类。

尽管我同意@Mat,但将Qt的QString方法用于此特定目的可能是一个好主意。