I've been debugging this code for hours trying to get the output correct and g++ error free. It was working earlier but there were logic errors in the output so then I went in and added the loop and an extra parameter in the output function.
Now g++ gives me the following error:
Student.cpp: In member function ‘void Student::InputData(std::string, int, std::string&)’: Student.cpp:81:21: error: invalid conversion from ‘std::string* {aka std::basic_string*}’ to ‘char’ [-fpermissive] /usr/include/c++/4.6/bits/basic_string.h:560:7: error: initializing argument 1 of ‘std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT) [with _CharT = char, _Traits = std::char_traits, _Alloc = std::allocator, std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string]’ [-fpermissive]
How can this code be fixed?:
//This program defines a class for storing the names of classes that a student has enrolled in.
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
class Student
{
public:
Student();
Student(Student& obj); // copy constructor
~Student();
void InputData(string,int,string&); // Input all data from user
void OutputData(); // Output class list to console
void ResetClasses(); // Reset class list
Student& operator =(const Student& rightSide){
this->name=rightSide.name;
this->numClasses=rightSide.numClasses;
this->classList=rightSide.classList;
}
// Assignment operator
private:
string name;
int numClasses;
string *classList;
};
// --------------------------------
// ----- ENTER YOUR CODE HERE -----
// --------------------------------
// ======================
// Student::Student
// The default constructor initialized variables to empty.
// The dynamic array is intialized to NULL.
// ======================
Student::Student () {
name="";
numClasses=0;
classList=NULL;
}
// ======================
// Student::Student
// The copy constructor creates a deep copy of the parameter object
// ======================
Student::Student (Student& obj) {
obj.name=name;
obj.numClasses=numClasses;
obj.classList=classList;
}
// ======================
// Student::~Student
// The destructor frees up any memory allocated to
// the dynamic array.
// ======================
Student::~Student () {
delete classList;
}
// ======================
// Student::ResetClasses
// This method deletes the class list
// ======================
void Student::ResetClasses () {
if(classList) { delete [] classList;}
}
// ======================
// Student::InputData
// This method inputs all data from the user.
// It allows the user to enter an arbitrary number of classes
// using a dynamic array to store the strings.
void Student::InputData(string nm, int nClasses, string& names) {
name=nm;
numClasses=nClasses;
delete classList;
for (int i=0; i<nClasses; i++) {
names=new string[i];
}
}
// Reset the class list before entering data just in case this method
// was called repeatedly and the dynamic array wasn't cleared
// ======================
// ======================
// Student::OutputData
// This method outputs the data entered by the user.
// ======================
void Student::OutputData() {
cout << "Student name : " << name <<endl;
cout << "Student number of classes : " << numClasses <<endl;
cout << "Student class list : " <<classList<<endl;
}
// ======================
// Student::=
// operator, we would end up with two references to the same
// class list when the assignment operator is used.
// ======================
//
// --------------------------------
// --------- END USER CODE --------
// --------------------------------
// ======================
// main function
// ======================
int main()
{
// Test our code with two student classes
Student s1, s2;
string sname;
int snumClasses;
string snames[]="";
cout << "Enter student name, number of classes, and names of classes for first student" << endl;
cin >> sname; cin >> snumClasses;
int i;
for (i=0; i < snumClasses; i++) {
cin >> snames[i];
}
s1.InputData(sname, snumClasses, snames[i]); // Input data for student 1
cout << "Student 1's data:" << endl;
s1.OutputData(); // Output data for student 1
cout << endl;
s2 = s1;
cout << "Student 2's data after assignment from student 1:" << endl;
s2.OutputData(); // Should output same data as for student 1
s1.ResetClasses();
cout << "Student 1's data after reset:" << endl;
s1.OutputData(); // Should have no classes
cout << "Student 2's data, should still have original classes:" << endl;
s2.OutputData(); // Should still have original classes
Student s3(s2); // explicit copy constructor call
cout << "Student 3's data after assignment from student 2:" << endl;
s2.OutputData(); // should have the same classes as student 2
cout << endl;
return 0;
}
classList
and understand its management better. – Keith