Click here to Skip to main content
15,903,834 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Here is The CODE:
i want to call that fetch function before main()

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

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
    bool isFull();
    bool isEmpty();
    int length();
    List(const List &other); // copy construct
    const List &operator=(const List &rhs);

    private:
    bool removeAt(int index);

    int MaxSize;
    T * listarray;
    int mSize;
};
template<class T>
int List<T>::length()
{
    return mSize;
}
template<class T>
List<T>::~List()
{
    delete [] listarray;
}
template<class T>
T List<T>::getData(int index)
{
    if (index < 0 || index >= mSize) throw "Index ! Positive";
    return listarray[index];
}
template<class T>
List<T>::List(int size)
{
    if(size < 1) throw "ILLEGAL_SIZE";
    else
    {
        MaxSize = size;
        listarray = new T[MaxSize];
        if(listarray == NULL) throw "OUT_OF_MEMORY";
        mSize = 0;
    }
}
template<class T>
void List<T>::insert(const T value)
{
    if(isFull()) throw "OUT_OF_SPACE";
    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 "ILLEGAL_INDEX";
    for(int i = index; i < mSize - 1; i++)
        listarray[i] = listarray[i+1];
        mSize--;
        return true;
}
template<class T>
bool List<T>::isFull()
{
    return mSize == MaxSize;
}
template<class T>
bool List<T>::isEmpty()
{
    return mSize == 0;
}
template<class T>
int List<T>::search(T key)
{
		for (int i = 0; i < mSize; i++)
		{
			if (key == listarray[i])
			{
				key = i;
			}
		}
		return key;
}
template<class T>
void fetch(T mylist, int length)
{
	 for(int j = 0; j < length; j++)
        {
            cout << mylist.getData(j) << endl;
        }
        cout << endl;
}

int main()
{
    const int length = 3;
    try
    {
		List<int> mylist(length);
		label:
        for(int i = 0; i < length; i++)
        {
            int temp;
			cout << "Enter Element # " << i +1 << ": ";
            cin >> temp;
            mylist.insert(temp);
        }
        cout << endl;
		
		fetch(mylist, length);
       
		int key;
		cout << "Enter Key To Search: "; cin >> key;
		int key_index = mylist.search(key);
		if (key_index == -1)
			cout << "Key Not Present In The List :(" << endl;
		else
        cout << "Key Present @: " << key_index + 1 << endl;
		cout << "Priting The List " << endl;
		// OR should I write goto statement :? 
    }
	


    catch(...)
    {
        cout << "Something Else Happened .. !!" << endl;
    }
    return 0;
}
Posted
Comments
CPallini 19-May-15 12:49pm    
How could you call it before main, since its argument (mylist) is scoped by main?

main is a reserved name for the startup of program. It is "The mother of all functions".

You must exlude fetch from the class, or declare it as static and than call like: "class-name"::fetch(...)
 
Share this answer
 
Comments
Usman Hunjra 19-May-15 12:35pm    
yes sir, i tried that but it dosen't work .. :(
The only way you might call a function before main is making it part of the construction of a global (or static) object, e.g.
C++
#include <iostream>
using namespace std;

class A
{
  int n;
public:
  A(int n):n(n){show();}
  void show(){cout << n << endl;}
};

A a(3);

int main()
{
  cout << "main" << endl;
}


However, you fetch function needs an instance of List<int> that is currently scoped by main.
 
Share this answer
 
v2
Comments
Usman Hunjra 19-May-15 13:39pm    
sir I've tried that but it doesn't work that way ..
int main()
List<int> mylist(length);
mylist.fetch(mylist, length);
Usman Hunjra 19-May-15 13:41pm    
ERROR List<int> has no member fetch .. :(

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