问题描述
//这是我的源文件,.cpp
#include< iostream>
#include< string>
#includekingdom.h
namespace westeros {
void display(Kingdom pKingdom [],int kingdomElement,string KingdomName){
cout<< ----------------<< ENDL;
cout<< 寻找王国<< KingdomName<< 在维斯特洛斯<< ENDL;
for(int i = 0; i< kingdomElement; i ++){
if(pKingdom [i] .m_name == KingdomName){
cout<< ---------------------<< ENDL;
cout<< KingdomName<< ,人口<< pKingdom [i] .m_population<< ENDL;
cout<< ---------------------<< ENDL;
}
else {
cout<< ---------------------<< ENDL;
cout<< KingdomName<< 不是维斯特洛的一部分。<< ENDL;
cout<< ---------------------<< ENDL;
}
}
}
}
//这是我的主文件
#include< iostream>
#includekingdom.h
#include< string>
using namespace std;
使用名称空间westeros;
int main(void){
int count = 0;
Kingdom * pKingdoms = nullptr;
pKingdoms =新王国[count];
显示(pKingdoms,count,Mordor);
显示(pKingdoms,count,The_Vale);
delete [] pKingdoms;
pKingdoms = nullptr;
返回0;
}
//这是我的头文件
#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 [],int kingdomElement,string KingdomName);
}
#endif
现在打印
Mordor不属于Westeros
Mordor不属于Westeros
Mordor不属于Westeros
Mordor不属于Westeros
Mordor不属于Westeros
The_Vale不属于Westeros
The_Vale,人口234567
The_Vale不属于Westeros
The_Vale不是Westeros的一部分
The_Vale不是Westeros的一部分
也许你只是忘了添加 The_Vale
到 pKingdoms
数组。
所以,某事这样会让你明白你做错了什么:
在主文件中你可以改进它,如下所示,所有其他文件都可以;)
//这是我的主文件
#include< iostream>
#includekingdom.h
#include< string.h>
using namespace std;
使用名称空间westeros;
int main(void){
int count = 0;
// Kingdom * pKingdoms = nullptr;
// pKingdoms =新王国[count];
王国* pKingdoms =新王国[count]; //可能是一个更好的选择,
//因为它减少了代码。
显示(pKingdoms,count,Mordor); // pKingdoms没有Mordor
显示(pKingdoms,count,The_Vale); // pKingdoms没有The_vale
//这里我将'The_Vale'添加到数组
strcpy(pKingdoms [0] .m_name,The_Vale);
pKingdoms [0] .m_population = 1000;
显示(pKingdoms,count,The_Vale); // pKingdoms有The_vale
delete [] pKingdoms;
pKingdoms = nullptr;
返回0;
}
同样在编辑之后,source.cpp中的这样的东西也会有所帮助。 #include< iostream>
#include< string>
#includekingdom.h
namespace westeros {
void display(Kingdom pKingdom [],int kingdomElement,string KingdomName){
int flag = -1;
cout<< ----------------<< ENDL;
cout<< 寻找王国<< KingdomName<< 在维斯特洛斯<< ENDL;
for(int i = 0; i< kingdomElement; i ++)
if(pKingdom [i] .m_name == KingdomName)
flag = i;
if(flag!= -1)
{
cout<< ---------------------<< ENDL;
cout<< KingdomName<< ,人口<< pKingdom [flag] .m_population<< ENDL;
cout<< ---------------------<< ENDL;
}
else {
cout<< ---------------------<< ENDL;
cout<< KingdomName<< 不是维斯特洛的一部分。<< ENDL;
cout<< ---------------------<< ENDL;
}
}
}
//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, count, "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[], int kingdomElement, string KingdomName);
}
#endif
Now it prints
The Mordor is not part of WesterosThe Mordor is not part of WesterosThe Mordor is not part of WesterosThe Mordor is not part of WesterosThe Mordor is not part of Westeros
The_Vale is not part of WesterosThe_Vale, population 234567The_Vale is not part of WesterosThe_Vale is not part of WesterosThe_Vale is not part of Westeros
Maybe you just forgot to add the The_Vale
to the pKingdoms
array.
So, something like this will make you understand what did you do wrong:
In the main file you can improve it like below, all other files are okay ;)
//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, count, "Mordor"); // pKingdoms doesn't have Mordor
display(pKingdoms, count, "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, count, "The_Vale"); // pKingdoms have The_vale
delete[]pKingdoms;
pKingdoms = nullptr;
return 0;
}
Also after edit, maybe something like this in source.cpp will help.
//this is my source file, .cpp
#include <iostream>
#include <string>
#include "kingdom.h"
namespace westeros{
void display(Kingdom pKingdom[], int kingdomElement, 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;
}
}
}
这篇关于如果语句不起作用,则跳过其他部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!