我已经搜索了这个问题的答案几个小时没有运气,所以我在这里问。我有这个程序,它只是在一个大的.cpp文件中工作正常。但是,当我将类放在头文件中,在不同的.cpp文件中放置更多函数定义,而将其余放在主.cpp文件中时,命令窗口会弹出一秒钟并立即关闭。即使我在任何文件的任何行上放置断点,也会发生这种情况。
标题文件:
#ifndef SURNAMEINFO_H
#define SURNAMEINFO_H
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
using namespace std;
class SurnameInfo
{
private:
char *name;
int counter;
float pctrace[6];
public:
char getname(){ return *name; };
int getcounter(){ return counter; };
float getpctrace(int i){ return pctrace[i]; };
void setname(char*, int);
void setcounter(int);
void setpctrace(float, int);
void printname() { cout << left << setw(15) << name; };
char *getpointer(){ return name; };
~SurnameInfo();
};
#endif
.cpp文件定义了其余的功能:
#include "SURNAMEINFO.h"
void SurnameInfo::setname(char * nm, int len)
{
name = new char[len + 1];
memset(name, len + 1, 0);
memcpy(name, nm, len);
name[len] = 0;
}
void SurnameInfo::setcounter(int ct)
{
counter = ct;
}
void SurnameInfo::setpctrace(float prace, int i)
{
pctrace[i] = prace;
}
SurnameInfo::~SurnameInfo()
{
delete[] name;
}
主程序:
#include <iostream>
#include <cstring>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include "SurnameInfo.h"
int numberOfNames = 0;
using namespace std;
const int MAXARRAY = 151671;
SurnameInfo * surnames[MAXARRAY];
const int MAXLINE = MAXARRAY;
void processLine(char *, int);
void mostPopular(int);
void searchName();
int main()
{
char searchaname;
ifstream inputfile;
inputfile.open("names.csv");
char line[MAXLINE];
if (!inputfile) return 0;
inputfile.getline(line, MAXLINE);
inputfile.getline(line, MAXLINE);
while (!inputfile.eof())
{
processLine(line, numberOfNames++);
inputfile.getline(line, MAXLINE);
}
do
{
cout << "Do you want to search for a name? (y/n) ";
cin >> searchaname;
if (searchaname == 'y')
searchName();
} while (searchaname == 'y');
system("CLS");
cout << "Here is a list of names most common in each race: " << endl;
cout << "Press enter to continue.";
cin.clear(); cin.sync(); cin.get();
system("CLS");
cout << "Highest Population: \n\nWHITE: \n";
mostPopular(0);
cout << "\n\nBLACK:\n";
mostPopular(1);
cout << "\n\nASIAN AND PACIFIC ISLANDER: \n";
mostPopular(2);
cout << "\n\nAMERICAN INDIAN OR ALASKAN NATIVE: \n";
mostPopular(3);
cout << "\n\n2 OR MORE RACES: \n";
mostPopular(4);
cout << "\n\nHISPANIC: \n";
mostPopular(5);
inputfile.close();
cin.clear(); cin.sync(); cin.get();
return 0;
}
void processLine(char *line, int n)
{
surnames[n] = new SurnameInfo;
char * pch = strtok(line, ",");
int len = strlen(pch);
surnames[n]->setname(pch, len);
surnames[n]->setcounter(atoi(strtok(NULL, ",")));
for (int i = 0; i < 6; i++)
{
pch = strtok(NULL, ",");
surnames[n]->setpctrace(pch[0] == '(' ? -1 : atof(pch), i);
}
}
void mostPopular(int race)
{
const int TOPS = 20;
int tops[TOPS + 1] = { 0 };
int kept = 0;
for (int i = 0; i < numberOfNames; i++)
{
if (surnames[i]->getcounter() < 10000) continue;
int j = kept - 1;
for (; j >= 0; j--)
{
if (surnames[i]->getpctrace(race)*surnames[i]->getcounter() > surnames[tops[j]]->getpctrace(race)*surnames[tops[j]]->getcounter())
tops[j + 1] = tops[j];
else break;
}
if (j + 1 < TOPS) tops[j + 1] = i;
if (kept < TOPS) kept++;
}
for (int i = 0; i < TOPS; i++)
{
surnames[tops[i]]->printname();
cout << "\t" << surnames[tops[i]]->getpctrace(race)*surnames[tops[i]]->getcounter() / 100 << endl;
}
}
void binarysearch(char *name2, int &index)
{
int low = 0; int high = numberOfNames - 1;
while (low <= high)
{
int mid = (low + high) / 2;
char * name3 = (surnames[mid]->getpointer());
int diff = strcmp(name2, name3);
if (diff == 0)
{
index = mid;
low = high + 1;
}
else if (diff > 0)
low = mid + 1;
else
high = mid - 1;
}
}
void searchName()
{
char inputName[100];
cin.ignore();
cout << "Enter a name (all caps): ";
cin.getline(inputName, 100);
int index2 = -1;
binarysearch(inputName, index2);
if (index2 != -1)
{
surnames[index2]->printname();
cout << " appears " << surnames[index2]->getcounter() << " times." << endl;
}
}
如果我用#include "SURNAMEINFO.H"
替换#include "SurnameInfo.cpp"
,我会收到一个错误,说我正在定义函数(在.cpp文件中定义的函数)两次。
答案 0 :(得分:0)
如果我替换#include&#34; SURNAMEINFO.H&#34; #include&#34; SurnameInfo.cpp&#34;我&gt;得到一个错误,说我定义了两次函数(在.cpp&gt;文件中定义的函数)。
您不希望在SurnameInfo.cpp中包含SurnameInfo.cpp。你应该使用SurnameInfo.h
如果你没有&#34; names.csv&#34;文件或它没有在正确的位置打开,然后当你打电话
if(!inputfile)返回0;
程序将移至主语句的末尾,不会发生任何事情。