trouble initializing constructor

This is a discussion on trouble initializing constructor within the C++ Programming forums, part of the General Programming Boards category; Code: class List { public: List(const int* arr, int sz); //initializes list object by copying the data from the array ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    61

    trouble initializing constructor

    Code:
      class List
    {
    
      public:
        List(const int* arr, int sz);
                   //initializes list object by copying the data from the array arr (of size sz) into it
    
    
        int HowMany(int val); //returns the number of times the value val appears in the list
    
    
        void PrintSquares();  //prints squares of data elements in list, separated by commas
    
    
      private:
        int data[100]; //list of up to 100 numbers
        int size; //number of values currently stored
    
    };
    This is my header file. The following code now is the definition file. I'm supposed to initialize the constructor with a constant int pointer and that is the part that is giving me the trouble. I'm not so sure if i defined my other functions correctly. But if anyone can give me a heads up on how to go about solving the constructor problem and analyzing if I did the other functions right, I would really appreciate it. Thank you.

    Code:
     #include <iostream>
    #include "list2.h"
    using namespace std;
    
    
     List::List(const int* arr, int sz)
    {
                   //initializes list object by copying the data from the
                   //array arr (of size sz) into it
    
       
     arr[data];// this part trips me out, I also tried this
      size=sz;
    
    }
    
    int List:: HowMany(int val) //returns the number of times the value
                                    //val appears in the list
    {
        int v=0;
         for(int i=0; i<size; i++)
    
     if (data[i]==val)
            v++;
     
          return v;
    } 
    
     
     
    void List:: PrintSquares()  //prints squares of data elements in list,
                                    //separated by commas
    { 
     for(int i=0; i<size; i++)
     {
      cout<<data[i]*data[i];
    
        for (int j=0;j<data[i];j++)
       cout<<',';
    
     cout<<endl;
     }
        
    }
    Last edited by dantestwin; 07-04-2004 at 08:00 PM.

  2. #2
    Registered User
    Join Date
    Jul 2004
    Posts
    61
    Hmmm K. Put the code tags but it didn't print out in a nice box, why???

  3. #3
    Climber spoon_'s Avatar
    Join Date
    Jun 2002
    Location
    ATL
    Posts
    182
    Use the brackets, [ and ], instead of < and > for your code tags...
    {RTFM, KISS}

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    61
    ahh thanks Spoon.

  5. #5
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    Code:
     arr[data];    //this is bad
      size=sz;
    You want to dynamicly create arr based on the value of sz. You also want to keep track of that with size, so you've got that part aokay.

  6. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    61
    Hmmm, you mean something like this?

    size=sz;
    arr= new int [size];


    I'm a bit confused. I'm just now starting to learn about dynamic allocation. It compiles fine but i'm not sure if i'm on the right track. Thanks for your help.

  7. #7
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I'm not entirely sure what the goal here is. Are you trying to make some sort of linked list? If you are, then you don't want to just dynamically allocate an array.

    Is this an assignment for a class or an excercise out of book? Maybe you could clarify what you want to accomplish.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  8. #8
    Registered User
    Join Date
    Jul 2004
    Posts
    61
    I just need to initialize list object by copying the data from the array arr (of size sz) into it. I'm stuck on that part.

    This isn't for an assignment.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I just need to initialize list object by copying the data from the array arr (of size sz) into it.
    Code:
    #include <stdexcept>
    
    List::List(const int* arr, int sz)
    {
      if (sz >= 100) {
        throw std::out_of_range("sz too large");
      }
      for (int i = 0; i < sz; i++) {
        data[i] = arr[i];
      }
    }
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    Jul 2004
    Posts
    61
    Thank you all for your help on my constructor problem, I really appreciate it.

    Just one more question about this problem. For my PrintSquares() function. To print out commas in between the integers but not after the last comma, is my for loop laid out correctly? should I replace the i<data[i] condition with i<size? I'm leaning towards the first one but I'm not too sure.

    Code:
      for(int i=0; i<size; i++)
         {
          cout<<data[i]*data[i];     
     
          for (int j=0;j<data[i];j++)   
          cout<<',';
         }
    You guys rule. Thanks for helping out a programming novice.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    You either need to treat the first item specially or the last item specially. To do it by focusing on the first item, you print the item outside of the loop and then start with leading commas:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int
    main()
    {
      int i = 0;
    
      cout<< i;
      for (i = 1; i < 10; i++) {
        cout<<", "<< i;
      }
      cout<<endl;
    }
    To focus on the last item, you need to test for whether you're at the end or not:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int
    main()
    {
      int i;
    
      for (i = 0; i < 10; i++) {
        cout<< i;
        if (i != 9) {
          cout<<", ";
        }
      }
      cout<<endl;
    }
    Those examples should be easy to translate into what you need done.
    My best code is written with the delete key.

  12. #12
    Registered User
    Join Date
    Jul 2004
    Posts
    61
    Wow, thanks a lot. Makes sense. Thank you very much and everyone else to for helping me out. I really appreciate it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 06-10-2008, 09:38 PM
  2. C++ have a constructor call another constructor
    By QuestionC in forum C++ Programming
    Replies: 4
    Last Post: 05-17-2007, 02:59 AM
  3. Replies: 3
    Last Post: 03-26-2006, 12:59 AM
  4. Initializing array and non-standard constructor
    By Jasel in forum C++ Programming
    Replies: 15
    Last Post: 11-10-2003, 02:56 PM
  5. newbie - copy constructor - trouble
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 02-19-2002, 09:56 PM

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