-1

我一直在尝试将我的程序拆分为头文件和主文件,但无法完全弄清楚头文件将如何与它一起工作。任何试图理解这一点的帮助将不胜感激。

#include <iostream>
#include <fstream>
#include <string>
#include <set>


void comparing(std::ifstream& fileIn, std::ifstream& keywords, std::ofstream& outFile)
{
    std::string str;
    std::string word;
    std::set<std::string> keywordsSet;
        int check = 0;

        while (keywords >> word) {
        keywordsSet.insert(word);
        }

    while(fileIn >> str){

        if(str == "<p>") {
            check++;
        }


        if((check > 1 && str == "<p>") || (check == 1 && str == "</body>"))
        {
            outFile << "</p>";
            check--;
        }



    if(keywordsSet.find(str) != keywordsSet.end()) {

        outFile << "<i>" << str << "</i>" << " ";
        }

        else{
        outFile << str << " ";
        }           
    }   

}
int main(int argc, char* argv[])
{

std::ifstream fileIn;
fileIn.open (argv[1]);

std::ifstream keywords;
keywords.open (argv[2]);

std::ofstream outFile;
outFile.open(argv[3]);

    comparing(fileIn, keywords, outFile);

fileIn.close();
keywords.close();
outFile.close();

return 0;
}
4

2 回答 2

2

在您的代码中,您声明一个函数:comparing. 您通常将所有声明放在一个头文件中(MSVC++ 使用.h后缀,但我更喜欢.hppC++ 头文件),并将实现放在一个.cpp文件中。

您的标题(comparing.hpp或类似的东西)看起来像这样:

// Prevents redefinitions when including the same header multiple times
#pragma once

// Does the same thing, just to make sure 
// (some compilers don't support #pragma once)
#ifndef COMPARING_HPP
#define COMPARING_HPP

#include <fstream>

void comparing(std::ifstream&, std::ifstream&, std::ofstream&); // Function prototype

#endif

你的comparing.cpp文件看起来像这样:

#include "comparing.hpp"
// Note the quotation marks instead of greater than, less than signs
// This is because the header is not in the standard include path,
// but rather in your project include path.

// Implementation, belongs in cpp file
void comparing(std::ifstream& fileIn, std::ifstream& keywords, std::ofstream& outFile)
{
    std::string str;
    std::string word;
    std::set<std::string> keywordsSet;
        int check = 0;

        while (keywords >> word) {
        keywordsSet.insert(word);
        }

    while(fileIn >> str){

        if(str == "<p>") {
            check++;
        }


        if((check > 1 && str == "<p>") || (check == 1 && str == "</body>"))
        {
            outFile << "</p>";
            check--;
        }



    if(keywordsSet.find(str) != keywordsSet.end()) {

        outFile << "<i>" << str << "</i>" << " ";
        }

        else{
        outFile << str << " ";
        }           
    }   

}

然后你可以compare在 any 中使用该函数.cpp,只要包含标题即可。所以你的main.cpp* 文件看起来像这样:

#include "comparing.hpp"

int main(int argc, char* argv[])
{

std::ifstream fileIn;
fileIn.open (argv[1]);

std::ifstream keywords;
keywords.open (argv[2]);

std::ofstream outFile;
outFile.open(argv[3]);

    comparing(fileIn, keywords, outFile);

fileIn.close();
keywords.close();
outFile.close();

return 0;
}

**main.cpp文件永远不需要标题;为什么需要main在代码中的其他地方调用?

于 2013-02-03T10:22:54.440 回答
0

您通常将类和函数声明放在头文件中,然后将.cpp定义和实现放在文件中。

于 2013-02-03T10:13:34.513 回答