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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:29:56+00:00 2026-06-13T13:29:56+00:00

I am having trouble finding a solution to my homework. Here’s what I am

  • 0

I am having trouble finding a solution to my homework. Here’s what I am trying to do:
I have a list of objects stored into a vector. The vector, called “AvailableObjects” is declared globally.

vector<const Object*> AvailableObjects; 

…

void       read_obj_file(const char* filename){
ifstream infile (filename, ios::in);

while(!infile.eof()) {
        char name[20];
        int attribute;
        int size = 0;
        infile >> name >> attribute >> size;
        AvailableObjects.push_back(new Object(name, attribute, size));
        }
 infile.close();
 return;
 }

After reading the objects, I need to write a function to generate a single object, and push it on to a stack of objects available to the user.

Object*     generate_object(){
        return AvailableObjects.at(rand_in_range(1,AvailableObjects.size()));
        }

The code above is what I wanted to use. I need to randomly select one of the objects stored in the vector, and return a pointer of that object back to whatever called the function. However, this cannot be done as the objects in the vector are const Object*, not simple Object*. This is a homework assignment, so I cannot modify the const values, they prototypes must remain the same. Lastly, I will share the object class. It has a constructor dedicated to creating a new object when passed a const Object*, but I cannot make the constructor work as it is intended.

/**
 * object.h
 *
 * Objects are immutable (read-only) once instantiated.
 */
#ifndef OBJECT_H
#define OBJECT_H

#include<string>
    using std::string;

class object{
    private:
    string _name;
    int    _attribute;
    int    _size;

    public:
    // Constructor
    Object(string name, int attribute, int size){
        _name  = name;
        _attribute = attribute;
        _size  = size;
    }
    Treat(const Treat& t){
        _name  = t._name;
        _attribute = t._attribute;
        _size  = t._size;
    }
    // Accessors
    string name() const {return _name;}
    int    attribute()const {return _attribute;}
    int    size() const {return _size;}
    };

    #endif

And here, also, is a function displayed throughout the code that chooses a random number in a particular range.

    int rand_in_range(int low, int high){
        init_rand();
        high < low ? throw "Invalid range." : 0 ;
        int modbase = ((high+1) - low);
        int ans = 0;
        ans = rand() % modbase + low;
        return ans;
        }

Thanks for any response, I’ll be actively watching this, so, should anyone have any questions, I’ll be happy to reply. Again, just to summarize, I need help to get my generate_object function to return an Object* using the vector of const Object* available to me.

  • 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-13T13:29:57+00:00Added an answer on June 13, 2026 at 1:29 pm

    Vectors are zero-indexed, so the valid range of AvailableObjects.at is 0 to AvailableObjects.size()-1.

    Presuming Treat(const Treat& t) should be Object(const Object& t) and that you’ve made a mistake in transcribing, then it does not take a const Object* as you said. Since it takes a const reference, not a const pointer, you must dereference your pointer. For example, if we wanted to make a deep copy of the fifth object in AvailableObjects we’d do:

    int index = 4; // 0, 1, 2, 3, 4... thus index 4 refers to the fifth item.
    const Object* object = AvailableObjects.at(index);
    Object* clone = new Object(*object);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble finding a solution to this via google but I would have
Having trouble finding a solution for my situation here. Sorry if this has been
I am having trouble in finding a solution to my problem. I am trying
I'm having trouble finding the right syntax to replace an array of objects nested
Ok, This seems straight forward but I am having trouble finding the solution. I
I'm a newbie to Silverlight and I'm having some trouble finding a solution to
I'm having trouble finding a correct regex to achieve what I want. I have
I've done research on this problem, but am having trouble finding a solution. I
I'm having trouble finding the right solution for this fully fluid layout. I want
I am having some trouble finding the solution for the Join all exercise 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.