简体   繁体   中英

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. 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? I would like to ask why is that?

Article13Filter.h file:

#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:

#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. In general, you want to compile and link the .cpp files and include the .h and .hpp files.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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