开发者

C++ Floating point exception with global list array

开发者 https://www.devze.com 2023-01-20 19:50 出处:网络
I\'m just getting started, but I\'m already having trouble.So far, my code is simply: (In Searcher开发者_JAVA百科.h)

I'm just getting started, but I'm already having trouble. So far, my code is simply:

(In Searcher开发者_JAVA百科.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

(In Searcher.cpp)

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

(In testSearcher.cpp)

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

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

It compiles, but when I run it, it gives a floating point exception. I even replaced x with 211 to no avail. Thank you in advance for any help. Also, to amateur debug it, I put a cout statement in the constructor before the initialization and it printed fine, then g++ gave me the floating point exception.


Try making the parameter to Searcher "const int &x".


Shouldn't it be:

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

I've never seen the syntax you posted with the [x].

0

精彩评论

暂无评论...
验证码 换一张
取 消