0

So I have my header and my CPP for that class. My understanding is that although StudentID is a private variable in StudentClass.h I assign it to StudentID = FullStudentID; under my set statement so it should work. Hoewever, I get error C2065: 'firstName': undeclared identifier. Tried all solutions I found but none work.

Am I setting up these variables incorrectly? I tried to have the variables as protected rather than private but same issue.

Header

#define Student_H
#include <iostream>
#include <string>
using namespace std;

class Student {
public:
    void SetStudentID(int FullStudentID);
    void SetAge(int ageInYears);
    void SetFirstName(string firstAName);
    void SetLastName(string lastAName);
    void SetEmail(string aEmailAddress);

    int GetStudentID() const;
    int GetAge() const;
    string GetFirstName() const;
    string GetLastName() const;
    string GetEmail() const;

private:
    int studentID;
    int age;
    string firstName;
    string lastName;
    string emailAddress;


};

#endif

Source

#include "StudentClass.h"
#include <iostream>
#include <string>
using namespace std;

void Student::SetStudentID(int FullStudentID) {
    studentID = FullStudentID;
    return;
}
void Student::SetAge(int ageInYears) {
    age = ageInYears;
    return;
}
void Student::SetFirstName(string firstAName) {
    firstName = firstAName;
    return;
}
void Student::SetLastName(string lastAName) {
    lastName = lastAName;
    return;
}
void Student::SetEmail(string aEmailAddress) {
    emailAddress = aEmailAddress;
    return;
}


//start of get statements -------------------------------------------------------------------

int Student::GetStudentID() const {
    return;
}
int Student::GetAge() const {
    return;
}
string Student::GetFirstName()const {
    return;
}
string Student::GetLastName()const {
    return;
}
string Student::GetEmail()const {
    return;
}


void StudentPrint() const {
    cout << "student ID: " << studentID << endl;
    cout << "age: " << age << endl;
    cout << "First name: " << firstName << endl;
    cout << "Last name: " << lastName << endl;
    cout << "Email: " << emailAddress << endl;
}
JaMiT
  • 14,422
  • 4
  • 15
  • 31
  • 2
    How is `StudentPrint` supposed to print a student's information? Either make it a member function, or pass in a `Student` object as a parameter. – cigien May 15 '20 at 18:22
  • 2
    You have your header guard wrong. [https://stackoverflow.com/questions/4767068/header-guards-in-c-and-c](https://stackoverflow.com/questions/4767068/header-guards-in-c-and-c) The variables look fine to me. – drescherjm May 15 '20 at 18:22
  • Please indicate exactly the lines of code that give you the error. – cigien May 15 '20 at 18:25
  • Your compiler should have told you where the error occurs and that information should have helped you. I am sure that @cigien is correct about the error in the first comment. – drescherjm May 15 '20 at 18:25
  • Sorry good point about error location. (33,8), (32)(36,8)(35)(39,8)(38)(42,8)(41)(45,8)(44)(49,27) which is basically all my variables – theafterwar May 15 '20 at 18:27
  • The `StudentPrint` is not a member function of `Student`, so it can't access the private variables. Try making it a member function. – Thomas Matthews May 15 '20 at 18:27
  • I'll check out the header thank you. – theafterwar May 15 '20 at 18:30
  • Can you copy the exact text of the error message from the Output Tab of Visual Studio. And I mean Output tab and not the errors list. Your last comment was very confusing. – drescherjm May 15 '20 at 18:31
  • Somewhat relevant when you go to fix your header guard: https://stackoverflow.com/a/50356043/5910058 – Jesper Juhl May 15 '20 at 18:32

2 Answers2

2

StudentPrint is not a member function of Student and can thus not access those variables. Either make it a member function of Student, or better yet, use the 'get' functions you've defined (after you've actually implemented them).

void StudentPrint(const Student& student) {
    cout << "student ID: " << student.GetStudentID() << endl;
    cout << "age: " << student.GetAge() << endl;
    cout << "First name: " << student.GetFirstName() << endl;
    cout << "Last name: " << student.GetLastName() << endl;
    cout << "Email: " << student.GetEmailAddress() << endl;
}

As a side note; StudentPrint cannot be marked as const as it's not a member function, but a free function. Only member functions can be marked as const.

Ted Klein Bergman
  • 9,146
  • 4
  • 29
  • 50
0

Not sure how to mark a selection as the fix but

"How is StudentPrint supposed to print a student's information? Either make it a member function, or pass in a Student object as a parameter. – cigien 8 mins ago"

Called it. I forgot to include the print as a member function. Now I'm getting declaration errors but I'll try to sort that out my self and post a new question if it comes up. Thanks everyone you're awesome!

  • 1
    Please don't post an answer that does not answer the question. if the answer from @Ted solves this accept his answer. – drescherjm May 15 '20 at 18:33