呼唤全球功能 [英] Calling The Global Function
本文介绍了呼唤全球功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是代码:
i想在main()之前调用该获取函数
#include < iostream >
使用 命名空间跨度> ::性病;
模板< class T>
class 列出
{
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); // 线性搜索
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;
};
模板< class T>
int 列表< T> :: length()
{
return mSize;
}
模板< class T>
列表< T> ::〜List()
{
delete [] listarray;
}
模板< class T>
T List< T> :: getData( int index)
{
if (index< 0 || index> = mSize) throw 索引!正面;
return listarray [index];
}
模板< class T>
列表< T> ::列表( int 大小)
{
if (size< 1 ) throw ILLEGAL_SIZE;
else
{
MaxSize = size;
listarray = new T [MaxSize];
if (listarray == NULL) throw OUT_OF_MEMORY;
mSize = 0 ;
}
}
模板< class T>
void 列表< T> :: insert( const T 值)
{
if (isFull()) throw OUT_OF_SPACE;
listarray [mSize] = value ;
mSize ++;
}
模板< class T>
bool 列表< T> :: remove( const T 值)
{
int index = search( value 跨度>);
if (index == - 1 )
return false ;
else return removeAt(index);
}
模板< class T>
bool 列表< 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 ;
}
模板< class T>
bool 列表< T> :: isFull()
{
return mSize == MaxSize;
}
模板< class T>
bool 列表< T> :: isEmpty()
{
return mSize == 0 ;
}
模板< class T>
int 列表< T> :: search(T key)
{
for ( int i = 0 ; i< mSize; i ++)
{
if (key == listarray [i])
{
key = i;
}
}
返回键;
}
模板< 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 ;
尝试
{
List< int> MYLIST(长度);
label:
for ( int i = 0 ; i< length; i ++)
{
int temp;
cout<< 输入元素#<< i +1<< :;
cin>>温度;
mylist.insert(temp);
}
cout<< ENDL;
fetch(mylist,length);
int 键;
cout<< 输入要搜索的键:; cin>>键;
int key_index = mylist.search(key);
if (key_index == - 1 )
cout<< 列表中不存在键:(<< endl;
else
cout<< Key Present @ :<< key_index + 1 << endl;
cout<< Priting List<< endl;
// 或者我应该写goto语句:?
}
catch (...)
{
cout<< 其他事情发生.. !!<< endl;
}
return 0 ;
}
解决方案
main是保留名称fo程序的启动。它是所有职能的母亲。
你必须从类中排除fetch,或者将它声明为静态而不是调用:class-name:: fetch(...)
在main
之前调用函数的唯一方法是将其作为构建全局(或静态)对象的一部分,例如
#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;
}
但是,你fetch
函数需要一个实例列表< int>
目前的范围是main
。
Here is The CODE:
i want to call that fetch function before main()
#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;
}
解决方案
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(...)
The only way you might call a function beforemain
is making it part of the construction of a global (or static) object, e.g.
#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, youfetch
function needs an instance ofList<int>
that is currently scoped bymain
.
这篇关于呼唤全球功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文