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

  • SEARCH
  • Home
  • 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 7402693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:48:23+00:00 2026-05-29T04:48:23+00:00

My Record Constructor looks like this: #include Record.h //both these headers #include <iostream> #include

  • 0

My “Record” Constructor looks like this:

#include "Record.h"  //both these headers #include <iostream>
#include "String.h"

Record :: Record(std::ifstream& is)
{
    std :: istream & iss = static_cast<std::istream &>(is);
    iss >> ID >> medium >> rating;
    getline(iss, title);        
    if (!iss.good())        
        throw Record_exception(invalid_data);
}

where ID is an int, medium and title are a user-defined type String (not the standard string) for which operator>> , getline, and operator<< are defined on an istream as follows

#include "String.h"

ostream& operator << (ostream& os, const String & s)
{   
    int str_length = s.size(); 
    for (int i = 0; i < str_length ; i++)
        os << s[i];
    return os;
}

istream& operator >> (istream& is, String& str)
{
    char input;
    bool string_end = false;
    int length = 0;
    str.clear();
    while (!string_end){
        is.get(input);
        if (!isspace(input) && is){
            length++;
            str +=input;
             }else if (length || !is)
            string_end = true;
    }   
    return is;
}


istream& getline(std::istream& is, String& str)
{
    char input;
    bool string_end = false;
    int length = 0;
    str.clear();
    while (!string_end){
        is.get(input);
        if (!isspace(input) && is){
            length++;
            str +=input;
        }else if ((length && is.peek() == '\0') || !is)
            string_end = true;
    }   
    return is;
}

i static_casted the ifstream to an istream because i realized the compiler wasn’t accepting my “getline” and operator<< for String and operator<< for string with an ifstream. I still do not know if this is the correct approach however

my compiler error NOW includes:

“Record.cpp: In constructor ‘Record::Record(std::ifstream&)’:
Record.cpp:26: error: invalid static_cast from type
‘std::basic_ifstream >’ to type
‘std::istream&’

i am compiling with flags “g++ -c -pedantic -Wall -fno-elide-constructors” on a red_hat linux machine

  • 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-05-29T04:48:24+00:00Added an answer on May 29, 2026 at 4:48 am

    In your case, I think you have to understand that ifstreams implement the istream interface. Your Record constructor actually does not need an ifstream. All the code inside makes use only of istream’s public interface. So you only need to make your Record constructor take istream& (not ifstream&). This will also make your Record class more general since you can use it with all kinds of istreams, not just file input streams.

    However, your String class overloads look fine. I suspect from the way you’re trying to static_cast fstream to istream (this is an upcast and should never be required explicitly and also shouldn’t be done), the original problem you were trying to work around is that you don’t have the proper headers included in all relevant areas. Make sure you have istream, ostream, and fstream included where necessary, and not simply iosfwd or iostream. If the compiler is giving you errors trying to pass an fstream reference to a function that expects istream&, something is definitely wrong and the most likely explanation is forward declarations and missing includes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm adding a record like this: Dim pathString As String = HttpContext.Current.Request.MapPath(Banking.mdb) Dim odbconBanking
I have data from a CD or Record currently setup like this: class Release
Castle Active Record? Plain NHibernate? Subsonic? Entity? Something like Nettiers? I'd like to use
I'm pattern matching on the data constructor of a record, and I have the
What is the best way to implement a constructor for a record? It seems
Suppose I have this class: type Pet (name:string) as this = let mutable age
Abridged schema looks like Table: Apartment id | city | price ... Table Amenity:
I'd like to record some dynamic behaviors into some global variables. So I wrote
How to create a record of list of string and, pair of in Haskell
My record sheet app has a menu option for creating a new, blank record

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.