-1

The problem I'm having is when i try to set some of my class variables within an array of 3 class objects, I get an error stating that the variables required by the function are not declared within the scope of the main function.

I'm still new to classes and separating my interface from my implementation and I do find it to be confusing at times.

I have been looking at some other examples on this site and others and I can't figure out exactly what my issue is and how to fix it.

interface.h file:

    #ifndef INTERFACE_H
    #define INTERFACE_H

    #include <iostream>
    #include <string>
    #include <fstream>

    using namespace std; // I know this is a programming sin to include inside a header file but my textbook wants me to do it this way

    class student
    {
    public:
        student(); // class constructor
        void setstudent(string studentName, string ID, string studentNumber, string diploma); // Function im using to set objects in array object

    private:
        string studentName;
        string ID;
        string studentNumber; // class variables
        string diploma;
        int averageMark;
        string codes [5];
        int marks [5];
    };


    #endif // INTERFACE_H

implementation.cpp file:

#include <iostream>
#include "interface.h"

using namespace std;

student::student()
{
    studentName = "";
    ID = "";
    studentNumber = ""; // constructor to initialise all class variables
    diploma = "";
    averageMark = 0;
    codes [5] = {"", "", "", "", ""};
    marks [5] = {0, 0, 0, 0, 0};
}

void student::setstudent(string studentName, string ID, string studentNumber, string diploma) // function to set class variables
{
    cout << "Please enter a student name" << endl;
    cin.getline(studentName);

    cout << "Please enter a unique student ID" << endl;
    cin.getline(ID);

    cout << "Please enter a unique student number" << endl;
    cin.getline(studentNumber);

    cout << "Please enter diploma name" << endl;
    cin.getline(diploma)
    if (diploma == "Garden Design")
    {
        codes[5] = {"G1","G2","G3","G4","G5"};
    }
    else if (diploma == "Gourmet Cooking")
    {
        codes[5] = {"C1","C2","C3","C4","C5"};
    }
}

main.cpp file:

#include <iostream>
#include "interface.h"

using namespace std;

int main()
{
    student studentDetails[3]; // Creation of class array object to store 3 objects / Maybe ive declared my array to hold 3 class objects incorrectly?
    for (int i = 0; i < 3; i++)
    {
        studentDetails[i].setstudent(studentName, ID, studentNumber, diploma); // function call to set class variables for the objects

    }

    return 0;
}

If anyone can please point out what I've done wrong and point me in the right direction would be very appreciated.

Error by CodeBlocks:

||=== Build: Debug in student (compiler: GNU GCC Compiler) ===|
C:\Users\nicbe\Desktop\Student\student\main.cpp||In function 'int main()':|
C:\Users\nicbe\Desktop\Student\student\main.cpp|12|error: 'studentName' was not declared in this scope|
C:\Users\nicbe\Desktop\Student\student\main.cpp|12|error: 'ID' was not declared in this scope|
C:\Users\nicbe\Desktop\Student\student\main.cpp|12|error: 'studentNumber' was not declared in this scope|
C:\Users\nicbe\Desktop\Student\student\main.cpp|12|error: 'diploma' was not declared in this scope|
||=== Build failed: 4 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
Bezuid
  • 47
  • 2
  • 7
  • 1
    `setstudent` shouldn't take parameters. – tkausl Sep 17 '17 at 17:24
  • 2
    Your `student` constructor will not work as you expect it to. You define a set of *local variables* and initialize them. The class member variables will not be initialized. And the member function you call, you pass arguments that are variables that doesn't exist in the scope of the `main` function (or in the global scope). There are also many other problems, like how you attempt to assign to arrays. All in all it seems you could use [a couple of good beginners books to read](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Some programmer dude Sep 17 '17 at 17:25
  • @tkausl Yes it should, at least it has been declared (and defined) to take arguments. Otherwise the error would be different. – Some programmer dude Sep 17 '17 at 17:27
  • 1
    `studentName, ID, studentNumber, diploma` The compiler sees these as variables being passed to the `setstudent` method. Where do they come from? – user4581301 Sep 17 '17 at 17:27
  • This is certainly not a [mcve]. Next time, cut out all of the unnecessary prompts, `if` statements, etc., and you could have reduced this post down to a program that would be less than 15 lines of code. Maybe when doing that, you will find what you're doing that is not correct. – PaulMcKenzie Sep 17 '17 at 17:30

1 Answers1

0

The error is exactly what it says: was not declared.

in your main function:

studentDetails[i].setstudent(studentName, ID, studentNumber, diploma);

suddenly these four arguments appear out of thin air(?). Not sure if you have just excluded that part or what.

andersfylling
  • 718
  • 10
  • 24