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 527217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:51:32+00:00 2026-05-13T08:51:32+00:00

Well, I think my problem originates in a lake of knowledge of basic C++

  • 0

Well, I think my problem originates in a lake of knowledge of basic C++ concepts. The problem is, in my code (below) I have the classes Header and Register. For both, I pass a reference to a ifstrem file already opened. The Header reads some bytes from it. Register has a method to return a reference of Header (which is passed in Register constructor).

The problem is, when I declare the reference to Header as const (in my Register class), I’ve got an error message saying:

error C2662: 'Header::Field_1' : cannot convert 'this' pointer from 'const Header' to 'Header &'

The problem, I guess, is:

Field_1 method changes the file cursor through seekg. therefore this member object cannot be const (its internal structure is being changed). Being fstream _stream declared inside Header class, and having a const reference for this class does it make all its internal members const as well?

PS: I’m using VS C++ express. The code below doesn’t do anything useful, is just an example.

// ********** HEADER.H ***********
#ifndef __HEADER_H__
#define __HEADER_H__

#include <fstream>

class Header
{
    private:

        std::ifstream* _stream;
        unsigned long field1;

    public:

        Header(std::ifstream* stream);
        ~Header() { }

        unsigned long Field_1(void);
};

class Register
{
    private:
        const Header& _header;
        std::ifstream* _stream;

    public:

        Register(const Header& header, std::ifstream* stream);
        ~Register(){ }

        const Header& GetHeader(void) { return _header; }
};

#endif /*__HEADER_H__*/

//********* PROGRAM.CPP *************

#include <iostream>
#include "header.h"

using namespace std;

Header::Header(ifstream* stream) : _stream(stream)
{
}

unsigned long Header::Field_1(void)
{
    _stream->seekg(0x00, fstream::beg);
    _stream->read(reinterpret_cast<char*>(&field1), sizeof(field1));
    return field1;
}


Register::Register(const Header& header, std::ifstream* stream): _header(header), _stream(stream)
{
}

int main(void)
{
    ifstream file("test.dat", ios::binary | ios::in);
    Header h(&file);
    Register reg(h, &file);

    cout << "Field 1 " << reg.GetHeader().Field_1() << endl;

    file.close();
    return 0;
}
  • 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-13T08:51:32+00:00Added an answer on May 13, 2026 at 8:51 am

    Yes, when GetHeader() returns a const reference to the Header object, you can only call const methods on it.
    In this case if you declare Field_1as a const method by writing:

    unsigned long Header::Field_1(void) const

    (note the const on the end)

    The trouble is if you do this then within the const method all other members appear as if they are const too (unless you mark them as mutable).

    As a result you will probably have trouble with the reinterpret_cast<char*>(&field1).

    In which case the simplest solution is probably to have GetHeader() return a non-const reference. Alternatively you could mark field1 as mutable (but make sure you understand the semantics and dangers of mutable first if you go down that route. My feeling is that making GetHeader() return a non const reference is what you really want here)

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

Sidebar

Related Questions

I think i have a basic problem in understanding the dojo toolkit. Well I
Well, I think I have a very basic doubt here: I'm developing an app
well I think I know what the problem is. I am just having a
Well, I think my problem is a little bit interesting and I want to
Visual Studio 2008 - framework 3.5 - Visual Basic Hi! I have a problem
Having some problems with a piece of Jquery code, well that's where I think
Well I think I know the answer to this, but I would appreciate anybody
Well I don't think that it's really important but since the program has to
I think I know C++ reasonably well and I am thinking about implementing something
Do you think you are a java wizard? That you are well versed in

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.