Problem with get/set methods (can access some methods, and not others).

Hello,

I'm following along with the Live Lessons C++ video tutorial. I did the entire chapter/all the exercises correctly yesterday, and now today, I tried to do it from memory, for some additional practice/re-enforcement. I know that the main can access the header file, but for some reason, my constructor, or any other object, cannot access any other methods other than the following when I try to access it (i.e. when I click the "." after the object): 'courseName,' 'GradeBook,' and 'setCourseName.'"



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <string>

using namespace std;

#include "GradeBook.h"

int main()
{
    string nameOfCourse;

    GradeBook GradeBookX("02 Data Structures and Algorithms in C++");

    GradeBook.getCourseName();

return 0;









}

#ifndef GRADEBOOK_H
#define GRADEBOOK_H

#include <iostream>
#include <string>

using namespace std;

class GradeBook{

private:

string courseName;

public:

GradeBook(string name){
setCourseName(name);
}

void setCourseName(string name){

if (name.length() <= 25)
    courseName = name;
if (name.length() > 25)
    {
    courseName = name.substr(0,25);
cout << "\nYour course name " << name << "exceeds the maximum alloted characters(25). File has been shortened to first 25 characters." << endl;
}
string getCourseName(){
return courseName;
}

void displayMessage(){
cout << "\nWelcome to the GradeBook for " << getCourseName() << "!" << endl;

}

};
#endif // GRADEBOOK_H




C:\Users\Leo\Documents\Test_Project\main.cpp|26|error: expected ';' after class definition|
C:\Users\Leo\Documents\Test_Project\GradeBook.h||In member function 'void GradeBook::setCourseName(std::string)':|
C:\Users\Leo\Documents\Test_Project\GradeBook.h|32|error: a function-definition is not allowed here before '{' token|
C:\Users\Leo\Documents\Test_Project\GradeBook.h|36|error: a function-definition is not allowed here before '{' token|
C:\Users\Leo\Documents\Test_Project\GradeBook.h|41|error: a function-definition is not allowed here before '{' token|
C:\Users\Leo\Documents\Test_Project\GradeBook.h|64|error: expected '}' at end of input|
C:\Users\Leo\Documents\Test_Project\main.cpp||In member function 'int GradeBook::main()':|
C:\Users\Leo\Documents\Test_Project\main.cpp|16|error: 'class GradeBook' has no member named 'getCourseName'|
||=== Build finished: 6 errors, 0 warnings (0 minutes, 0 seconds) ===|
Last edited on
Show the valid code where you specify a method and the compiler issues an error.
Show the valid code where you specify a method and the compiler issues an error.


I see what you're talking about...I'll try to re-submit the code on one page so the errors can be located later on today after I get back from work.
Change thsi constructor

GradeBook(string name){
setCourseName(name);
}

either as

GradeBook(string name) : setCourseName(name) {}

or as

GradeBook(string name){
setCourseName = name;
}


EDIT: Oh, I am sorry. wait a minute.


You missed a closing brace in the function definition

void setCourseName(string name){

if (name.length() <= 25)
courseName = name;
if (name.length() > 25)
{
courseName = name.substr(0,25);
cout << "\nYour course name " << name << "exceeds the maximum alloted characters(25). File has been shortened to first 25 characters." << endl;
}
// <== here one more closing brace must be before the next function definition
string getCourseName(){


Next time format your code.
Last edited on
It looks like you only have 3 brackets in this function, with nothing closing the function.
1
2
3
4
5
6
7
8
9
void setCourseName(string name){

if (name.length() <= 25)
    courseName = name;
if (name.length() > 25)
    {
    courseName = name.substr(0,25);
cout << "\nYour course name " << name << "exceeds the maximum alloted characters(25). File has been shortened to first 25 characters." << endl;
}
Last edited on
Thanks guys, I checked everything on my computer at work, and everything is working correctly.
Topic archived. No new replies allowed.