2020年1月16日 星期四

[Log book] stringstream的建構子中不可傳入其他建構子

─ 問題描述
目標:在win 10環境下使用VS 2008的C++讀入ini檔並用逗點分隔讀入的key值
使用方法及問題:使用std的iostream類的函式,並用getline來分割,但在getline的函式一直編譯出錯,編譯器一直顯示引數類型不正確、没有相關多載之類的錯誤訊息。
下面是程式碼
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <sstream>
#include "tchar.h"  
#include "windows.h"
#define COUNTOF(x)  ( sizeof(x) / sizeof((x)[0]) )
bool GetCurrentDirectory(TCHAR *obuf, DWORD osize)
{
    if ( ! GetModuleFileName(0, obuf, osize) )
    {
        *obuf = '\0';
        return FALSE;
    }
    TCHAR *lastslash = 0;
    for ( ; *obuf; obuf ++)   
    {   
        if ( *obuf == '\\' || *obuf == '/' )   
            lastslash = obuf;   
    }   
    
    if ( lastslash ) *lastslash = '\0';  
    
    return TRUE;   
}
void main()
{
        TCHAR filebuf[256];
        char data[100];
        std::string tempStr;
        std::vector<std::string> output;
        if ( GetCurrentDirectory(filebuf, COUNTOF(filebuf)) )   
    {  
               wcscat_s(filebuf,_T("\\IrregularShapeSetting.ini"));
               TCHAR temp_str[100];
               ::GetPrivateProfileString(_T("Irregular Shape Setting"), _T("Folder"),  _T("="), temp_str, 100, filebuf);
               wcstombs(data, temp_str, 100);
               std::string ConverToStr = std::string(data);
               
               std::stringstream folders(std::string(data)); // 但問題在這
               while (std::getline(folders, tempStr, ',')) // 出錯在這行
                       output.push_back(tempStr);
               for(int i = 0; i < output.size(); i++)
                       std::cout << output[i].c_str() << std::endl;
    }   
    else
    {           
               wprintf_s(_T("ERROR: cannot get irregular shape inspection setting  directory\n"));
    }
        system("pause");
}
下面是錯誤訊息


─ 思路
既然在stringstream的建構子中使用string的建構子無法編譯成功,那先建構string再將string丟入。


─ 解決方法
先建構string再將string丟入。
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <sstream>
#include "tchar.h"  
#include "windows.h"
#define COUNTOF(x)  ( sizeof(x) / sizeof((x)[0]) )
bool GetCurrentDirectory(TCHAR *obuf, DWORD osize)
{
    if ( ! GetModuleFileName(0, obuf, osize) )
    {
        *obuf = '\0';
        return FALSE;
    }
    TCHAR *lastslash = 0;
    for ( ; *obuf; obuf ++)   
    {   
        if ( *obuf == '\\' || *obuf == '/' )   
            lastslash = obuf;   
    }   
    
    if ( lastslash ) *lastslash = '\0';  
    
    return TRUE;   
}
void main()
{
        TCHAR filebuf[256];
        char data[100];
        std::string tempStr;
        std::vector<std::string> output;
        if ( GetCurrentDirectory(filebuf, COUNTOF(filebuf)) )   
    {  
               wcscat_s(filebuf,_T("\\IrregularShapeSetting.ini"));
               TCHAR temp_str[100];
               ::GetPrivateProfileString(_T("Irregular Shape Setting"), _T("Folder"),  _T("="), temp_str, 100, filebuf);
               wcstombs(data, temp_str, 100);
               std::string ConverToStr = std::string(data);
               
               std::stringstream folders(ConverToStr);
               while (std::getline(folders, tempStr, ','))
                       output.push_back(tempStr);
               for(int i = 0; i < output.size(); i++)
                       std::cout << output[i].c_str() << std::endl;
    }   
    else
    {           
               wprintf_s(_T("ERROR: cannot get irregular shape inspection setting  directory\n"));
    }
        system("pause");
}

這封郵件來自 Evernote。Evernote 是您專屬的工作空間,免費下載 Evernote

沒有留言:

張貼留言