【问题标题】:C++ Floating point exception with global list array具有全局列表数组的 C++ 浮点异常
【发布时间】:2011-04-27 00:24:26
【问题描述】:

我才刚刚开始,但我已经遇到了麻烦。到目前为止,我的代码很简单:

(在 Searcher.h 中)

#ifndef SEARCHER_H
#define SEARCHER_H

#include <string>
#include <list>
using namespace std;

class Searcher{

 public:
  Searcher( int& x );
  ~Searcher();

 private:
  int size;
  list<string> * lists;
};
#endif

(在 Searcher.cpp 中)

#include "Searcher.h"
Searcher::Searcher (int& x){
  lists = new list<string>[x];
}

(在 testSearcher.cpp 中)

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

int main (){
   Searcher * x = new Searcher(211);
}

它可以编译,但是当我运行它时,它给出了一个浮点异常。我什至用 211 替换了 x 也无济于事。预先感谢您的任何帮助。另外,为了业余调试它,我在初始化之前在构造函数中放了一条 cout 语句,它打印得很好,然后 g++ 给了我浮点异常。

【问题讨论】:

  • 请发布完整的代码,而不是一些片段。上面的代码不会导致异常,尽管它有自己的问题。
  • @Robert - 编辑问题,不要在评论中发布代码
  • @Robert:这并没有告诉我们什么。那里没有任何东西会导致浮点异常。编辑问题以包含足够的代码来触发异常。您可能需要自己做一些工作来将代码精简为可以发布并易于阅读但仍会导致异常的内容。目前,我们无能为力帮助您(除了告诉您永远不要在标题中使用 using namespace std;)。
  • 这就是我目前所拥有的一切,我才刚刚开始。这就是我感到困惑的原因。
  • 我刚刚编译了你用 g++ 发布的内容。不过,我需要进行一些更改。 Searcher(int &x) 应该只是 Searcher(int x) 因为它没有理由成为参考,并且您不能将 const (即 - 211)传递给它(我不确定您是如何做到的编译)。另外我添加了一个 return 0;到你的主要功能。它运行没有问题。

标签: c++ arrays list floating-point


【解决方案1】:

尝试将参数设置为搜索器“const int &x”。

【讨论】:

    【解决方案2】:

    不应该是:

    Searcher::Searcher (int& x) {
        lists = new list<string>(x);
    }
    

    我从未见过您使用 [x] 发布的语法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-21
      • 1970-01-01
      • 1970-01-01
      • 2021-07-16
      • 1970-01-01
      • 2010-09-10
      • 2011-03-15
      相关资源
      最近更新 更多