简体   繁体   English

在C ++中的单独的.cpp文件中,以头文件编写的类的成员函数的定义

[英]Definition of member functions of a class written in a header file, in a separate .cpp file in C++

I have 2 files Article13Filter.h and Article.cpp in which i define all of the declared functions of the class located in the .h file. 我有2个文件Article13Filter.hArticle.cpp ,其中我定义了位于.h文件中的类的所有已声明函数。 However, he VS compiler tells me that in the Article13Filter.h file the Article13Filter word which I write before the definition of every function (to denote that it belongs to that class) is not a class or namespace name, even though are include directives are correctly included to their corresponding files? 但是,他的VS编译器告诉我,在Article13Filter.h文件中,我在每个函数的定义之前写的Article13Filter词(表示它属于该类)不是类或名称空间的名称,即使include指令是正确包含在其相应文件中? I would like to ask why is that? 我想问为什么?

Article13Filter.h file: Article13Filter.h文件:

#ifndef ARTICLE_13_FILTER_H
#define ARTICLE_13_FILTER_H
#include <string>
#include <vector>
#include <set>
#include "Article.cpp"
class Article13Filter {
private:
    std::set<std::string> copyrighted;
    std::vector<std::string> blocked;
public:
    Article13Filter(std::set<std::string> copyrighted);
    bool blockIfCopyrighted(std::string s);
    bool isCopyrighted(std::string s);
    std::vector<std::string> getBlocked();
};
#endif // !ARTICLE_13_FILTER_H

Article .cpp file: Article .cpp文件:

#include <iostream>
#include <string>
#include <set>
#include "Article13Filter.h"
using namespace std;
bool Article13Filter::isCopyrighted(string s) {
    for (set<string>::iterator it = copyrighted.begin(); it != copyrighted.end(); it++) {
        if (*it == s) {
            return true;
        }
        return false;
    }
}
bool Article13Filter::blockIfCopyrighted(string s) {
    if (isCopyrighted(s)) {
        return false;
    }
    return true;
}
vector<string> Article13Filter::getBlocked() {
    return blocked;
}

Remove #include "Article.cpp" from Article13Filter.h. #include "Article.cpp"删除#include "Article.cpp" In general, you want to compile and link the .cpp files and include the .h and .hpp files. 通常,您要编译并链接.cpp文件,并包括.h和.hpp文件。

When you include a file it is effectively copy-pasted into the including file. 当您包含文件时,它实际上会被复制粘贴到包含文件中。 Ultimately the file being compiled looks something like 最终,正在编译的文件看起来像

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

#include <string>
#include <vector>
#include <set>
#include <iostream>
#include <string>
#include <set>


using namespace std;
bool Article13Filter::isCopyrighted(string s) {
    for (set<string>::iterator it = copyrighted.begin(); it != copyrighted.end(); it++) {
        if (*it == s) {
            return true;
        }
        return false;
    }
}
bool Article13Filter::blockIfCopyrighted(string s) {
    if (isCopyrighted(s)) {
        return false;
    }
    return true;
}
vector<string> Article13Filter::getBlocked() {
    return blocked;
}

class Article13Filter {
private:
    std::set<std::string> copyrighted;
    std::vector<std::string> blocked;
public:
    Article13Filter(std::set<std::string> copyrighted);
    bool blockIfCopyrighted(std::string s);
    bool isCopyrighted(std::string s);
    std::vector<std::string> getBlocked();
};


using namespace std;
bool Article13Filter::isCopyrighted(string s) {
    for (set<string>::iterator it = copyrighted.begin(); it != copyrighted.end(); it++) {
        if (*it == s) {
            return true;
        }
        return false;
    }
}
bool Article13Filter::blockIfCopyrighted(string s) {
    if (isCopyrighted(s)) {
        return false;
    }
    return true;
}
vector<string> Article13Filter::getBlocked() {
    return blocked;
}

The errors you are receiving are because by including the cpp file in the .h file, particularly ahead of the class definition, the declarations of the class methods are seen by the compiler before the class is defined. 收到错误的原因是,通过在.h文件中包含cpp文件(尤其是在类定义之前),编译器可以在定义类之前看到类方法的声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM