Click here to Skip to main content
15,904,415 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Guys, Hope you are at good state of health ..
i'm trying to write code to maintain a list of students.

encountered a problem i.e: Operator = is ambigous
I tried all the possibilities to rectify that, but failed, now i'm here for some help ..

C++
#include <iostream>
#include <string>
using namespace::std;

//List: A collection of elements of the same type.

template<class T>
class List
{
public:
	List(int size); 
	~List(); 
	void insert(const T value);
	bool remove(const T value);
	void setData(int index, T value);
	T getData(int index);
	int  search(T key);  // linear search
	// if found, returns index of the key
	// else returns -1
	void printArr() const;
	bool isFull() const;
	bool isEmpty() const;
	int  length() const;
	List(const List& other); // copy constructor
	const List& operator=(const List& rhs); // assignment operator
private:
	bool removeAt(int index);  // remove the element at a given index
	int MaxSize;      // max List size
	T *listArray;      // array to store the data
	int mSize;        // no. of elements in the List
	string sName;
	int rollNo;
};
template<class T>
List<T>::List(int size){
	if (size < 0){
		throw -1;
	}
	else
	MaxSize = size;
	mSize = 0;
	sName = " ";
	rollNo = -69;
	listArray = new T[MaxSize];
		if (listArray == NULL){
			throw 1;
		}
		else{
			cout << "Memory Allocated SuccessfullY .. " << endl;
			for (int i = 0; i < MaxSize; i++)
			{
				listArray[i] = 0;
			}
		}
}
template<class T>
List<T>::List(const List<T>& other){ // Well Copy Constructor //
	MaxSize = other.MaxSize;
	mSize = other.mSize;
	listArray = new T[mSize]; // always check for NULL ?:
	if (listArray == NULL)
	{
		throw 1;
	}
	for (int i = 0; i < mSize; i++)
	{
	//	*(listArray + i) = *(other + i)
		listArray[i] = other.listArray[i];
	}
}
template<class T>
//List<T>::operator=(const List<T>& rhs){
const List<T>& List<T>::operator=(const List<T>& rhs){ // Finally OverLoaded Assignment Operator =
	if (this != &rhs)
	{
		MaxSize = rhs.MaxSize;
		mSize = rhs.mSize;
		delete listArray;
		listArray = new T[mSize];
		if (listArray == NULL)
		{
			throw 1; //"OUT_OF_MEMORY"
		}
		for (int i = 0; i < mSize; i++)
		{
			listArray[i] = rhs.listArray[i];
		}
		return *this;
	}
	else
		cout << "Whatever " << endl;
}


The statement listArray[i] = 0; in the constructor cause the error .. 


CODE(V1):
Here is the Problem statement:
Develop a system to store and query information about students and the courses they have taken. The information about the student includes his/her GIFT roll number and name. The course information has the course code and the GPA in that course. A student can take up to 50 courses and this information is maintained in a list. The list of students is also a list. These lists should be ordered lists.
You need to provide the following operations:
1. Create an empty list of students.
2. Add a new student to the list.
3. Delete a student from the list.
4. Add a course for a given student.
5. Delete a course for a given student.
6. Given the roll number, find if a student is present in the list. (use binary search)
7. Given the roll number, compute the CGPA of the students.
8. Given the roll number, list all the course a student has taken along with the awarded grades and GPA for each course.
9. List all the students. (complete details – student and his grades)
You have to use the ADT for list for storing the information about students and grades. In order to access the data, you must use iterator. In the process you also have to write all the necessary functions for the proper working of the system (e.g. the copy constructors and assignment operators, if needed).

I'm much frustrated regarding this, Please help me for good statrup, Please, :(
this is a new concept for me(templates, Iterators) :(
what i know is: there should be 4 classes:
class Student {...};
class Course {...};
class StudentIterator {...};
class CourseIterator {...};

where i stuck is:
what i write in <type>, actually i want to create suppose 5 Students. But how can i do that. ??? ?? ? 


XML
#include <iostream>
#include <string>
using namespace::std;

//List: A collection of elements of the same type.

template<class T>
class List // List of Students
{
public:
    List(int size);
    ~List();
    void insert(const T value);
    bool remove(const T value);
    void setData(int index, T value);
    T getData(int index);
    int  search(T key);  // linear search
    // if found, returns index of the key
    // else returns -1
    void printArr() const;
    bool isFull() const;
    bool isEmpty() const;
    int  length() const;
    List(const List& other); // copy constructor
    const List& operator=(const List& rhs); // assignment operator
private:
    bool removeAt(int index);  // remove the element at a given index
    int MaxSize;      // max List size
    T *listArray;      // array to store the data
    int mSize;        // no. of elements in the List
    string sName;
    int rollNo;
};
template<class T>
List<T>::List(int size){
    if (size < 0){
        throw -1;
    }
    else
    MaxSize = size;
    mSize = 0;
    sName = " ";
    rollNo = -69;
    listArray = new T[MaxSize];
        if (listArray == NULL){
            throw 1;
        }
        else{
            cout << "Memory Allocated SuccessfullY .. " << endl;
            /*for (int i = 0; i < MaxSize; i++)
            {
                listArray[i] = 0;
            }*/
        }
}
template <class T>
List<T>::~List(){
    delete[] listArray;
}
template<class T>
void List<T>::printArr() const{
    for (int i = 0; i < mSize; i++)
    {
        cout << *(listArray + i) << " ||";
    }
    cout << endl;
}
template<class T>
List<T>::List(const List<T>& other){ // Well Copy Constructor //
    MaxSize = other.MaxSize;
    mSize = other.mSize;
    listArray = new T[mSize]; // always check for NULL ?:
    if (listArray == NULL)
    {
        throw 1;
    }
    for (int i = 0; i < mSize; i++)
    {
    //  *(listArray + i) = *(other + i)
        listArray[i] = other.listArray[i];
    }
}
template<class T>
//List<T>::operator=(const List<T>& rhs){
const List<T>& List<T>::operator=(const List<T>& rhs){ // Finally OverLoaded Assignment Operator =
    if (this != &rhs)
    {
        MaxSize = rhs.MaxSize;
        mSize = rhs.mSize;
        delete listArray;
        listArray = new T[mSize];
        if (listArray == NULL)
        {
            throw 1; //"OUT_OF_MEMORY"
        }
        for (int i = 0; i < mSize; i++)
        {
            listArray[i] = rhs.listArray[i];
        }
        return *this;
    }
    else
        cout << "Daru Desi " << endl;
}
template<class T>
bool List<T>::isEmpty()const {
    return (mSize == 0);
}
template<class T>
bool List<T>::isFull()const{
    return (mSize == MaxSize);
}
template<class T>
int List<T>::length()const {
    return mSize;
}
template<class T>
void List<T>::insert(const T value)
{
    if (isFull()) throw 2;
    listArray[mSize] = value;
    mSize++;
}

template<class T>
bool List<T>::remove(const T value)
{
    int index = search(value);
    if (index == -1) return false;
    else return removeAt(index);
}

template<class T>
bool List<T>::removeAt(int index)
{
    if (index < 0 || index >= mSize)
        throw 3;
    else
    {
        listArray[index] = listArray[mSize - 1];
        mSize--;
        return true;
    }
}
template<class T>
T List<T>::getData(int index)
{
    if (index < 0 || index >= mSize) throw 3;
    else    return listArray[index];
}

template<class T>
void List<T>::setData(int index, T value)
{
    if (index < 0 || index >= mSize) throw 3;
    else     listArray[index] = value;
    mSize++;
}

template<class T>
int List<T>::search(T key)
{
    for (int i = 0; i < mSize; i++)
    if (listArray[i] == key) return i;
    else
    return -1;
}

int main(){
    try{
        string name;
        int id;
        List<int> s_id(7);
        List<string> s_name(7);
        for (int i = 0; i < 5; i++)
        {
            cin >> name;
            s_name.insert(name);
            cin >> id;
            s_id.insert(id);

        }
        cout << endl;
        for (int j = 0; j < s_name.length(); j++)
        {
            cout << s_name.getData(j) << "\t" << s_id.getData(j) << endl;
        }
    }
    catch (int x){
        if (x == -1){
            cout << "Size Should Be >= 0 ERROR CODE: -1" << endl;
        }
        else if (x == 1){
            cout << " OUT_OF_MEMORY ERROR CODE: 1" << endl;
        }
        else if (x == 2){
            cout << " OUT_OF_SPACE ERROR CODE: 2" << endl;
        }
        else if (x == 3){
            cout << " ILLEGAL_INDEX ERROR CODE: 3" << endl;
        }
        else
            cout << "Some Thing Else happened " << endl;
    }
    catch (...){
        cout << "Whatever .. " << endl;
    }

    return 0;
}



Keenly waiting Your Suggestions.

-Thanks
Posted
Updated 7-Apr-14 6:37am
v2
Comments
Richard MacCutchan 7-Apr-14 12:16pm    
The compiler cannot decide which List you mean. I would suggest you change your code so that everything is not defined as a List.
Usman Hunjra 7-Apr-14 12:41pm    
thank you sir, for your reply. sir i've updated the code & the problem statement as well..
sir how can i create objects using template .. :? :(
Richard MacCutchan 7-Apr-14 12:50pm    
Why are you trying to create templates when you obviously have difficulties with them? Start by creating the classes you need for your object types, and work from there. If you want to create lists of objects then use the standard template library list class.
Usman Hunjra 7-Apr-14 12:56pm    
yes sir i understand, but that is required .. by the instructor in the data structures course.. :(

1 solution

I tested your code (with gcc 4.7.3), after having added the missing destructor and a dumb main function, namely
C++
template<class T>
List<T>::~List()
{
  delete [] listArray;
}

int main()
{
  List<int> l=List<int>(5);
}


It compiles and runs, producing the following output:
Memory Allocated SuccessfullY ..
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 7-Apr-14 14:19pm    
Good effort, a 5.

It's a bit late but... have you seen my 1st of April publication?
Power Over IP: Testing of the First Experimental Facility.

Hope it can feel you more energetic :-) and fun; and perhaps you can ask me some difficult questions. ;-)

—SA
Usman Hunjra 7-Apr-14 14:27pm    
thank you sir, thank you for your time .. actually i want to create 5 objects of type student.. ??
can i do that OR i should use Associative Contrainer OR if do something:
List<string> student_name(5);
List<int> student_id(5);
...
here the message: Memory Allocated SuccessfullY ..
printed twice .. and then i can store 5 objects of students having attributes Names and RollNo. :(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900