lundi 6 décembre 2021

Does not name a type C++

I am making a program that hopefully removes tags from html files. 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:

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

#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

Aucun commentaire:

Enregistrer un commentaire