Connecting Tech Pros Worldwide Forums | Help | Site Map

Dynamic array problems

blackstormdragon's Avatar
Member
 
Join Date: Feb 2007
Posts: 32
#1: Apr 27 '07
It seems pointers and dynamic arrays are giving me a hard time.
Heres part of the assignment. We have to create a class named Student that has three member variables. One of the variables is called classList – "A dynamic array of strings used to store the names of the classes that the student is enrolled in"

My problem is when Im trying to send classList from my input function to my setClassList function. I keep getting the error "subscript requires array or pointer type' and I have no clue what that means. Heres some pieces of my code.
Expand|Select|Wrap|Line Numbers
  1. typedef char* CharPtr;
  2.  
  3. class Student
  4.  
  5. {
  6. public:
  7.     ~Student();
  8.     void input();
  9.     void output();
  10.     void setNumClasses(int);
  11.     int getNumClasses();
  12.     void setClassList(char);
  13.     char getClassList();
  14.     void setName(char);
  15.     char getName();
  16.     void reset();
  17.  
  18.  
  19. private:
  20.     char _name;
  21.     int _numClasses;
  22.     char _classList;
  23. };
  24. void Student::input()
  25. {
  26.     CharPtr classList;
  27.     classList = new char[];
  28.     int numberClasses;
  29.     char name;
  30.  
  31.     cout<<"Enter students name ";
  32.     cin>>name;
  33.     setName(name);
  34.  
  35.     cout<<"Enter the number of classes student is taking ";
  36.     cin>>numberClasses;
  37.     setNumClasses(numberClasses);
  38.  
  39.     for(int counter = 0; counter < numberClasses; counter++)
  40.     {
  41.         cout<<"Enter name of class ";
  42.         cin>>classList[counter];
  43.         setClassList(classList[counter]);
  44.     }    
  45.  
  46. void Student::setClassList(char classList)
  47. {
  48.     int index = 0;
  49.     _classList = classList[index];
  50. }
Now this isnt all my code, these are just the snipets Im having problems with. Oh and the classList is a char because we havent gone over strings yet.
Thanks.

Savage's Avatar
Expert
 
Join Date: Feb 2007
Posts: 1,737
#2: Apr 27 '07

re: Dynamic array problems


Quote:

Originally Posted by blackstormdragon


void Student::setClassList(char classList)
{
int index = 0;
_classList = classList[index];
}

In ur typedef u have char* CharPtr which is pointer.

later in main u have CharPtr classList so this also will be a pointer.

In ur function u are calling char classList which is simple char not pointer to char.

Think about this

Savage
blackstormdragon's Avatar
Member
 
Join Date: Feb 2007
Posts: 32
#3: Apr 29 '07

re: Dynamic array problems


Quote:

Originally Posted by Savage

In ur typedef u have char* CharPtr which is pointer.

later in main u have CharPtr classList so this also will be a pointer.

In ur function u are calling char classList which is simple char not pointer to char.

Think about this

Savage

Thanks, you hint sent me in the right direction. I think I figured it out.
Savage's Avatar
Expert
 
Join Date: Feb 2007
Posts: 1,737
#4: Apr 29 '07

re: Dynamic array problems


Quote:

Originally Posted by blackstormdragon

Thanks, you hint sent me in the right direction. I think I figured it out.

Always a pleasure......

Savage
Reply