2017-04-03 49 views
-2

首先我很抱歉格式,这是我第一次在这个网站上发布。下面是我的程序的开始,它只是一个简单的菜单,它具有不同的排序和搜索以及创建列表的方式。当我尝试在我的项目列表上使用.empty时,我的错误在于菜单方法。我得到了非类型成员的错误请求。我只是在编程的第二年,所以试图解释,像我是一个白痴大声笑。请求会员,这是非班级类型使用.empty或.size

const int MLS = 50; 

typedef int element; 

const element SENTINEL = -1; 

element read_element(); 

int read_int(); 

class AList{ 

     private: 
       element items[MLS]; 
       int size; 
       void Swap(int pos1, int pos2); 
       bool sorted; 
     public: 
       void Read(); 
       void GenerateRandomList(); 
       void Print(); 
       void BubbleSort(); 
       void InsertionSort(); 
       void SelectionSort(); 
       void LinearSearch(element target); 
       void BinarySearch(element target); 
       void Menu(); 
}; 

int main(){ 

    AList A; 

     A.Menu(); 
} 


void AList::Menu(){ 

     int choice; 
     element target; 


     cout << "Current list: "; 
     if (items.empty == true) 
       cout << "(empty)"; 
     else 
       Print(); 
     if (sorted == true) 
       cout << "(KNOWN to be ordered)" << endl << endl; 
     else 
       cout << "(NOT KNOWN to be ordered)" << endl << endl; 

     cout << "Actions:" << endl; 
     cout << "  1. Reset the current list from the keyboard" << endl; 
     cout << "  2. Reset the current list using randomly generated "; 
+1

请正确格式化您的代码。你可以在这里找到帮助(https://stackoverflow.com/help/formatting)。 –

+0

无论在这里新增什么,您都可以使用1360万个现有问题来查看应该看到的问题。 –

回答

1
typedef int element; 
... 
class AList { 
    element items[MLS]; 
    ... 
}; 
... 
void AList::Menu() { 
    ... 
    if (items.empty == true) 
     ... 
    ... 
} 

正如你所看到的,items只是int秒的阵列。但是,在C++中,数组既没有命名成员(例如java的arr.length),也没有与它们关联的方法。如果你想在你的代码中使用这个功能,我推荐你使用std::vector

1

从我所看到的,items只是element阵列。在C++数组中没有任何方法/属性。这只是对齐的数据没有任何逻辑。考虑改用vector

编辑:改变

element items[MLS] 

vector<element> items 

你将能够使用items.empty()items.size()你赢了,T需要设置的50的初始大小,向量将调整本身每当它需要。

+0

我该怎么做呢? –

+0

@ S.Speaks更新了我的答案。 – AlexG