Dynamic Memory Allocation for a Class Attribute

This is a discussion on Dynamic Memory Allocation for a Class Attribute within the C++ Programming forums, part of the General Programming Boards category; Hi, I am fairly new to c++/oop but not to programming. In my current project, I have already written a ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    4

    Dynamic Memory Allocation for a Class Attribute

    Hi,

    I am fairly new to c++/oop but not to programming. In my current project, I have already written a class for complex numbers wich works reliably. I am now trying to write a class for complex polynomials using complex objects as the coeficients. Because the order of the polynomial will not be known until run time, I need to dynamically allocate an array of complex numbers to represent the coeficients. I would like this array to operate as an attribute of the polynomial class. My questions/problems are these:

    1.) I am unsure how to handle the declaration in the .h file. I have tried leaving this out as well as

    Code:
    complex *coef = new complex[];
    And
    Code:
    complex *coef = new complex[order];
    Where order is another attribute defined in the constructor and prototyped imediately above

    Within the .cpp file my code appears as:

    Code:
    //implement constructors
    poly::poly(unsigned int a)
    {
    	order = a;
    	//allocate an array of complex objects as coefficients
    	complex *coef = new complex[order];
    }
    My goal is to creat the pointer so that I can use it in a for loop external to creat objects in the complex class in order to set the value of the coefficients.

    Without any form of declaration I get the expected

    error C2065: 'coef' : undeclared identifier

    With the declaration I get the much more forboding:

    error C2864: 'poly::coef' : only static const integral data members can be initialized within a class

    This leads me to believe that what I would like to do may not be possible, which seems odd

    Can anyone please suggest any ways in which I might fix these errors or restructure the program in a legal manner.

    Thank you in advance.

    Carl

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    You would want
    Code:
    complex *coef;
    in the class declaration, and
    Code:
    coef = new complex[order];
    in the constructor.

    Although you should probably be using a vector instead. Also, unless this is a learning exercise, there is already a complex class in C++ that you should use instead of writing your own.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    4

    Thanks a million

    Thank you for the help. I didn't realize that after using the new command, the memory needs to be addressed as a regular variable. I was trying to continue to use a pointer apperantly without the correct syntax.

    I appreciate the other pointers. I am going to look into using vectors. As for the existing complex class, some of my methods exist in some fairly wierd spaces and have unusual definitions, but thank you for the advise.

    Thanks again.

    Carl

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Memory Allocation (Seg. Fault)
    By dr_jaymahdi in forum C++ Programming
    Replies: 10
    Last Post: 09-30-2007, 02:02 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Dynamic Memory Allocation for fstream (binary)
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2001, 09:52 AM
  5. memory allocation class
    By Orca in forum C++ Programming
    Replies: 2
    Last Post: 10-06-2001, 11:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21