2015-10-15 86 views
0

我有一個程序使用迭代器來打印對象。指向每個對象的指針都存儲在一個向量中。當我嘗試通過取消引用迭代器來打印這些對象時,它只是打印內存位置。我相信它是因爲我有一個指向性的間接層,但是當我再次解除引用時,我得到了一個我一直無法追蹤的seg故障。任何人都可以找到問題嗎?解除迭代器到指針

int main(int argc, const char * argv[]){ 
    ObjectFactory* objFact = readInput(); 
    for (ObjectFactory::const_iterator p = objFact->begin(); p!=objFact->end(); ++p){ 
     std::cout << *p << " "; 
    } 

    return 0; 
} 

ObjectFactory* readInput(){ 
    std::vector<MyObject*> myObjects; 
    ObjectFactory* objFact = ObjectFactory::get_inst(); 
    std::string input; 
    int years_passed; 
    std::cin >> years_passed; 
    std::cin >> input; 
    int age = 0; 
    float diameter = 0; 
    while (std::cin){ 
      try{ 
        if (input == "int"){ 
          int value; 
          if (std::cin >> value){ 
            if (value > 0){ 
              age = value; 
              std::cin >> input; 
            } 
            else{ 
              std::cin >> input; 
              throw negativeNumber("integer", value); 
            } 
          } 
          else{ 
            std::cin.clear(); 
            std::cin >> input; 
            throw missingValue("int", input); 
          } 
        } 
        else if (input == "float"){ 
          float value; 
          if (std::cin >> value){ 
            if (value > 0){ 
              diameter = value; 
              std::cin >> input; 
            } 
            else{ 
              std::cin >> input; 
              throw negativeNumber("float", value); 
            } 
          } 
          else{ 
            std::cin.clear(); 
            std::cin >> input; 
            throw missingValue("float", input); 
          } 
        } 
        else if (input == "string"){ 
          std::string value; 
          std::cin >> value; 
          if (diameter == 0 || age == 0){ 
            std::cin >> input; 
            throw incompleteObject(value); 
          } 
          try{ 
            objFact->new_object(value, diameter, age); 
          } 
          catch (invalidAge ex){ 
            ex.printMessage(); 
          } 
          std::cin >> input; 
        } 
        else{ 
          std::string tempInput = input; 
          std::cin >> input; 
          throw unrecognizedType(tempInput); 
        } 
      } 
      catch (unrecognizedType ex){ 
        ex.printMessage(); 
      } 
      catch (incompleteObject ex){ 
        ex.printMessage(); 
      } 
      catch (negativeNumber ex){ 
        ex.printMessage(); 
      } 
      catch (missingValue ex){ 
        ex.printMessage(); 
      } 
    } 
    return objFact; 
} 

class ObjectFactory { 
public: 
    static ObjectFactory* get_inst(); 
    MyObject* new_object(std::string name, float diameter, int age); 
    void time_passed(int year); 
    typedef std::vector<MyObject*>::const_iterator const_iterator; 
    const_iterator begin() const{return objects.begin();} 
    const_iterator end() const{return objects.end();} 
protected: 
    ObjectFactory(){} 
private: 
    static ObjectFactory* _instance; 
    std::vector<MyObject*> objects; 
}; 

ObjectFactory* ObjectFactory::_instance=0; 
ObjectFactory* ObjectFactory::get_inst(){ 
    if (_instance ==0) {_instance = new ObjectFactory;} 
    return _instance; 
} 
//Create the correct object type based of the name 
MyObject* ObjectFactory::new_object(std::string name, float diameter, int age){ 
    if (toupper(name.at(0)) == 'C'){ 
     objects.push_back((MyObject*)new Car(name, diameter, age)); 
     return objects.at(objects.size()-1); 
    } 
    if (toupper(name.at(0)) == 'T'){ 
     objects.push_back((MyObject*)new Tiger(name, diameter, age)); 
     return objects.at(objects.size()-1); 
    } 
    objects.push_back((MyObject*)new Human(name, diameter, age)); 
    return objects.at(objects.size()-1); 
} 

void ObjectFactory::time_passed(int year){ 
for (int i = 0; i < objects.size();i++){ 
    objects[i]->year_passed(year); 
    if (objects[i]->get_age()>objects[i]->get_max_age()){ 
     delete objects[i]; 
    } 
} 
} 
//Prints output for objecs of MyObject type 
std::ostream& operator << (std::ostream& out, MyObject& obj) 
{ 
    out << "(" <<obj.get_type_name() << "," << obj.get_name() << "," << obj.get_diameter() << "," << obj.get_age() << ","<< obj.get_speed() << ")"; 
} 

ObjectFactory* ObjectFactory::_instance=0; 
ObjectFactory* ObjectFactory::get_inst(){ 
if (_instance ==0) {_instance = new ObjectFactory;} 
return _instance; 
} 
//Create the correct object type based of the name 
MyObject* ObjectFactory::new_object(std::string name, float diameter, int age){ 
    if (toupper(name.at(0)) == 'C'){ 
     objects.push_back((MyObject*)new Car(name, diameter, age)); 
     return objects.at(objects.size()-1); 
    } 
    if (toupper(name.at(0)) == 'T'){ 
     objects.push_back((MyObject*)new Tiger(name, diameter, age)); 
     return objects.at(objects.size()-1); 
    } 
    objects.push_back((MyObject*)new Human(name, diameter, age)); 
    return objects.at(objects.size()-1); 
} 

void ObjectFactory::time_passed(int year){ 
for (int i = 0; i < objects.size();i++){ 
    objects[i]->year_passed(year); 
    if (objects[i]->get_age()>objects[i]->get_max_age()){ 
     delete objects[i]; 
    } 
} 
} 
//Prints output for objecs of MyObject type 
std::ostream& operator << (std::ostream& out, MyObject& obj) 
{ 
    out << "(" <<obj.get_type_name() << "," << obj.get_name() << "," << obj.get_diameter() << "," << obj.get_age() << ","<< obj.get_speed() << ")"; 
} 

我並不包括爲MyObject或它的子類或我使用的,因爲它似乎不相關的錯誤結構的代碼,但讓我知道,如果你需要它。

std::cout << *(*p) << " "; 

添加return聲明:

+0

您似乎沒有ObjectFactory的'<<'運算符。 –

+2

歡迎使用堆棧溢出。在編寫代碼時,獨立開發新功能非常重要*。你應該構造一個包含一個指向一個對象的指針(通過硬編碼構建,不需要運行時輸入),並在將它連接到I/O和其他所有對象之前完美工作。這個例子[既不完整也不小](http://stackoverflow.com/help/mcve)。 – Beta

+0

@Jonathan我的迭代器正在瀏覽ObjectFactory中名爲'objects'的數組。數組內部是MyObject指針。當我嘗試使用double取消引用時,從迭代器獲取MyObject指向MyObject本身的指針時,我收到了無法跟蹤的Seg故障。這就是爲什麼我沒有ObjectFactory的<<運算符。 – Evan

回答

1

的問題很可能是由缺少return聲明

std::ostream& operator << (std::ostream& out, MyObject& obj) 
{ 
    out << "(" <<obj.get_type_name() << "," << obj.get_name() << "," << obj.get_diameter() << "," << obj.get_age() << ","<< obj.get_speed() << ")"; 
} 

如果使用肯定會導致不確定的行爲造成的。我想將obj更改爲MyObject const&

std::ostream& operator << (std::ostream& out, MyObject const& obj) 
{ 
    out << "(" <<obj.get_type_name() << "," << obj.get_name() << "," << obj.get_diameter() << "," << obj.get_age() << ","<< obj.get_speed() << ")"; 
    return out; 
} 
+0

你釘了它!非常感謝您的幫助! – Evan

+0

@Evan,不客氣。 –