error: read-only variable is not assignable


first part is the header
and the other is the class

the class i'm receiving a error message

GradeBook.cpp:48:19: error: read-only variable is not assignable
numberCredits = x;


see underline

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#pragma once
 
#include <iostream> 
// GradeBook class definition
#include <string> // class GradeBook uses C++ standard string class
 
// GradeBook class definition
class GradeBook
{
public:
   explicit GradeBook( std::string, std::string,int); // constructor initialize courseName
   void setCourseName( std::string ); // sets the course name
   std::string getCourseName() const; // gets the course name
   void setInstructorName( std::string ); // sets the instructor name
   std::string getInstructorName() const; // gets the instructor name
   void displayMessage() const; // displays a welcome message
   void setNumberCredits(int x) const;//sets the number of credit
   int getNumberCredits() const;//gets the number of credits
   
private:
   std::string courseName; // course name for this GradeBook
   std::string instructorName; // instructor name
   int numberCredits; //number of credits
}; // end class GradeBook  



#include <iostream>
#include "GradeBook.h" // include definition of class GradeBook
using namespace std;
 
// constructor initializes courseName with string supplied as argument
GradeBook::GradeBook( string cName, string iName,int credits )
{  
setCourseName(cName);
setInstructorName(iName);
setNumberCredits(credits);
   // empty body
} // end GradeBook constructor
 
// function to set the course name
void GradeBook::setCourseName( string name )
{
   if ( name.size() <= 25 ) // if name has 25 or fewer characters
      courseName = name; // store the course name in the object
 
   if ( name.size() > 25 ) // if name has more than 25 characters
   { 
      // set courseName to first 25 characters of parameter name
      courseName = name.substr( 0, 25 ); // start at 0, length of 25
 
      cerr << "Name \"" << name << "\" exceeds maximum length (25).\n"
         << "Limiting courseName to first 25 characters.\n" << endl;
   } // end if
} // end function setCourseName
 
// function to get the course name
string GradeBook::getCourseName() const
{
   return courseName; // return object's courseName
} // end function getCourseName
 
void GradeBook::setInstructorName( std::string name) // sets the instructor name
{
instructorName = name;
}
 
std::string GradeBook::getInstructorName() const // gets the instructor name
{
return instructorName;
}
 
void GradeBook::setNumberCredits(int x) const // sets the number of credits
{
    numberCredits = x;
}
int GradeBook::getNumberCredits() const
{
    return numberCredits;
}
// display a welcome message to the GradeBook user
void GradeBook::displayMessage() const
{
   // call getCourseName to get the courseName
   cout << "Welcome to the grade book for\n" << getCourseName() 
      << "!" << endl;
   cout << "Instructor is " << getInstructorName() << endl;
   cout << "The Number of credit is " << getNumberCredits() << endl;
} // end function displayMessage
 

your method should not be declared as const if your going to change somenthing
ahhhh i'm a dumbass

thanks that fixed the problem
Topic archived. No new replies allowed.