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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:35:47+00:00 2026-06-10T05:35:47+00:00

I am trying to learn how to use constant functions and objects, however, I

  • 0

I am trying to learn how to use constant functions and objects, however, I have some error that has kept me up for over an hour and I can’t seem to figure out. I was following a simple example and I guess I got lost somewhere along the way. Here is my code.

Main.cpp

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

int main(){
    ExampleClass exampleObj; // object used to call members of ExampleClass.
    exampleObj.printText(); // calls printVar from the ExampleClass.

    const ExampleClass constantObject; // object used to call constant members of ExampleClass.
    constantObject.printConstText(); // calls printConstVar from the ExampleClass.

    return 0;
}

ExampleClass.h

#ifndef EXAMPLECLASS_H
#define EXAMPLECLASS_H


class ExampleClass
{
    public:
        void printText();
        void printConstText() const;
};

#endif // EXAMPLECLASS_H

ExampleClass.cpp

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

void ExampleClass::printText(){
    std::cout << "The code works!" << "\n";
}

void ExampleClass::printConstText() const{
    std::cout << "The code works!" << "\n";
}

And I’m getting the error:

C:\Documents and Settings\Me\My Documents\ConstObjects\main.cpp||In function 'int main()':|
C:\Documents and Settings\Me\My Documents\ConstObjects\main.cpp|8|error: uninitialized const 'constantObject'|
||=== Build finished: 1 errors, 0 warnings ===|

If I take out the const before ExampleClass the code executes fine. But is it still a constant object? Thanks for the help, I hope I gave enough information. If it matters at all I’m using Code Blocks.

  • 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-10T05:35:48+00:00Added an answer on June 10, 2026 at 5:35 am

    Your ExampleClass is a POD (plain old data) structure. When it’s declared as a local variable like this ExampleClass exampleObj no default constructor gets called and it remains uninitialized.

    You need either to create a default constructor of your own or use the following syntax –ExampleClass exampleObj = ExampleClass();. This will create a temporary ExampleClass object and value initialize your exampoleObj.

    updated:

    Here is an excerpt from C++03 standard 8.5.9.

    If no initializer is specified for an object, and the object is of
    (possibly cv-qualified) non-POD class type (or array thereof), the
    object shall be default-initialized;

    if the object is of const-qualified type, the underlying class type shall have a
    user-declared default constructor
    . [this one applies to const objects]

    Otherwise, if no initializer is
    specified for a nonstatic object, the object and its subobjects, if
    any, have an indeterminate initial value); if the object or any of its
    subobjects are of const-qualified type, the program is ill-formed.
    [this one applies to const and POD types]

    This means that the constantObject should have user-defined default constructor, otherwise a program is ill-formed, which should be diagnosed. If we remove const, the object will remain uninitialized anyway (will have indeterminate initial value)

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

Sidebar

Related Questions

I have been trying to learn to use github, it has been fine until
im trying to learn how to use javascript objects, and got some issue here.
I am trying to learn to use JavaCC and realized that it has support
I'm trying to learn how to use the force.layout features in D3.js and have
I am trying to learn and use html5 and have a basic layout but
I am trying to learn how to use Cascadding Style Sheets. I have a
I'm trying to learn to use ANTLR, and seem to have come across an
I'm trying to learn to use only the keyboard with some applications, one of
I'm trying to learn how to use PHP's DOM functions . As an exercise,
I am trying to learn and use Blocks effectively. On the web, I have

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.