2010-09-27 163 views
1

我正在寫我的C++項目,並在Visual Studio中的一切項目進展良好,但是當我編譯它在Ubuntu上很多事情就錯了。 例如:編譯C++ ubuntu上

int main (int argsNum, char* args[]){ 
    Country* country = new Country("USA"); 
    Military* military = new Military("Army",country); 
    Shalishut* shalishut = new Shalishut(military); 
    Manager* manager = Manager::GetInstance(); 

    FileReader* fileReader = FileReader::GetInstance(); 
    fileReader->ReadCityConfig(args,country); 
    fileReader->ReadRoadConfig(args,country); 
    fileReader->ReadMilitrayCampConfig(args,military); 

    military->ShowBases(); 

    return 0; 
} 

void FileReader::ReadMilitrayCampConfig(char* args[], Military* military){ 
    string line; 
    char inputFileName [MAX_FILE_NAME_LEN]; 
    strcpy (inputFileName,args[3]); 
    ifstream myfile (inputFileName); //inputFileName 
    char* campName; 
    string cityName; 

    if (myfile.is_open()){ 
     while (!myfile.eof()){ //until the end of file 
      getline (myfile,line); //separate each line. 
      if ((line.size() != 0) && (line[0] != '#')) { 
       campName = strtok(&line[0],","); 
       cityName = (string)strtok(NULL,","); 
       Shalishut::FixName(campName); Shalishut::FixName(&cityName[0]); 
       if (!(military->IsBaseExist(campName))){ 
        if (military->GetCountry()->IsCityExist(cityName)){ 
         Base* baseToAdd = new Base(campName,cityName); 
         if (baseToAdd != NULL){ 
          military->AddBaseToMilitary(baseToAdd); 
          military->GetCountry()->FindCity(cityName)->AddBaseToCity(baseToAdd); 
         } 
        } 
        else cout << "ERROR: City named \"" << cityName << "\" does not exist, can't add base \"" << campName << "\" !" << endl<<endl; 
       } 
       else cout << "ERROR: Base Named \"" << campName << "\" is already exist in Military, can't create base!" << endl<<endl; 
      }  
     } 
     myfile.close(); 
    } 
    else throw ExceptionMilitaryCampConfigFileFault(); /*cout << "ERROR: Unable to open MilitaryConfig file!"<< endl;*/ 
} 

bool Country::IsCityExist(const string cityName){ 
    map<string ,City*>::iterator itCities; 
    itCities = m_cities.find((string)cityName); 
    if (itCities != m_cities.end()) return true; 
    else return false; 
} 

void Shalishut::FixName(char* name){ 
    int i; 
    name[0] = toupper(name[0]); 
    for (i=1 ; name[i] ; i++){ 
      name[i] = tolower (name[i]); 
     } 
    } 
} 

的問題是程序讀取城市和道路,但是當它讀取軍營,我得到:

" does not exist, can't add base "Hazerim" ! 

即使在配置文件中,我有基地同名。 提醒:在視覺工作室,它完美的作品!

+2

你可能對你的數據文件DOS(CR + LF)行結尾,你的解析代碼是不是合租穩健,因此不會處理這個非常好。 – 2010-09-27 10:22:38

+0

@Paul R,之前的編輯在最後3行更清晰! – DumbCoder 2010-09-27 10:22:49

+0

我現在看到,代碼是清晰的,顏色..改變的東西? – Aviadjo 2010-09-27 10:35:39

回答

1

假設錯誤消息實際上是ERROR: City named _____ does not exist, can't add base "Hazerim"我會仔細看城市和城市爲基地在你投入的資本/拼寫。他們可能不匹配。

std::string上也使用strtok只是要求麻煩,因爲它具有破壞性,字符串不會指望它們的內部狀態被隨機吹走。有像find_first_of這樣的方法可以幫助你解析C++字符串。

1

像其他人說:

  • 仔細檢查行結束(可能在輸入文件運行DOS2UNIX的代替一個更強大的/錯誤=容易解決)

  • 確保一切的情況下,是正確的,文件名是區分大小寫的

  • 知道它正在尋找的文件,確保一切都在CWD

1

我建議不std::string內部亂搞。我不知道這是合法的,肯定會造成問題。使用.c_str()獲取C風格的字符串並將其複製到char [],或使用字符串函數來分析輸入。

要進行調試,請輸入insome輸出語句,以便查看字符串值,或瞭解一些關於gdb的內容並逐步完成初始化運行。

cityname = (string)...只是普通的難看。既然你沒有使用cityname指出範圍,你可以聲明string cityname(...);,並cityname總會被初始化,將被定義靠近它使用的地方。