mercredi 25 avril 2018

template on OOP in c++

so its the first time that i write a code on the subject of template. the problem starting after i got to the function

optionsOfTheClassArraylist(ArrayList<T> &obj)

in this func i call another function in header files to do things in my Dynamic array, like add elements, print, found which is most bigger/lower and remove him from dynamic array and all this... this is the list of all func in header file.

public:

ArrayList();//def c'tor
ArrayList(const ArrayList&); //copy c'tor.
~ArrayList();//remove the dynamic array and print arraylist deleted and size of his array.
void addElement(T toAdd);//add element in specific index.
void addElement(T toAdd, int index);//Add 
T popMin();//return the min element and delete him from array and deacrease array by 1.
T popMax();//return the max element and delete him from array and deacrease array by 1.
int getSize() const;//return the size of array.
void print() const; // print all elements by order.

so the main problem is that i got lots of error, like in the else if statement: else if (choose == 2) the error come when i call another function.obj.addElement(AddElemn, index); and i think becouse of that i have more 3 errors likes exepted ; or decleration. and one another that i dont not what hes meaninig.

Error   2   error C1075: end of file found before the left brace '{' at 'c:\users\yacov\documents\visual studio 2013\projects\ex6\ex6\arraylist .h(118)' was matched    c:\users\yacov\documents\visual studio 2013\projects\ex6\ex6\arraylist .cpp 77  1   EX6

so this is my main.cpp

#include "ArrayList .h";
#include <iostream>
using namespace std;

template <class T>
void optionsOfTheClassArraylist(ArrayList<T> &obj){
    int choose = 0;
    do {
        cout << "1. Add elements to ArrayList " << endl;
        cout << "2. Add elements to the specific index (two parameters,index and value. " << endl;
        cout << "3. Found which elemts in array is the most lower, remove him from array and print the value. " << endl;
        cout << "4. Found which elemts in array is the most bigger, remove him from array and print the value. " << endl;
        cout << "5. Print the size of ArrayList. " << endl;
        cout << "6. Print all elements of ArrayList. " << endl;
        cout << "7. exit. " << endl;
        cin >> choose;

        if (choose == 1){
            T AddElement = 0;
            cout << "Enter the element: " << endl;
            cin >> AddElement;
            //we pass to function the element that we want add.
            obj.addElement(AddElement);
        }
        else if (choose = 2){

            T AddElemn = 0;
            int index;
            cout << "Enter the element: " << endl;
            cin >> AddElemn;
            cout << "Enter the index: " << endl;
            cin >> index;
            //we pass to function the element and the specific index.
            obj.addElement(AddElemn, index);
        }
        else if (choose == 3){
            obj.popMin();
        }
        else if (choose == 4){
            obj.popMax();
        }
        else if (choose == 5){
            obj.getSize();
        }
        else if (choose == 6){
            obj.print();
        }
        else if (choose < 1 && choose > 7){
            cout << "Invalid value, please select number between 1 - 3. " << endl;
        }
        else{
            cout << "you press 7, so we exit, thanks! " << endl;
        }
    } while (choose != 7);
    }


     int main(){
    cout << "for Array list of int preese 1. " << endl;
    cout << "for Array list of char preese 2. " << endl;
    cout << "preese 3 to exit. " << endl;
    int choose = 0;

    if (choose == 1){
        ArrayList <int>ArrayOfInt;
        optionsOfTheClassArraylist(ArrayOfInt);
    }
    else if (choose == 2){
        ArrayList <char>ArrayOfChar;
        optionsOfTheClassArraylist(ArrayOfChar);
    }
    else{
        cout << "Invalid value, please select number between 1 - 3. " << endl;
    }
    return 0;
  }//end main

Aucun commentaire:

Enregistrer un commentaire