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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:08:46+00:00 2026-06-02T01:08:46+00:00

I’m a newbie to c++ and am trying to do some basic object creation

  • 0

I’m a newbie to c++ and am trying to do some basic object creation using dynamic memory.
I’m passing an int argument to a method and it is changing the value of the global variable. I think it has something to do with the way I’m allocating memory for new objects, I cant get it to compile any other way.

int main () {
    int inp;
    CRectangle rectb (2,2);
    cout << "enter number of items to add" << endl;
    cin >> inp; // let's say inp = 7
    rectb.addItemsArray(inp);
    cout << "inp after adding items: " << inp << endl; // inp is now 1.
}

header files:

class CRectangle {
    int width;
    int height;
    item *items[]; // SOLUTION: change this line to "item *items"
    int input;

public:
        CRectangle (int,int);
        int addItemsArray(int);
        int area () { return (width*height); }
        int get_items(int);

};

-and-

class item {
    int foo;
    char bar;
public:
    //SOLUTION add "item ();" here (a default constructor declaration without arguments)
    item (int, char);
    int get_foo();
    char get_bar();
};

method:

int CRectangle::addItemsArray(int in) {
    cout << "value of in at begginning:" << in << endl; //in = 7
    int i;
    i = 0;
    //SOLUTION: add "items = new item[in];" on this line.
    while (i < in) {
        items[i] = new item(1, 'z'); //SOLUTION: change this line to "items[i] = item(1, 'z');"
        i++;
    }
    cout << "value of in at end " << in << endl; //in = 7
    return 1;
}

Sometimes I get a bus error or seg fault. Sometimes it works as expected with lower numbers like 2 or 3, but not always.

Any help would be greatly appreciated.

Edit (CRectangle’s constructor):

CRectangle::CRectangle (int a, int b) {
    width = a;
    height = b;
} 

(item’s constructor):

/* SOLUTION add default item constuctor
item::item() {
    foo = 0;
    bar = 'a';
}
*/

item::item(int arg, char arg2) {
    foo = arg;
    bar = arg2;
}
  • 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-02T01:08:48+00:00Added an answer on June 2, 2026 at 1:08 am

    It looks like you forgot to create the items array…

    You defined a dynamic allocated array (not item *items[100], but item *items[]). Before you can use the array, you have to allocate memory to hold the items:

    items = new item[100];
    

    and dont forget to delete it with

    delete [] items; 
    

    at the end. 😉

    And instead of

    int i;
    i = 0;
    while (i < in) {
       items[i] = new item(1, 'z');
       i++;
    }
    

    i would use

    for (int i=0; i<in; i++)
    {
       items[i] = new item(1, 'z');
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am trying to render a haml file in a javascript response like so:

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.