繁体   English   中英

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

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

我有2个文件Article13Filter.hArticle.cpp ,其中我定义了位于.h文件中的类的所有已声明函数。 但是,他的VS编译器告诉我,在Article13Filter.h文件中,我在每个函数的定义之前写的Article13Filter词(表示它属于该类)不是类或名称空间的名称,即使include指令是正确包含在其相应文件中? 我想问为什么?

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文件:

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

#include "Article.cpp"删除#include "Article.cpp" 通常,您要编译并链接.cpp文件,并包括.h和.hpp文件。

当您包含文件时,它实际上会被复制粘贴到包含文件中。 最终,正在编译的文件看起来像

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

收到错误的原因是,通过在.h文件中包含cpp文件(尤其是在类定义之前),编译器可以在定义类之前看到类方法的声明。

暂无
暂无

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

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