将程序拆分为头文件和主文件

时间:2013-02-03 10:11:45

标签: c++ header

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

#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;
}

2 个答案:

答案 0 :(得分:2)

在您的代码中,您声明了一个函数:comparing。您通常将所有声明放在头文件中(MSVC ++使用.h后缀,但我更喜欢.hpp用于C ++标头),以及.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中使用.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

答案 1 :(得分:0)

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