简体   繁体   English

C ++程序不进入for循环

[英]C++ Program does not enter into for loop

I am new to C++ and I'm trying to do the following: 我是C ++的新手,我正在尝试执行以下操作:

1) I have created a class of objects called Objects which contains the name of the object and a number to identify them. 1)我创建了一类名为Objects的对象,它包含对象的名称和一个用于标识它们的数字。

2) I have created a class Group which inherits from list, as follows: 2)我创建了一个继承自list的类Group,如下所示:

#include "objects.h"
#include <list>
#include <iostream>
#include <string> using namespace std;

class Group : public std::list<Objects*> { private:
    string groupname;

public:
    Group(string groupname);
    virtual ~Group() {} //destructor

    virtual string getGroupName() const;
    virtual void showlist(ostream & sl) const; };

3) Then, I have implemented the method showlist as follows: 3)然后,我实现了方法showlist如下:

void Groupe::showlist(ostream & sl) const{
        printf("I'm here 1\n");

        for(auto it = this->begin(); it != this->end(); it++){
             printf("I'm here 2\n");

             sl << this->getGroupName() << "test" << "test\n" << endl;
             std::cout << "I'm alive";
    } }

And the method getGroupName is as follows: getGroupName方法如下:

string Group::getGroupName() const{
    return groupname;
}    

4) In the main program I have created a pointer to a variable of type Group. 4)在主程序中,我创建了一个指向Group类型变量的指针。 The code compiles without any error, but when I executed it I realized that the program enters the method showlist and gets out without executing the for-loop. 代码编译没有任何错误,但是当我执行它时,我意识到程序进入方法showlist并在没有执行for循环的情况下退出。 I have tested this by putting messages with printf. 我通过使用printf放置消息来测试它。 Just the messages "Before method", "I'm here 1", and "After method" are showed in the terminal. 只是消息“前方法”,“我在这里1”,以及“后方法”在终端显示。 It doesn't show "I'm here 2". 它没有显示“我在这里2”。 I call from main as follows: 我从main打电话如下:

Group *lgroup = new Group[5] {Group("g1"), Group("g2"),Group("g3"),Group("g4"),Group("g5")};

    printf("Before method\n");
    lgroup->showlist(sl);
    printf("After method\n");
    cout << sl.str() << endl;

Could you please help me to understand why the loop is not being executed? 你能帮我理解循环没有被执行的原因吗?


Update 更新

The program didn't enter to the loop because the list was empty, as explained in the answers of the members. 该程序没有进入循环,因为列表是空的,如成员的答案中所述。

As for this case inheriting from List is a constraint, I have filled the list in the main function as follows: 至于从List继承的这种情况是一个约束,我在main函数中填写了如下列表:

Groupe *lgroup1 = new Groupe("g1");
Object *objets[3];

objets[1] = new File("/home/Documents", "b2.jpg",0,0);
objets[2] = new File("/home/Documents", "b3.jpg",0,0);
objets[3] = new File("/home/Documents", "b4.jpg",0,0);

lgroup1->push_back(objets[1]);
lgroup1->push_back(objets[2]);
lgroup1->push_back(objets[3]);

Where File is a class which inherits from the class Objects . 其中File是一个继承自Objects类的类。 This way the program compiles and executes. 这样程序就可以编译和执行。 In the command line it is shown the attribute of the class Groupe , being g1 . 在命令行中,它显示了类Groupe的属性,即g1 I want to use the method display which is already implemented in the class Objects but when I try to do it the compiler shows this error: 我想使用已在类Objects实现的方法display ,但是当我尝试这样做时,编译器会显示以下错误:

 error: 'const class Group' has no member named 'display'
              sl << this->display(cout) << '\n' << endl;

So, my question is how can I make the class Group to inherit the methods from both List (which is already done) and Objects ? 所以,我的问题是如何让类GroupList (已经完成)和Objects继承方法?

Because you are not putting anything into your list. 因为您没有在列表中添加任何内容。 You are creating five empty, unrelated lists in an array, and then stepping through the (empty) first group. 您正在数组中创建五个空的,不相关的列表,然后单步执行(空)第一个组。

Disregarding for the moment that you should not derive from the standard library containers (see Is there any real risk to deriving from the C++ STL containers? ) ... 忽略您不应该从标准库容器派生的那一刻(请参阅从C ++ STL容器派生是否存在任何真正的风险? )...

The line 这条线

Group *lgroup = new Group[5] {Group("g1"), Group("g2"),Group("g3"),Group("g4"),Group("g5")};

allocates memory for 5 Group objects and assigns that memory to lgroup . 为5个Group对象分配内存并将该内存分配给lgroup However, lgroup still has no items contained in it as a list. 但是, lgroup仍然没有包含在列表中的项目。 As a result, lgroup->begin() equals lgroup->end() . 因此, lgroup->begin()等于lgroup->end()

If you want the other Group s to be contained in lgroup , you need to use: 如果您希望其他Group包含在lgroup ,则需要使用:

Group *lgroup = new Group;
lgroup->push_back(new Group("g1"));
lgroup->push_back(new Group("g2"));
lgroup->push_back(new Group("g3"));
lgroup->push_back(new Group("g4"));
lgroup->push_back(new Group("g4"));

For that to work, you need to make Group a sub-type of Object . 要使其工作,您需要使Group成为Object的子类型。 It will be better to change your class to: 最好将您的班级改为:

class Group : public Object
{
   private:
      string groupname;

   public:
      Group(string groupname);
      virtual ~Group() {}

      std::list<Objects*>& getObjects();
      std::list<Objects*> const& getObjects() const;

      virtual string getGroupName() const;
      virtual void showlist(ostream & sl) const; 
      std::list<Objects*> objects_;
};

and then use 然后使用

Group *lgroup = new Group;
lgroup->getObjects().push_back(new Group("g1"));
lgroup->getObjects().push_back(new Group("g2"));
lgroup->getObjects().push_back(new Group("g3"));
lgroup->getObjects().push_back(new Group("g4"));
lgroup->getObjects().push_back(new Group("g4"));

For the first question, the program didn't enter to the loop because the list was empty, as explained in the answers of the members. 对于第一个问题,程序没有进入循环,因为列表是空的,如成员的答案中所解释的那样。

For the second question, it was not necessary to use multiple inheritance to inherit the methods from the class Object . 对于第二个问题,没有必要使用多重继承来继承Object类中的方法。 For this case, display is the method I want to use. 对于这种情况, display是我想要使用的方法。 It is necessary to call it with a pointer: 有必要用指针调用它:

    for(list<Object*>::const_iterator it = this->begin(); it != this->end(); it++){
        sl << this->getGroupName() << ' ' ;
             (*it)->display(sl);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM