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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:28:13+00:00 2026-05-11T18:28:13+00:00

Thanks to all that responded to my previous thread. There is still a problem

  • 0

Thanks to all that responded to my previous thread. There is still a problem with this simple program that I would like to solve. I am trying to import one class into another as a member object. The output of this program is confusing, though. As a test of the code, I output the scalar contained within the first class object. This works. Then I output the scalar the is contained in the object contained as a member of the second class, and this outputs zero. I assume that something is wrong. Could someone shed some light on this problem? Thanks!

#include <iostream>
using namespace std;

typedef double* doublevec;

// This first class contains a vector and a scalar representing the size of the vector.
typedef class Structure1
{
 int N;
 doublevec vec;
public: 
 // Constructor and copy constructor.
 Structure1(int Nin);
 Structure1(const Structure1& structurein);
 // Accessor functions:
 int get_N() { return N; }
 doublevec get_vec() { return vec; }
 // Destructor:
 ~Structure1() { delete []vec; }
} Structure1;

Structure1::Structure1(int Nin)
{
 N = Nin;
 vec = new double [N];
 for (int i = 0; i < N; i++)
  {
   vec[i] = i;
  };
}

Structure1::Structure1(const Structure1& structurein)
{
 vec = new double[structurein.N];
 for(int i = 0; i < structurein.N; i++)
 {
  vec[i] = structurein.vec[i];
 };
}

// This class just contains the first structure.
typedef class Structure2
 {
  Structure1 structure;
 public:  
  // Constructor:
  Structure2(const Structure1& structurein) : structure(structurein) {}
  // Accessor Function:
  Structure1 get_structure() { return structure; }
  // Destructor:
  ~Structure2() {}
 } Structure2;

int main (int argc, char * const argv[])
{
 const int N = 100;
 Structure1 structure1(N);
 Structure2 structure2(structure1);

 cout << structure1.get_N() << endl;
 cout << structure2.get_structure().get_N();

 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-11T18:28:14+00:00Added an answer on May 11, 2026 at 6:28 pm

    Looks like you don’t set N in Structure1‘s copy constructor. You need:

    Structure1::Structure1(const Structure1& structurein){
     N = structurein.N;
     vec = new double[structurein.N];
     for(int i = 0; i < structurein.N; i++) {
      vec[i] = structurein.vec[i];
     }
    }
    

    (Also, you don’t need a semicolon after a for-loop. But that’s another matter.)

    Edit: This was actually mentioned in an answer to your other question. 😉

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

Sidebar

Ask A Question

Stats

  • Questions 242k
  • Answers 242k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You need to create a constructor for your class that… May 13, 2026 at 7:32 am
  • Editorial Team
    Editorial Team added an answer Does this thread help at all? http://www.webmasterworld.com/forum92/4097.htm May 13, 2026 at 7:32 am
  • Editorial Team
    Editorial Team added an answer The :private part of the var_dump output isn't actually part… May 13, 2026 at 7:32 am

Related Questions

Let's say that I have written a custom e-mail management application for the company
I tried to ask this question previously howver I did not recieve the correct
FIrst, thanks to all who responded to my first two questions. I have tried
I have two strings: the first's value is catdog and the second's is got.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.