Hey guys, just learning about composition of classes and ran into this error.
Gradebook.h
#ifndef GRADEBOOK_h
#define GRADEBOOK_h
#include "StudentRec.h"
#include <iostream>
#include <string>
class GradeBook
{
public:
GradeBook();
GradeBook(string initLastName, int studGrades);
void AddStudent(string initLastName, int studGrades);
void ShowStudents();
void UserInterface();
private:
static const int numStudents=20;
StudentRec student[numStudents];
static int studentCounter;
static int gradeCounter;
};
#endif
Gradebook.cpp
#include "Gradebook.h"
#include "StudentRec.h"
#include <iostream>
#include <string>
using namespace std;
GradeBook::GradeBook()
{}
GradeBook::GradeBook(string initLastName, int studGrades)
{}
void GradeBook::AddStudent(string initLastName, int studGrades)
{
gradeCounter++; //Increments variable responsible for tracking # of grades per student
StudentRec newStudent(initLastName, studGrades); //creates new student object
student[studentCounter]=newStudent; //Assigns new student object to array
studentCounter++; //Increments variable responsible for tracking # of students
}
void GradeBook::ShowStudents()
{
for(int i=0;i<studentCounter; i++){ //Displays information for each student instance
cout<<student[i].GetLastName()<<' ';
for(int j=0; j<gradeCounter; j++)
cout<<student[i].GetGrades(j)<<' ';
cout<<endl;
}
}
void GradeBook::UserInterface()
{
char choice=' ';
string studLastName;
int studGrade;
cout<<"Welcome to GradeBook, this program stores students"
<<" grades by last name. To ADD a student press the 'A'"
<<" key. To LIST all students, press the 'L' key. To "
<<" QUIT, press the 'Q' key."<<endl<<endl;
cin>>choice;
choice=toupper(choice);
while(choice!='Q')
{
if(choice='A'){
cout<<"To add a student, please enter their last name"
<<" followed by a space and a non-negative grade"
<<" Ex. McClure 96";
cin>>studLastName>>studGrade;
AddStudent(studLastName, studGrade);
}
else if(choice='L'){
cout<<"This is a list of all students in GradeBook"
<<endl<<endl;
ShowStudents(); //Displays all StudentRec objects
}
else if(choice!='Q')
cout<<"Please enter another letter"<<endl;
cout<<"To ADD a student press the 'A' key. To LIST all students, press the 'L' key. To "
<<" QUIT, press the 'Q' key."<<endl<<endl;
}
}
Main.cpp
#include <iostream>
#include <string>
#include "StudentRec.h"
#include "Gradebook.h"
using namespace std;
int main()
{
GradeBook gradeBook;
UserInterface();
return 0;
}
StudentRec.cpp
#include <iostream>
#include <string>
#include "StudentRec.h"
using namespace std;
StudentRec::StudentRec()
{
lastName=" ";
for(int i=0;i<numGrades; i++)
grades[i]=0;
}
StudentRec::StudentRec(string initLastName, int studGrade)
{
static int gradeCounter=0;
lastName=initLastName;
grades[gradeCounter]=studGrade;
}
string StudentRec::GetLastName()
{
return lastName;
}
int StudentRec::GetGrades(int gradeNum)
{
return grades[gradeNum];
}
void StudentRec::AddGrades(int studGrade)
{
gradeCounter++;
if(gradeCounter<=numGrades)
grades[gradeCounter]=studGrade;
else
cout<<"Too many grades for this student";
}
StudentRec.h
#ifndef STUDENTREC_h
#define STUDENTREC_h
#include <iostream>
#include <string>
using namespace std;
class StudentRec
{
public:
StudentRec();
StudentRec(string initLastName, int studGrade);
string GetLastName();
int GetGrades(int gradeNum);
void AddGrades(int studGrade);
private:
static const int numGrades=10;
static int gradeCounter;
string lastName;
int grades[numGrades];
};
#endif
In the Main.cpp file, I get an error I can't find the solution for. It reads
error: "UserInterface" was not declared in this scope. I got this error while compiling in XCode
I got error C3861: 'UserInterface': identifier not found
Obviously i've tried it in two IDEs, I also have the StudentRec.cpp and .h, but not sure you need them. Thanks in advance for the help
It appears that UserInterface() is actually a member function of GradeBook, correct?
If so, you need to add a declaration for the member function in the GradeBook class declaration:
class GradeBook
{
public:
GradeBook();
GradeBook(string initLastName, int studGrades);
void AddStudent(string initLastName, int studGrades);
void ShowStudents();
void UserInterface(); // Added
// ...
private:
// ...
};
This way, the compiler will "know" that the UserInterface() function exists as a member function. You then provided the definition in void GradeBook::UserInterface() in your .cpp file.
Then you need to call it on a GradeBook instance, like the gradeBook variable in your main() function:
int main()
{
GradeBook gradeBook;
// This calls the member function UserInterface() on the gradeBook variable.
gradeBook.UserInterface();
// This calls the global UserInterface(), which doesn't exist.
// UserInterface();
return 0;
}
UserInterface() is a method of GradeBook. The call probably needs to be:
gradeBook.UserInterface();
Related
I have been racking my brain on how to properly define the function CountProbation() properly in the Course.css file. I know that a for and if loop should probably be included but I am having trouble including functions from other files, even though I am including a header tag at the beginning of the current Course.css file.
Below are the C++ files that are given in the lab:
NOTE: Only the Course.cpp file is the one that needs to be edited. All of the other files are READ ONLY and purely for your information as the reader.
Sorry if it seems like a lot of code, but I didn't just want to include the Course.cpp file because then you might not understand the program.
Here are the compiler Errors/Warnings:
Course.cpp: In member function ‘int Course::CountProbation()’:
Course.cpp:8:18: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::vector<Student>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
8 | for (int i=0; i < roster.size(); ++i) {
| ~~^~~~~~~~~~~~~~~
Course.cpp:9:9: error: ‘GetGPA’ was not declared in this scope
9 | if (GetGPA() < 2.0) {
| ^~~~~~
Course.cpp
#include <iostream>
#include "Course.h"
#include "Student.h"
using namespace std;
int Course::CountProbation() { //This is the function that I have tried to define as you can see by my
int probCount; //code
for (int i=1; i < roster.size(); ++i) {
if (GetGPA() < 2.0) {
probCount = probCount + 1;
}
}
return probCount;
}
void Course::AddStudent(Student s) {
roster.push_back(s);
}
Course.h (READ ONLY)
#ifndef COURSE_H
#define COURSE_H
#include <vector>
#include "Student.h"
class Course {
public:
int CountProbation();
void AddStudent(Student s);
private:
vector<Student> roster; //collection of Student objects
};
#endif
Main.cpp (READ ONLY)
#include <iostream>
#include <string>
#include "Course.h"
using namespace std;
int main() {
Course course;
int probCount;
// Example students for testing
course.AddStudent(Student("Henry", "Cabot", 3.2));
course.AddStudent(Student("Brenda", "Stern", 1.1));
course.AddStudent(Student("Lynda", "Robison", 2.4));
course.AddStudent(Student("Jane", "Flynn", 1.8));
probCount = course.CountProbation();
cout << "Probation count: " << probCount << endl;
return 0;
}
Student.h (READ ONLY)
#ifndef STUDENT_H
#define STUDENT_H
#include <string>
using namespace std;
// Class representing a student
class Student {
public:
Student(string first, string last, double gpa);
double GetGPA() ;
string GetLast();
private:
string first; // first name
string last; // last name
double gpa; // grade point average
};
#endif
Student.cpp (READ ONLY)
#include "Student.h"
// Student class constructor
Student::Student(string first, string last, double gpa) {
this->first = first; // first name
this->last = last; // last name
this->gpa = gpa; // grade point average
}
double Student::GetGPA() {
return gpa;
}
string Student::GetLast() {
return last;
}
This loop (as it was coded in the original question, before it was edited):
for (int i=1; i < roster.size(); ++i) {
if (Student.GetGPA() < 2.0) {
probCount = probCount + 1;
}
}
is flawed for three reasons:
i should start from 0, rather than 1
i should be of type size_t, rather than int
Student is a type, not a variable
A nice way to solve all these problems is to use a ranged for loop, like this:
for (Student &student : roster)
{
if (student.GetGPA() < 2.0)
probCount = probCount + 1;
}
As mentioned in the comments, you also need to initialise probCount to zero before entering the loop:
int probCount = 0;
Finally, it's worth noting that the above loop would normally be coded as:
for (const Student &student : roster)
{
...
but that won't work here as GetGPA is not declared as a const method. Thanks to #user4581301 for pointing this out.
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 2 years ago.
I am working with my own custom header file for the first time in C++. The goal of this program is to create a dice class that can be used to create an object oriented dice game.
When I go to run my program (made of three files (header/class specification file, class implementation file, and finally the application file), I am getting an error:
undefined reference to `Die::Die(int)'
I have about six of these errors when running app.cpp, one for every time I try to access information from my Die class.
Full Error Message
My Three Files
Die.h
#ifndef DIE_H
#define DIE_H
#include <ctime>
//#include
class Die
{
private:
int numberOfSides=0;
int value=0;
public:
Die(int=6);
void setSide(int);
void roll();
int getSides();
int getValue();
};
#endif
Die.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "Die.h"
using namespace std;
Die::Die(int numSides)
{
// Get the system time.
unsigned seed = time(0);
// Seed the random number generator.
srand(seed);
// Set the number of sides.
numberOfSides = numSides;
// Perform an initial roll.
roll();
}
void Die::setSide(int side=6){
if (side > 0){
numberOfSides = side;
}
else{
cout << "Invalid amount of sides\n";
exit(EXIT_FAILURE);
}
}
void Die::roll(){
const int MIN_VALUE = 1; // Minimum die value
value = (rand() % (numberOfSides - MIN_VALUE + 1)) + MIN_VALUE;
}
int Die::getSides()
{
return numberOfSides;
}
int Die::getValue()
{
return value;
}
app.cpp
#include <iostream>
#include "Die.h"
using namespace std;
bool getChoice();
void playGame(Die,Die,int &, int &);
int main()
{
int playerTotal=0;
int compTotal=0;
Die player(6);
Die computer(6);
while(getChoice()){
playGame(player, computer, playerTotal, compTotal);
getChoice();
}
}
void playGame(Die play, Die comp, int &pTotal, int &cTotal){
//add points
play.roll();
pTotal += play.getValue();
comp.roll();
cTotal += comp.getValue();
//show points each round
cout << "You have " << pTotal << " points;\n";
}
bool getChoice(){
bool choice;
cout << "Would you like to roll the dice? (Y/N): ";
cin>> choice;
return choice;
}
You should compile both app.cpp and Die.cpp at the same time:
g++ app.cpp Die.cpp
I am enrolled in an Object Oriented programming class and am using code::blocks as my IDE. We have a project that requires to pass an array of objects ( a Libray book array with private names and titles) into a sort array that will sort the titles or authors alphabetically. I have all the components working as they should be except the sort function. I used a sort function that I have used before in other programs but am having trouble getting it to work with this program. I am still in the beginning stages of learning how to code and would appreciate any feedback.
Header file:
#include <iostream>
#include <cstring>
using namespace std;
class Book
{
private:
string title;
string author;
public:
Book();
string getTitle();
string getAuthor();
void setTitle(string Title1);
void setAuthor(string Author1);
};
Main:
#include <iostream>
#include <cstring>
#include <iomanip>
#include "Library.h"
using namespace std;
//Sort function prototype
void s_sort (Book lib_books[], int n);
int main()
{
// Initializes n as the number of books
int n = 15;
//Declaration of array of objects
Book lib_books[15];
// Declartion of the private member "title" for each object
lib_books[0].setTitle("The Alchemist");
lib_books[1].setTitle("The Princess Bride");
lib_books[2].setTitle("The Catcher in the Rye");
lib_books[3].setTitle("1984");
lib_books[4].setTitle("Fahrenheit 451");
lib_books[5].setTitle("The Great Gatsby");
lib_books[6].setTitle("Poland");
lib_books[7].setTitle("The Cantebury Tales");
lib_books[8].setTitle("Leaves of Grass");
lib_books[9].setTitle("Where the Sidewalk Ends");
lib_books[10].setTitle("The Iliad");
lib_books[11].setTitle("Things to Think On");
lib_books[12].setTitle("The Grapes of Wraith");
lib_books[13].setTitle("Hop on Pop");
lib_books[14].setTitle("The Prince and the Pauper");
// Declartion of the private member "author" for each object
lib_books[0].setAuthor ("Paulo Coalho");
lib_books[1].setAuthor ("William Goldman");
lib_books[2].setAuthor ("J.D. Salinger");
lib_books[3].setAuthor ("George Orwell");
lib_books[4].setAuthor ("Ray Bradbury");
lib_books[5].setAuthor ("F. Scott Fitzgerald");
lib_books[6].setAuthor ("James A. Mitchener");
lib_books[7].setAuthor ("Chaucer");
lib_books[8].setAuthor ("Walt Whitman");
lib_books[9].setAuthor ("Shel Silverstein");
lib_books[10].setAuthor("Homer");
lib_books[11].setAuthor("Krishnamurti");
lib_books[12].setAuthor("John Steinbeck");
lib_books[13].setAuthor("Dr. Seuss");
lib_books[14].setAuthor("Mark Twain");
The portion of main that I'm having issues:
// Function to sort the books alphabetically by author
s_sort (lib_books,n);
cout << "\n";
// For loop to print all 15 books with author and title
for(int i = 0; i < 14; i++)
{
cout <<" "<<setw(25)<<lib_books[i].getTitle()<< " " <<lib_books[i].getTitle()<< "\n" ;
}
return 0;
}
//Sort function definition
void s_sort (Book::getTitle(), int n)
{
int m;
string hold;// Initializes 'hold' as a 'string' so all the characters will be used when it is sorted
for (int k=0; k<=n-1; k++)// 'For' loop sorts through the index
{
m = k;
for (int j=k+1; j <= n-1; j++)
{
if (lib_books[j].getTitle() < lib_books[m].getTitle());
//m = j;
}
// Swaps the lib_book[].getTitle() around and uses 'hold' as a placement
hold = lib_books[m].getTitle();
lib_books[m].getTitle() = lib_books[k].getTitle();
lib_books[k].getTitle() = hold;
hold = lib_books[m].getAuthor();
lib_books[m].getAuthor() = lib_books[k].getAuthor();
lib_books[k].getAuthor() = hold;
}
}
Implementation file:
#include <iostream>
#include <cstring>
#include "Library.h"
using namespace std;
Book::Book()
{
title = " ";
author = " ";
}
string Book::getTitle()
{
return title;
}
string Book::getAuthor()
{
return author;
}
void Book::setTitle(string Title1)
{
title= Title1;
}
void Book::setAuthor(string Author1)
{
author= Author1;
}
The problem that I am having is that it doesn't sort the array. It prints out the same list that was defined. I have tried to pass the array by reference but it needs a index number in the function prototype and definition.
Hoping someone can help a beginner out here. I searched but some of the solutions were out of scope my knowledge. I'm trying to create a function which will take in a an array of pointers to a class object and output a getName() function for each object. But I keep getting a "no instance of overloaded function". I can't figure out what the problem is. Please take a look at following code.
#include <iostream>
#include "Student.hpp"
#include <string>
using namespace std;
void getName(Student *studentArray[], int); //prototype
int main()
{
//Creates class objects with Constructors Name and Score
//Class object has a function getName() to return name
Student stud1("John", 100);
Student stud2("Mary", 90);
Student stud3 ("Jack", 85);
//Create an array of student objects
Student myArray[3]= {stud1, stud2, stud3};
//ERROR HERE NO INSTANCE OF OVERLOADED FUNCTON
getName(myArray, 3)
return 0;
}
void getName(Student *studentsArray[], int arraySize)
{
for(int index=0; index < arraySize; index++)
{
cout << studentsArray[index]->getName() << endl;
}
}
Student *studentsArray[] is an array of pointers to students:
[-> "John", -> "Mary", -> "Jack"]
While Student *studentsArray is a pointer to a student (e.g. the first of an array):
-> [ "John", "Mary", "Jack" ]
using the [] operator you can jump to the offset relative to a pointer
studentsArray[0] == (*studentsArray)
studentsArray[1] == *(studentsArray+1)
studentsArray[2] == *(studentsArray+2)
and the -> operator is syntactic sugar for (*var).
The problem is that you created an array of students instead of an array of pointers of students:
Student myArray[3] = {stud1, stud2, stud3};
should actually be
Student* myArray[3] = { &stud1, &stud2, &stud3 };
Here is a full working example:
#include <iostream>
#include "Student.hpp"
#include <string>
using namespace std;
void getName(Student *studentsArray[], int); //prototype
int main()
{
//Creates class objects with Constructors Name and Score
//Class object has a function getName() to return name
Student stud1("John", 100);
Student stud2("Mary", 90);
Student stud3("Jack", 85);
//Create an array of student objects
Student* myArray[3] = { &stud1, &stud2, &stud3 };
getName(myArray, 3);
return 0;
}
void getName(Student *studentsArray[], int arraySize)
{
for (int index = 0; index < arraySize; index++)
{
cout << studentsArray[index]->getName() << endl;
}
}
The most important thing is that your pointer doesn't actually point any existing variable or its address. In this case, pointer will instead point a randomly chosen memory cell address, so it's really dangerous because you will never know what it will cause, including data lose, memory leak and so on.
With std::vector, it would be something like:
#include "Student.hpp"
#include <iostream>
#include <string>
#include <vector>
void displayNames(const std::vector<Student>& students);
int main()
{
Student stud1("John", 100);
Student stud2("Mary", 90);
Student stud3("Jack", 85);
std::vector<Student> students = {stud1, stud2, stud3};
displayNames(students);
return 0;
}
void displayNames(const std::vector<Student>& students)
{
for (const auto& student : students)
{
std::cout << student.getName() << std::endl;
}
}
Main class code:
#include <iostream>
#include "Chair.h"
using namespace std;
int main()
{
cout << "Hello world!" << endl;
Chair c1;
c1.chairType("Olivia",4,32,true); // not working
Chair c2;
c1.chairType("Stephano",8,8,false);
return 0;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Header class code:
#ifndef CHAIR_H_INCLUDED
#define CHAIR_H_INCLUDED
#include <iostream>
using namespace std;
class Chair
{
private:
int legCount;
int height;
bool cushioned;
string name;
public:
void chairType(string newName, int newLegCount, int newHeight, bool cush);
};
#endif // CHAIR_H_INCLUDED
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Chair.cpp class:
#include <iostream>
#include "Chair.h"
using namespace std;
void Chair::chairType(string newName, int newLegCount, int newHeight, bool cush)
{
name=newName;
legCount=newLegCount;
newHeight=newHeight;
cushioned=cush;
cout<<"I'm a chair, the following are my specs: "<<endl;
cout<<"Model: "<<name<<endl;
cout<<"Num of Legs: "<<legCount<<endl;
cout<<"Height: "<<height<<endl;
cout<<"Cushioned? : "<<cush<<endl;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Output for the four variables fed into the method is as expected, apart from the third variable (second int) which is printing as being 2752192 regardless of what I feed to it, and for both objects c1 and c2.
I'm new to C++. I've been practising some object class questions trying to familiarise myself with the syntax. I vaguely understand pointers can cause reference addresses to print on occasion. However, this 7 digit number doesn't appear to be in address format. I've done some searches and can't see a similar question. If there is one, I would appreciate direction to it. I don't wish to break the terms of posting here on the site. Thank you in advance for your assistance.
newHeight=newHeight;
should be replaced with
height=newHeight;
but better you should initialize object in constructor, rather than separate method:
class Chair
{
private:
int legCount;
int height;
bool cushioned;
string name;
public:
Chair( const string &newName, int newLegCount, int newHeight, bool cush) :
legCount( newLegCount ),
height( newHeight ),
cushioned( cush ),
name( newName )
{
}
...
};
int main()
{
cout << "Hello world!" << endl;
Chair c1("Olivia",4,32,true); // works now
Chair c2("Stephano",8,8,false);
return 0;
}
this way you cannot have instance of your class uninitialized and your mistake also would be detected by compiler.
Here is the mistake in your implementation Chair.cpp:
newHeight=newHeight;
This is the correct:
height = newHeight;
The long number you get is the uninitialized value of member variable height in your Chair object.