0

I have three files, Person.h, Person.cpp, and my main.cpp. In Person.h I have all my classes and the .cpp part contains the actual implementation. My issue is that I'm getting two error messages when I try to read through an array created by one of my functions and pointed at by private data member *subjects.

The error messages reads:

Severity Code Description Project File Line Error C3867 'Student::myarray1': non-standard syntax; use '&' to create a pointer to member lab_10 c:\users\shireen\documents\visual studio 2015\projects\lab_10\lab_10\main.cpp 67

Severity Code Description Project File Line Error C2109 subscript requires array or pointer type lab_10 c:\users\shireen\documents\visual studio 2015\projects\lab_10\lab_10\main.cpp 67

and my actual code.

main.cpp

#include"Person.h"
#include<string>
#include<iostream>

using namespace std;

int main()
{
    Student *pointer1;
    Staff *pointer2;

    string name, gender;
    int age, studentno, subs;
    pointer1 = new Student[4];
    pointer2 = new Staff[2];

    for (int i = 1; i < 5; i++)
    {
        cout << "please enter the name of student " << i << endl;
        cin >> name;
        cout << "please enter the age of student " << i << endl;
        cin >> age;
        cout << "please enter the gender of student " << i << endl;
        cin >> gender;
        cout << "please enter student " << i << "'s student number" << endl;
        cin >> studentno;
        cout << "please enter the number of subjects taken by student " << i << endl;
        cin >> subs;
        pointer1[i].set_name(name);
        pointer1[i].set_age(age);
        pointer1[i].set_gender(gender);
        pointer1[i].set_StudentNumber(studentno);
        pointer1[i].set_NumberOfSubjects(subs);
        pointer1[i].myarray1(subs);

    }

    for (int i = 1; i < 3; i++)
    {
        cout << "please enter the name of Staff member " << i << endl;
        cin >> name;
        cout << "please enter the age of staff member " << i << endl;
        cin >> age;
        cout << "please enter the gender of staff member " << i << endl;
        cin >> gender;
        cout << "please enter the number of subjects taught by staff member " << i << endl;
        cin >> subs;
        pointer2[i].set_name(name);
        pointer2[i].set_age(age);
        pointer2[i].set_gender(gender);
        pointer2[i].set_NumberOfSubjects(subs);
        pointer2[i].myarray2(subs);

    }


    for (int i = 1; i < 5; i++)
    {
        pointer1[i].get_name();
        pointer1[i].get_StudentNumber();
        cout << "Student name: " << name << endl;
        cout << "Student age: " << age << endl;
        for (int j = 0; j < 4; j++)
        {
            string *word;

            pointer1[i].myarray1[j] = word;
            cout << word << endl;
        }

    }
    system("PAUSE");
    return 0;

Person.h

#ifndef _PERSON_H_
#define _PERSON_H_
#include <string>
#include <iostream> 

using namespace std;

class Person
{
private:
    string name;
    int age;
    string gender;
public:
    Person();
    ~Person();
    void set_name(string n);
    void set_age(int a);
    void set_gender(string g);
    string get_name();
    int get_age();
    string get_gender();

};

class Staff : public Person
{
private:
    int NumberOfSubjects;
    string *Subjects;
public:
    Staff();
    ~Staff();
    void set_NumberOfSubjects(int ns);
    int get_NumberOfSubjects();
    string myarray2(int ns);

};

class Student : public Person
{
private:
    int StudentNumber;
    int NumberOfSubjects;
    string *Subjects;
public:
    Student();
    ~Student();
    bool set_StudentNumber(int sn);
    int get_StudentNumber();
    void set_NumberOfSubjects(int ns);
    int get_NumberOfSubjects();
    string myarray1(int ns);
};

#endif _PERSON_H

Person.cpp

#include "Person.h"


Person::Person() {}

void Person::set_name(string n)
{
    name = n;
}
void Person::set_age(int a)
{
    age = a;
}
void Person::set_gender(string g)
{
    gender = g;
}
string Person::get_name() { return name; }
int Person::get_age() { return age; }
string Person::get_gender() { return gender; }
Person::~Person() {}


//staff
Staff::Staff() {}
void Staff::set_NumberOfSubjects(int ns)
{
    NumberOfSubjects = ns;
}
int Staff::get_NumberOfSubjects() { return NumberOfSubjects; }
string Staff::myarray2(int NumberOfSubjects)
{
    Subjects = new string[NumberOfSubjects];
    for (int i = 0; i < NumberOfSubjects; i++)
    {
        cout << "please enter the number of subjects taught" << endl;
        cin >> Subjects[i];
        return Subjects[i];
    }
}
Staff::~Staff()
{
    delete[] Subjects;
}

///student

Student::Student() {}
bool Student::set_StudentNumber(int sn)
{
    StudentNumber = sn;
    if (sn<0 || 100>sn)
    {

        cout << "This is not a valid student number" << endl;
        system("PAUSE");
        return false;
    }

}
int Student::get_StudentNumber() { return StudentNumber; }

void Student::set_NumberOfSubjects(int ns)
{
    cout << "Please enter the number of subjects: " << endl;
    cin >> ns;
    NumberOfSubjects = ns;
}
int Student::get_NumberOfSubjects() { return NumberOfSubjects; }

string Student::myarray1(int NumberOfSubjects)
{

    string *sub_Ptr;
    Subjects = new string[NumberOfSubjects];
    for (int i = 0; i < NumberOfSubjects; i++)
    {

        cout << "Please enter the name of the subject: " << endl;
        cin >> Subjects[i];
        sub_Ptr = &Subjects[i];
        return *sub_Ptr;


    }

}
Student::~Student()
{
    delete[] Subjects;
}
JAL
  • 41,701
  • 23
  • 172
  • 300
J0rdy
  • 43
  • 7

4 Answers4

0

Line 67 - Main.cpp

pointer1[i].myarray1[j] = word;

Not quite sure what you try to do here but since pointer1[i] returns a Student the error is accessing myarray1[j]. Neither Person nor Student have a public myarray1 array so this is not allowed.

There is however a function: string myarray1(int ns); in Student did you intend to call that? If you instead meant to access the pointer string *Subjects then you would have to provide access to that either through a function or by making it public (not recommended).


If I understand correctly you are trying:

// For each student
for (int i = 1; i < 5; i++) {
    // Print some information about the student
    cout << "Student name:   " << pointer1[i].get_name() << endl;
    cout << "Student age:    " << pointer1[i].get_age() << endl;
    cout << "Student number: " << pointer1[i].get_StudentNumber() << endl;

    // Obtain all the subjects the student has
    string* subjects = pointer1[i].AllSubjects();

    for (int j = 0; j < pointer1[i].get_NumberOfSubjects(); j++) {
        // Here you want to print the name of the subjects the student is taking (?)
        cout << "Course #" << (j + 1) << ": " << subjects[j] << std::endl;
    }

}

If you use this in your main you will have to add a function in your Student class:

string* Student::AllSubjects() { return Subjects; } 

The output will look like:

Student name:   <name>
Student age:    <age>
Student number: <number>
Course #1: <course1>
Course #2: <course2>

On a side note:

  • You have memory leaks (you never delete the Student/Staff arrays)
  • You violate the rule of 3/5 depending on your compiler
  • Try to avoid using namespace std; in headers
  • Your myarray1 function returns before it filled all the records
  • You can do all of this without pointers
  • If you have to use pointers then use std::shared_ptr<T>, std::unique_ptr<T> and friends
  • Read about std::vector<T> it is a lot easier then using pointer arrays (and more C++ like).
  • You can pass arguments to a constructor that will set class members if you want.
Floris Velleman
  • 4,848
  • 4
  • 29
  • 46
  • I'm trying to print the subjects that student pointer1[i].myarray1[j] contains after the user has I put them – J0rdy Feb 03 '16 at 12:38
  • @J0rdy Updated my answer. Let me know if there is something you don't understand. Example to see where you should put it http://coliru.stacked-crooked.com/a/47ac48535f929614 – Floris Velleman Feb 03 '16 at 12:49
  • Tried this mate. It says no operand matches your argument type at <<*subjects< – J0rdy Feb 03 '16 at 13:19
  • @J0rdy oops, change `cout << "Course #" << (j + 1) << ": " << *subjects[j] << std::endl;` to `cout << "Course #" << (j + 1) << ": " << subjects[j] << std::endl;` remove the asterisk :) – Floris Velleman Feb 03 '16 at 13:22
  • @J0rdy Alright.. is it working though? With the asterisk gone? – Floris Velleman Feb 03 '16 at 13:27
  • Indeed it is man!!! Well it actually builds now. You seem to know what you're talking about and have been really helpful man, do you have any recommended reading? As frustrating as this class is I actually enjoy it and would quite like to get better at it rather than just passing the class. – J0rdy Feb 03 '16 at 13:31
  • @J0rdy Great to hear that ! There is a list on SO containing some good books about C++ http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list. I personally like Bjarne's book for introductory level books: http://www.amazon.com/dp/0321992784/?tag=stackoverfl08-20. Scott Meyers has some great reads for experienced C++ programmers. – Floris Velleman Feb 03 '16 at 13:40
0

I guess problem is in this line

 pointer1[i].myarray1[j] = word;

myarray1 is a function, you need to pass paramter to it, probably you meant

 pointer1[i].myarray1(j) = *word;

Also note, that the word is a pointer to string and you are trying to assign it to just the string.

0

These are the changes I recommend to make your code work (altho there's more issues)

Student header (this function now returns a string array with [ns] elements in it

string * myarray1(int ns);

studen cpp

this function will only allocate the subjects array once, and it will ask the user to enter the subject [x] times.

string * Student::myarray1(int NumberOfSubjects)
{
    Subjects = new string[NumberOfSubjects];

    for (int i = 0; i < NumberOfSubjects; i++)
    {
        cout << "Please enter the name of the subject: " << endl;
        cin >> Subjects[i];         
    }
    return Subjects;
}

Once we got all the subjects, now in main we loop trought them (all 4) and print them.

Hopefully this is what you are trying to do...

main

for (int i = 1; i < 5; i++)
{
    pointer1[i].get_name();
    pointer1[i].get_StudentNumber();

    cout << "Student name: " << name << endl;
    cout << "Student age: " << age << endl;

    string *word = pointer1[i].myarray1(4);

    for (int j = 0; j < 4; j++)
        cout << word[j] << endl;
}
Jts
  • 3,447
  • 1
  • 11
  • 14
  • If he were to try this he would probably want to print the value of word, which would make `std::cout << pointer1[i].myarray1(5) << std::endl;` a lot easier then using a temporary. – Floris Velleman Feb 03 '16 at 12:33
  • He had string *word as a pointer, so I kept it like that.. His design, not mine. – Jts Feb 03 '16 at 12:38
  • why would it do that? – J0rdy Feb 03 '16 at 13:21
  • @J0rdy it doesn't work because you are using myarray[j] and you can't do that because it's a function, not an array. – Jts Feb 03 '16 at 13:22
0

small issues:

  • warning for the #endif pragma. #endif does not take any parameters, write this instead #endif //_PERSON_H_
  • include stdlib for system, i.e. #include <stdlib.h>
shisui
  • 195
  • 1
  • 7