具有多个 .cpp 文件的程序立即关闭,没有错误

Program With Multiple .cpp Files Closes Immediately, No Error

我已经搜索了这个问题的答案好几个小时了,但没有成功,所以我在这里问。我有这个程序,当它在一个大的 .cpp 文件中时,它工作得很好。但是当我将 class 放在头文件中,将更多函数定义放在不同的 .cpp 文件中,并将其余部分放在主 .cpp 文件中时,命令 window 弹出一秒钟并立即关闭.即使我在任何文件的任何行上放置一个断点,也会发生这种情况。

头文件:

#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 文件中定义的函数)两次。

If I replace #include "SURNAMEINFO.H" with #include "SurnameInfo.cpp" I >get an error saying I am defining functions (the ones defined in the .cpp >file) twice.

您不想在 SurnameInfo.cpp 中包含 SurnameInfo.cpp。你应该使用 SurnameInfo.h

如果您没有 "names.csv" 文件或者它不在正确的位置,那么当您调用

if (!inputfile) return 0;

程序将移动到主语句的末尾,什么也不会发生。