//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
现在打印
Mordor不属于维斯特洛 魔多不属于维斯特洛 魔多不属于维斯特洛 魔多不属于维斯特洛 Mordor不属于Westeros
The_Vale不属于维斯特洛 The_Vale,人口234567 The_Vale不是维斯特洛的一部分 The_Vale不是维斯特洛的一部分 The_Vale不是Westeros的一部分
答案 0 :(得分:1)
int count = 0;
Kingdom* pKingdoms = nullptr;
pKingdoms = new Kingdom[count];
这会创建一个包含count = 0
元素的数组。任何进一步的访问都将超出范围。
我建议你使用std::vector
:
std::vector<Kingdom> kingdoms(/*(good) count*/);
std::string
的{{1}}类型。
当我看到这条线时:
char m_name[32];
我希望循环跟随,而不是cout << " Searching for kingdom " << KingdomName << " in westeros " << endl;
。还有一些代码首先用一些名称填充数组。
if
=&gt;之后if
版:您应该打印
KingdomName不属于Westeros。
后循环不在内部,且仅在未找到for
的情况下。
答案 1 :(得分:0)
也许您忘了将The_Vale
添加到pKingdoms
数组。
所以,这样的事情会让你明白你做错了什么:
在主文件中你可以改进它,如下所示,所有其他文件都可以;)
//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;
}
在编辑之后,也许source.cpp中的这样的东西会有所帮助。
//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;
}
}
}