简体   繁体   English

未命名类型 C++

[英]Does not name a type C++

I am making a program that hopefully removes tags from html files.我正在制作一个程序,希望能从 html 文件中删除标签。 But when I am copiling the program I get the following error message:但是当我编译程序时,我收到以下错误消息:

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

And I can´t figure it out.我想不通。 Any help would be greatly appreciated.任何帮助将不胜感激。

This is my main for testing the tag remover:这是我测试标签去除器的主要方法:

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


}

This is tag_remover.cc script:这是 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;
}

This is the header file:这是 header 文件:

#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

First thing I notice:我注意到的第一件事:

#include <iterator>

Is nowhere to be found.无处可寻。 It should be in tag_remover.cc它应该在tag_remover.cc

std::istream_iterator<>

Is defined in… <iterator>定义在… <iterator>

Also the TagRemover default constructor seems to be declared but not defined.此外, TagRemover默认构造函数似乎已声明但未定义。

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

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