Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8256785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:58:36+00:00 2026-06-08T01:58:36+00:00

I have a class as outlined below: class InputReader { public: typedef void (*handler)(std::string,

  • 0

I have a class as outlined below:

class InputReader 
{  
public:
  typedef void (*handler)(std::string, int);
  static void errorHandler(std::string error, int severity); //Supplies a default error handler
  static void warningHandler(std::string error, int severity); //Supplies a default warning handler
  handler errorH;
  handler warningH;

  InputReader(std::string pwd = "", handler eHandler = errorHandler, handler wHandler = warningHandler);

  bool readFile(std::string filename);
  std::vector<first> mesh;
  //other irrelevant objects that need to be read into
};

first is a struct:

struct first
{
  std::string filename;
  double scale;
};

With Mooing Duck‘s help I have:

std::istream& operator>>(std::istream& file, first& obj) 
{
  std::string symbol;
  while(file >> symbol) 
  {
    if (symbol[0] == '#') 
    {
      std::getline(file, symbol);
    } 
    else if (symbol == FIRSTTAGEND) 
    {
      break;
    }
    else if (symbol == FILEPATH) 
    {
      if (!(file >> '=' >> obj.filename))
        std::cerr << symbol << " is incorrectly formatted"; //This needs to use errorH
    } 
    else if (symbol == SCALE) 
    {
      if (! (file >> '=' >> obj.scale) )
        std::cerr << symbol << " is incorrectly formatted"; //This needs to use errorH
    } 
    else 
    { //not a member: failure
      std::cerr << symbol << " is not a member of first";
      file.setstate(file.rdstate() | std::ios::badbit);
      break;
    }
  }
  return file;
}

std::istream& operator>>(std::istream& file, InputReader& obj) 
{
  std::string symbol;
  while(file >> symbol) 
  {
    if (symbol[0] == '#') 
    {
      std::getline(file, symbol);
    } 
    else if (symbol == FIRSTTAGBEG) 
    {
      first t;
      if (file >> t)
        obj.mesh.push_back(t);
    } 
    else 
    {
      obj.errorH(symbol + " is not a member of the input reader.", 1);
      file.setstate(file.rdstate() | std::ios::badbit);
    }
  }
  return file;
}

bool InputReader::readFile(std::string filename)
{
  std::ifstream infile;
  infile.open(filename.c_str());
  infile >> *this;
  return true;
}

errorH gets set upon construction of the InputReader object. It can be provided by the user of the class, otherwise, it uses the default ones I provide. The only problem is that I can’t access errorH when first is being read into. How can I solve this?

Problem constraints: External libraries are not allowed. C++11/C++OX is not allowed.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T01:58:37+00:00Added an answer on June 8, 2026 at 1:58 am

    You naturally have few options:

    1. Do not use >> operator and create a function that does the same but accepts three arguments – an input stream, a first object and an input reader object.
    2. Define operator >> that accepts a tuple of first and InputReader as a second argument (i.e. a pair of pointers). For example: std::istream& operator>>(std::istream& file, std::pair<first *, InputReader *> & obj).

    You can extend this list indefinitely as long as you have a good imagination.

    Hope it helps.

    UPDATE:

    Here goes some simple example:

    #include <string>
    #include <vector>
    #include <fstream>
    #include <iostream>
    
    struct first {
        std::string filename;
        double scale;
    };
    
    class InputReader {
      public:
        typedef void (*handler)(const std::string &, int);
    
        InputReader(const std::string & pwd = std::string(),
                    handler eHandler = errorHandler,
                    handler wHandler = warningHandler);
    
        bool readFile(const std::string & filename);
    
        static void errorHandler(const std::string & error, int severity);
        static void warningHandler(const std::string & error, int severity);
    
        handler errorH;
        handler warningH;
        first firstobj;
        std::vector<first> mesh;
    };
    
    std::istream & operator >> (std::istream & file,
                                std::pair<first, InputReader *> & obj)
    {
        std::string symbol;
        while (file >> symbol) {
            if (symbol[0] == '#') {
                std::getline(file, symbol);
            } else if (symbol == "FIRSTTAGEND") {
                break;
            } else if (symbol == "FILEPATH") {
                if (!(file >> obj.first.filename))
                    obj.second->errorHandler(symbol + " is incorrectly formatted",
                                             1);
            } else if (symbol == "SCALE") {
                if (!(file >> obj.first.scale))
                    obj.second->errorHandler(symbol + " is incorrectly formatted",
                                             1);
            } else { //not a member: failure
                std::cerr << symbol << " is not a member of first";
                file.setstate(file.rdstate() | std::ios::badbit);
                break;
            }
        }
        return file;
    }
    
    std::istream & operator>>(std::istream & file, InputReader & obj)
    {
        std::string symbol;
    
        while (file >> symbol) {
            if (symbol[0] == '#') {
                std::getline(file, symbol);
            } else if (symbol == "FIRSTTAGBEG") {
                std::pair<first, InputReader *> t(first(), &obj);
                if (file >> t)
                    obj.mesh.push_back(t.first);
            } else {
                obj.errorH(symbol + " is not a member of the input reader.", 1);
                file.setstate(file.rdstate() | std::ios::badbit);
            }
        }
        return file;
    }
    
    bool InputReader::readFile(const std::string & filename)
    {
        std::ifstream infile;
        infile.open(filename.c_str());
        infile >> *this;
        return true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have class with collection as below public class MyClass:IXmlSerializable { int vesrion; private
I have class with back reference: public class Employee : Entity { private string
I have a State class that is fully implemented as outlined below. I also
I have following DTO @XStreamAlias(outline) public class OutlineItem implements java.io.Serializable { private static final
I have class like this below shown. which contains the shopping items where the
I have class that looks like this: class A { public: class variables_map vm
I have a class, the outline of which is basically listed below. import org.apache.commons.math.stat.Frequency;
I have created a Sub-Class of NSOutlineView and used the below code to make
I have Class and Student objects. Both have collection of another as property. Which
I have class LegacyClass which inherits OldBaseClass. I'm considering a change to introduce a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.