operator =不明确 [英] operator= is ambiguous

查看:182
本文介绍了operator =不明确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Guys,希望您身体健康。

我正在尝试编写代码以维护学生名单。



遇到问题,即:运营商=暧昧

我尝试了所有可能性来纠正这个问题,但失败了,现在我来这里寻求帮助......



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 ..

#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 .. 





代码(V1):

这是问题陈述:

开发一个系统来存储和查询学生及其所学课程的信息。有关学生的信息包括他/她的GIFT卷号和姓名。课程信息包含该课程中的课程代码和GPA。学生最多可以修读50门课程,并将这些信息保存在列表中。学生列表也是一个列表。这些列表应该是有序列表。

您需要提供以下操作:

1.创建一个空的学生列表。

2。将新学生添加到列表中。

3.从列表中删除学生。

4.为特定学生添加课程。

5.删除给定学生的课程。

6.根据学生编号,查找学生是否在列表中。 (使用二分搜索)

7.给定学生编号,计算学生的CGPA。

8.给出学生编号,列出学生已经学过的所有课程以及每门课程的获奖成绩和GPA。

9.列出所有学生。 (完整的详细信息 - 学生和他的成绩)

您必须使用ADT列表来存储有关学生和成绩的信息。要访问数据,必须使用迭代器。在此过程中,您还必须为系统的正常工作编写所有必要的函数(例如,复制构造函数和赋值运算符,如果需要)。





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. ??? ?? ? 







#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




Keenly waiting Your Suggestions.

-Thanks

推荐答案

I tested your code (with gcc 4.7.3), after having added the missing destructor and a dumb main function, namely

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

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





It compiles and runs, producing the following output:



It compiles and runs, producing the following output:

Memory Allocated SuccessfullY ..


这篇关于operator =不明确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆