
//this is my source file,.cpp#include <iostream>#include <string>#include "kingdom.h"namespace westeros{ voID display(Kingdom pKingdom[],int kingdomElement,string Kingdomname){ cout << " ---------------- " << endl; cout << " Searching for kingdom " << Kingdomname << " in westeros " << endl; for(int i=0; i<kingdomElement; i++){ if (pKingdom[i].m_name == Kingdomname){ cout << " --------------------- " << endl; cout << Kingdomname << ",population " << pKingdom[i].m_population << endl; cout << " --------------------- " << endl; } else{ cout << " --------------------- " << endl; cout << Kingdomname << " is not part of Westeros. " << endl; cout << " --------------------- " << endl; } } }}//this is my main file#include <iostream>#include "kingdom.h"#include <string>using namespace std;using namespace westeros;int main(voID){ int count = 0; Kingdom* pKingdoms = nullptr; pKingdoms = new Kingdom[count]; display(pKingdoms,count,"Mordor"); display(pKingdoms,"The_Vale"); delete[]pKingdoms; pKingdoms = nullptr; return 0;}//this is my header file#ifndef KINGDOM_H_#define KINGDOM_H_using namespace std;namespace westeros{ class Kingdom{ public: char m_name[32]; int m_population; }; voID display(Kingdom pKingdom[],string Kingdomname);}#endif 现在它打印
魔多不属于维斯特洛
魔多不属于维斯特洛
魔多不属于维斯特洛
魔多不属于维斯特洛
魔多不属于维斯特洛
The_Vale不是维斯特洛的一部分
The_Vale,人口234567
The_Vale不是维斯特洛的一部分
The_Vale不是维斯特洛的一部分
The_Vale不是维斯特洛的一部分
所以,这样的事情会让你明白你做错了什么:
在主文件中你可以改进它,如下所示,所有其他文件都可以;)
//this is my main file #include <iostream> #include "kingdom.h" #include <string.h> using namespace std; using namespace westeros; int main(voID){ int count = 0; // Kingdom* pKingdoms = nullptr; // pKingdoms = new Kingdom[count]; Kingdom* pKingdoms = new Kingdom[count]; // Might be a better choice,// as it reduces code. display(pKingdoms,"Mordor"); // pKingdoms doesn't have Mordor display(pKingdoms,"The_Vale"); // pKingdoms doesn't have The_vale // Here I add the 'The_Vale to the array strcpy(pKingdoms[0].m_name,"The_Vale"); pKingdoms[0].m_population = 1000; display(pKingdoms,"The_Vale"); // pKingdoms have The_vale delete[]pKingdoms; pKingdoms = nullptr; return 0; } 在编辑之后,也许source.cpp中的这样的东西会有所帮助.
//this is my source file,string Kingdomname){ int flag = -1; cout << " ---------------- " << endl; cout << " Searching for kingdom " << Kingdomname << " in westeros " << endl; for(int i=0; i<kingdomElement; i++) if (pKingdom[i].m_name == Kingdomname) flag = i; if (flag != -1) { cout << " --------------------- " << endl; cout << Kingdomname << ",population " << pKingdom[flag].m_population << endl; cout << " --------------------- " << endl; } else{ cout << " --------------------- " << endl; cout << Kingdomname << " is not part of Westeros. " << endl; cout << " --------------------- " << endl; } }} 总结 以上是内存溢出为你收集整理的c – 如果语句不起作用,则跳过其他部分全部内容,希望文章能够帮你解决c – 如果语句不起作用,则跳过其他部分所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)