不命名类型 C++

Does not name a type C++

我正在制作一个程序,希望能从 html 个文件中删除标签。但是当我编译程序时,我收到以下错误消息:

tag_remover.cc:11:1: error: ‘TagRemover’ does not name a type
   11 | TagRemover::TagRemover(std::istream& in) {
      | ^~~~~~~~~~
tag_remover.cc:21:14: error: ‘TagRemover’ has not been declared
   21 | std::string& TagRemover::remove(std::string& s) {
      |              ^~~~~~~~~~
tag_remover.cc: In function ‘std::string& remove(std::string&)’:
tag_remover.cc:32:1: warning: no return statement in function returning non-void [-Wreturn-type]
   32 | }
      | ^
tag_remover.cc: At global scope:
tag_remover.cc:34:13: error: ‘TagRemover’ has not been declared
   34 | std::string TagRemover::print(std::ostream& out) const {
      |             ^~~~~~~~~~
tag_remover.cc:34:50: error: non-member function ‘std::string print(std::ostream&)’ cannot have cv-qualifier
   34 | std::string TagRemover::print(std::ostream& out) const {
      |                                                  ^~~~~
tag_remover.cc: In function ‘std::string print(std::ostream&)’:
tag_remover.cc:35:9: error: ‘str’ was not declared in this scope; did you mean ‘std’?
   35 |  out << str << endl;
      |         ^~~
      |         std
make: *** [<builtin>: tag_remover.o] Error 1

我想不通。任何帮助将不胜感激。

这是我测试标签移除器的主要内容:

#include <iostream>
#include <fstream>
#include <string>
#include "tag_remover.h"
using std::string;


int main() { 
    std::cout << "Opening html file" << std::endl;
    std::ifstream in1("tags.html");


    std::cout << "Removing tags ....." << std::endl;
    TagRemover test1(in1);

    test1.print(std::cout);
    std::cout << "Test Done!" << std::endl;


}

这是 tag_remover.cc 脚本:

#include <iostream>
#include <istream>
#include <ostream>
#include <fstream>

#include "tag_remover.h"

using std::endl;
using std::cout;
using std::string;
using std::istream;




TagRemover::TagRemover(std::istream& in) {
    //remove white spaces
    std::noskipws(in);
    std::istream_iterator <char> itr1(in);
    std::istream_iterator <char> itr2{};
    std::string s(itr1, itr2);

    str = remove(s);

}
std::string& TagRemover::remove(std::string& s) {
    while(s.find("<") != std::string::npos) {
        auto startpos = s.find("<");
        auto endpos = s.find(">");

        if(endpos == std::string::npos) {
            break;
        }
        auto eraseTo = (endpos - startpos) +1;
        s.erase(startpos, eraseTo);
    }
    return s;
}

std::string TagRemover::print(std::ostream& out) const {
    out << str << endl;
    return str;
}

这是头文件:

#ifndef TAG_REMOVER
#define TAG_REMOVER

#include <string>
#include <iostream>
//#include <iterator>
class TagRemover {
public:
    TagRemover();
    TagRemover(std::istream& in);

    std::string print(std::ostream& out) const;
    std::string& remove(std::string& s);

private:

    std::string str;

        //const std::string inLine;


};


#endif

我注意到的第一件事:

#include <iterator>

无处可寻。它应该在 tag_remover.cc

std::istream_iterator<>

定义于... <iterator>

另外 TagRemover 默认构造函数似乎已声明但未定义。

此外,您可以这样定义TagRemover::TagRemover(std::istream&)

TagRemover::TagRemover(std::istream& in) 
  : str{ [] (auto& is) {
    std::skipws(is); // ignore spaces
    using iser = std::istream_iterator<char>;
    return std::string(iser(is), iser());
  }(in) }
{}