gpt4 book ai didi

c++ - 在ubuntu上编译c++项目

转载 作者:行者123 更新时间:2023-11-30 03:09:48 26 4
gpt4 key购买 nike

我正在编写我的 c++ 项目,在 visual studio 中一切顺利,但是当我在 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" !

即使在配置文件中我有相同名称的基础。提醒:在 visual studio 中它完美运行!

最佳答案

假设错误消息实际上是 错误:名为 _____ 的城市不存在,无法添加基数“Hazerim” 我会仔细查看城市和城市的大写/拼写 -以您的输入为基础。它们可能不匹配。

还在 std::string 上使用 strtok 只是自找麻烦,因为它具有破坏性,并且字符串不希望它们的内部状态被随机破坏。 find_first_of 等方法可以帮助您解析 C++ 字符串。

关于c++ - 在ubuntu上编译c++项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3802859/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com