0

I'm new to C++. I'm trying to practice compiling a program on Visual Studio but I'm having some trouble deciphering the compiler errors. I could really use some help with figuring out how to properly debug and compile a program so I don't have too much trouble with compiling code in the future.

I have three files: Gradebook.h,, Gradebook.cpp and Source1.cpp. Gradebook.h is in header files, and the other two are in source files in solution explorer.

EDIT: I had a bunch of syntax errors and other unnecessary comments that had been making it difficult for me to read my own code. (thanks ray, and everyone else). below is my revised code. i also found out how to properly use the code sample tool, so now everything should be indented properly.

    #include <string>
    using namespace std;

    class GradeBook
    {
    public:
        //constants
        static const int students = 10; //number of tests
        static const int tests = 3;  //number of tests

        //constructor initializes course name and array of grades
        string Gradebook(string, const int[][tests]);

        void setCourseName(string); //function to set course name
        string getCourseName(); //function to retrieve the course name
        void displayMessage(); //display a welcome message
        void processGrades(); //perform various operations on the grade data
        int getMinimum(); //find the minimum grade in the grade book
        int getMaximum(); //find the maximum grade in the grade book
        double getAverage(const int[], const int); // get student's average
        void outputBarChart(); //output bar chart of grade dist
        void outputGrades(); //output the contents of the grades array

    private:
        string courseName; //course name for this gradebook
        int grades[students][tests]; //two-dimensional array of grades
    }; //end class GradeBook

I don't get any errors in Gradebook.h, but the main problem lies in my other source files:

#include <iostream>
#include <iomanip>
using namespace std;

//include definition of class GradeBook from GradeBook.h
#include "GradeBook.h"

// two-argument constructor initializes courseName and grades array
GradeBook:: GradeBook(string name, const int gradesArray[][GradeBook::tests])
{
    setCourseName(name); //initialize coursename

    //copy grades from gradeArray to grades
    for (int student = 0; student < students; ++student)

        for (int tests = 0; tests < tests; ++tests)
            grades[student][tests] = gradesArray[student][tests];
} //end two argument GradeBook constructor

//function to set the course name
void GradeBook::setCourseName(string name)
{
    courseName = name;
}

GradeBook::Gradebook(string, const int[][tests])
{
}

void GradeBook::setCourseName(string)
{
}

//function to retrieve the course name 
string GradeBook::getCourseName()
{
    return courseName;
} 

//display a welcome message to GradeBook user
void GradeBook::displayMessage()
{
    //statements calls getCourseName to get the name of the course the gradebook represents
    cout << "Welcome to the grade book for \n" << getCourseName() << "!"
        << endl;
} 

//perform various operations on the data
void GradeBook::processGrades()
{
    outputGrades(); //output grades array

    // call functions getMinimum and getMaximum
    cout << "\nLowest grade in the grade book is " << getMinimum()
        << "\nHighest grade in the grade book is " << getMaximum() << endl;

    outputBarChart(); //display distribution chart of grades on all tests
} 

//find minimum grade in the entire Gradebook
int GradeBook::getMinimum()
{
    int lowGrade = 100; //assume lowest grade is 100;
    //loop through rows of grades array
    for (int student = 0; student < students; ++student)
    {
        //loop to columns of current row
        for (int tests = 0; tests < tests; ++tests)
        {
            //if current grade less than lowGrade, assign it to lowGrade
            if (grades[student][tests] < lowGrade)
                lowGrade = grades[student][tests]; //new lowest grade
        }
    } 
    return lowGrade; 
} 

//find maximum grade in the entire gradebook
int GradeBook::getMaximum()
{
    int highGrade = 0; //assume highest grade is 0

    for (int student = 0; student < students; ++student)
    {
        //loop to columns of current row
        for (int tests = 0; tests < tests; ++tests)
        {
            //if current grade less than highGrade, assign it to highGrade
            if (grades[student][tests] > highGrade)
                highGrade = grades[student][tests]; //new highest grade
        }
    }  
    return highGrade; 

} 

//determine average grade for particular set of grades
double GradeBook::getAverage(const int setOfGrades[], const int grades)
{
    int total = 0; //initialize total

    //sum grades in array
    for (int grade = 0; grade < grades; ++grade)
        total += setOfGrades[grade];

    //return average of grades
    return static_cast <double>(total) / grades;
} 

//output bar chart displaying grade distribution
void GradeBook::outputBarChart()
{
    cout << "\nOverall grade distribution: " << endl;

    //stores frequency of grades in each range of 10 grades
    const int frequencySize = 11;
    int frequency[frequencySize] = {}; //initalize elements to 0;

    //for each grade, increment the appropriate frequency
    for (int student = 0; student < students; ++student)
        for (int tests = 0; tests < tests; ++tests)
            ++frequency[grades[student][tests] / 10];

    //for each grade frequency, print bar in chart
    for (int count = 0; count < frequencySize; ++count)
    {
        //output bar label (0-9, 90-99, 100)
        if (count == 0)
            cout << "0-9: ";
        else if (count == 10)
            cout << "100: ";
        else
            cout << count * 10 << "-" << (count * 10) + 9 << ": ";

        //print bar of asterisks
        for (int stars = 0; stars < frequency[count]; stars)
            cout << '*';

        cout << endl;
    }
}

//output the contents of the grades array
void GradeBook::outputGrades()
{
    cout << "\nThe grades are: \n\n";
    cout << "             "; //align column heads

    //create a column heading for each of the tests
    for (int tests = 0; tests < tests; ++tests)
        cout << "Test" << tests + 1 << " ";

    cout << "Average" << endl; //student average column heading

    //create rows/columns of text representing array grades
    for (int student = 0; student < students; ++student)
    {
        cout << "Student " << setw(2) << student + 1;

        //output student's grades
        for (int tests = 0; tests < tests; ++tests)
            cout << setw(8) << grades[student][tests];

        //call member function getAverage to calculate student's average
        //pass row of grades and the value of tests as the arguments
        double average = getAverage(grades[student], tests);
        cout << setw(9) << setprecision(2) << fixed << average << endl;
    }
}

The main errors that I'm receiving here are the following:

GradeBook:: GradeBook(string name, const int gradesArray[][GradeBook::tests])

ERROR: no instance of overloaded function "GradeBook::GradeBook" matches the specified type.

GradeBook::Gradebook(string, const int[][tests])
{
}

ERROR: Explicit type is missing ('int' assumed) & ERROR: declaration is incompatible with "std::string Gradebook (declared at line 12).

I'm really confused and frustrated. Any insight that you could provide would be extremely helpful. This is an example from my textbook, almost verbatim and I've been trying for the past two hours trying to figure out what's been missing.

The last source file I have is below:

#include "Gradebook.h"
#include "Source1.h"

//function main begins program execution
int main()
{
    //two-dimensional array of student grades
    int gradesArray[ GradeBook::students][GradeBook::tests] =
    {
        {87, 96, 70},
        {68, 87, 90},
        {94, 100, 90},
        {100, 81, 82},
        {83, 65, 85},
        {78, 87, 65},
        {85, 75, 83},
        {91, 94, 100},
        {76, 72, 84},
        {87, 93, 73}
    }

GradeBook myGradebook("CS101 Introduction to C++ Programming", gradesArray);
myGradeBook.displayMessage();
myGradeBook.processGrades();
} //end main

I'm receiving errors on the last couple of lines: Gradebook myGradebook("xxxx") receives ERROR: expected a ";" .. but don't I already have one?

and myGradebook.displayMessage(); ERROR: identifier "myGradeBook" is undefined.

Please advise and point me in the right direction. I desperately need help.

8
  • since, you are accessing tests in .cpp, you must use Gradebook::tests instead of tests. Commented Oct 28, 2015 at 5:58
  • hi raul, when i do that i receive the following error: must be class or namespace name. i'm genuinely confused. Commented Oct 28, 2015 at 6:00
  • 2
    In your .h, you made a typo....you have to change string Gradebook(....) to string GradeBook(...) Commented Oct 28, 2015 at 6:02
  • @Clark Get an IDE that supports error highlighting and proper indentation. Commented Oct 28, 2015 at 6:04
  • 1
    @Raul: even more GradeBook(...) with no return type - they're not legal on a constructor. Commented Oct 28, 2015 at 6:04

4 Answers 4

1

You must really improve this code.

Error

//function to set the course name
void GradeBook::setCourseName(string name
{
courseName = name;
})//end function setCourseName

Correct

//function to set the course name
void GradeBook::setCourseName(string name)
{
courseName = name;
}//end function setCourseName

====

Error

//find maximum grade in the entire gradebook
int GradeBook::getMaximum)

Correct

//find maximum grade in the entire gradebook
int GradeBook::getMaximum()
Sign up to request clarification or add additional context in comments.

2 Comments

thank you! jeez i spent all this time looking at my code and i didn't even realize how many small mistakes i made. i appreciate you taking the time to point these out.
@Clark, you welcome, though it wouldn't be bad to get an upvote :)
0

Some issues I noticed in the code you've posted:

Typo on Parenthesis

//function to set the course name
void GradeBook::setCourseName(string name
{
courseName = name;
})//end function setCourseName

There's a typo at the end with the parenthesis, which is probably more difficult for you to notice due to unnecessary // end function comment. It should be:

void GradeBook::setCourseName(string name)
{
    courseName = name;
}

Undefined Variable

error saying ERROR: identifier "tests" is undefined.

for (int student = 0; student < students; ++student)
    for (int test = 0; tests < tests; ++test)
        grades[student][test] = gradesArray[student][test];
}

It should be: for (int test = 0; /* not tests */ test < GradeBook::tests; ++test)

Definition vs Declaration

but isn't setCourseName already defined in "Gradebook.h"??

The method is not defined in the header file, it is declared. The difference is that a definition contains a body of code, whereas a declaration does not. In other words, method_name(); is a declaration and method_name() { /* empty body */ } is a definition.

Invalid Class Name - Case Sensitive

You also have some other syntax errors due to typos. The class name is GradeBook, not Gradebook as you've coded below.

//perform various operations on the data
void Gradebook::processGrades()
{
    outputGrades(); //output grades array
    // ...
}

Invalid Ctor Declaration

ERROR: Explicit type is missing ('int' assumed) & ERROR: declaration is incompatible with "std::string Gradebook (declared at line 12).

The reason for this error is the way in which you've declared the ctor in your header file, GradeBook.h:

string Gradebook(string, const int[][tests]);

That constructor declaration in the header is wrong on 2 counts:

  1. The name should match the name of the class, i.e. GradeBook (capital B), and
  2. Constructors do not have a return type of string or any other primitive. It should be GradeBook(string, const int[][tests]); because it returns an instance of itself as a type.

In short, it should look as follows:

Gradebook(string, const int[][tests]);  // does not return std::string

Duplicate Ctor Definition

You've defined the same ctor twice, the second one being several lines under the first, shown below:

GradeBook::Gradebook(string, const int[][tests])
{
}

You should remove one or the other.

Typo on Parenthesis - Sequel

This line of code

int GradeBook::getMaximum)

should be

int GradeBook::getMaximum()
//                       ^ note this guy

Additional Tips

Tip 1: Sometimes one syntax error will cascade and cause the compiler to list a bunch of other non-errors. As soon as you fix one, several others might also go away, so try to re-build often as you go.

Tip 2: Try to use non-static constants while you get your stuff to work. Declaring and initializing statics can be slightly more complicated than the alternative.

Tip 3: There's probably no reason to have the static members as public. Don't publish anything that's not really needed, and if you need to, consider using methods instead.

Tip 4: Consider reducing the amount of unnecessary comment noise. You don't really need to add a comment every time you close a brace to indicate that you're ending a section. It's just as obvious writing a = a + 1; and adding a comment that says // add 1 to variable 'a'. Only document non-obvious things.

Tip 5: Avoid trying to code everything up before trying to build the first time. Instead, press the build button as soon as you have a portion of code that should work, and move on after it actually does.

Tip 6: Consider using a Source Control Management tool, like Git. You will wonder how you ever tried to work on code without it.

7 Comments

thanks Ray for providing such a thorough response. I revised my code and all of the small syntax errors have mainly disappeared except for a few: for my Gradebook::GradeBook(string name, ...) I am receiving a new error: + 3 overloads. ERROR: no instance of overloaded function "GradeBook::GradeBook" matches the specified type.
@Clark: You're welcome. Upvotes are the best way to say thank you around here ;) I've updated the post re a duplicate ctor you have.
thanks again ray! I removed the invalid ctor declaration but I am still receiving the same error.. I'm taking all of these suggestions to heart and will download Git now!
@Clark: I've updated the post. It's your declaration in the header file.
NEVERMIND! thank you for all of your help! i think it's working properly now!
|
0

The problem is with your .h file. You have made a typo with your constructor name. you gotta change the

Gradebook(string, const int[][tests]); 

to

string GradeBook(string, const int[][tests]);

Also, since you are accessing the tests variable outside the class, the compiler doesn't understand the tests you are referring to. So, you have to use

GradeBook::tests

1 Comment

ah - sorry. somehow when i posted that was omitted from the code. it's been added, thank you.
0
First error:
void GradeBook::setCourseName(string name
{
courseName = name;
})//end function setCourseName


Error2:
int GradeBook::getMaximum)

1 Comment

While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.