Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将程序拆分为头文件和主文件_C++_Header - Fatal编程技术网

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

C++ 将程序拆分为头文件和主文件,c++,header,C++,Header,我一直在尝试将我的程序分为头文件和主文件,但不太清楚头文件到底如何处理这个文件。任何试图理解这一点的帮助都将不胜感激 #include <iostream> #include <fstream> #include <string> #include <set> void comparing(std::ifstream& fileIn, std::ifstream& keywords, std::ofstream& out

我一直在尝试将我的程序分为头文件和主文件,但不太清楚头文件到底如何处理这个文件。任何试图理解这一点的帮助都将不胜感激

#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;
}
#包括
#包括
#包括
#包括
无效比较(std::ifstream和fileIn,std::ifstream和关键字,std::ofstream和outFile)
{
std::字符串str;
字符串字;
std::set关键字set;
整数检查=0;
while(关键字>>word){
关键字设置。插入(word);
}
while(fileIn>>str){
如果(str==“”){
check++;
}
如果((check>1&&str==“”)| |(check==1&&str==”)
{

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

在代码中声明一个函数:
比较
。通常将所有声明放在头文件中(MSVC++使用了<代码> .H/COD>后缀,但我更喜欢 .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
#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 << " ";
        }           
    }   

}
#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;
}
然后,您可以在任何
.cpp
中使用
compare
功能,只要包含标题即可。因此,您的
main.cpp
*文件如下所示:

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

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


<>代码>主体.CPP< /Cord>文件不需要标题;为什么你需要在代码中别处调用<代码>主<代码>?< /P>你应该投资一本好的C++书籍。