פורסם 2006 בפברואר 2219 שנים ככה, עיקר הרקע שלי הוא בג'אווה lisp ו assembly אז יוצא ש c++ מתחרבש לי טיפה.נתקלתי בבעיה די בסיסית ואני מקווה שתעזרו לי לפתור אותה.אני מצרף את כל הקוד כדי שהקונטקסט יהיה ברור :octree.h#ifndef OCTREE_H#define OCTREE_H#include <iostream>#include <vector>#include <string>using namespace std;class Point{public: float _x,_y,_z; Point(const Point& p):_x(p._x),_y(p._y),_z(p._z) {;} Point(float x=0, float y=0, float z=0):_x(x),_y(y),_z(z) {;} ~Point(){;} string toString() { string str=""; str+=" x = "; str+=_x; str+=" y = "; str+=_y; str+=" z = "; str+=_z; str+="\n"; return str; }};class Cube{public: Cube(Point p=NULL,float h=0):_h(h) { _p = Point(p); } Point getP() { return _p; } Point* getPoints() { Point* tmp = new Point[9]; tmp[0] = Point(_p); tmp[1] = Point(_p._x+_h,_p._y,_p._z); tmp[2] = Point(_p._x,_p._y,_p._z+_h); tmp[3] = Point(_p._x+_h,_p._y,_p._z+_h); tmp[4] = Point(_p._x,_p._y+_h,_p._z); tmp[5] = Point(_p._x+_h,_p._y+_h,_p._z); tmp[6] = Point(_p._x,_p._y+_h,_p._z+_h); tmp[7] = Point(_p._x+_h,_p._y+_h,_p._z+_h); tmp[8] = Point(_h,_h,_h); // dummy entry; contains h. return tmp; } string toString() { string str=""; for (int i=0;i<8;i++) { str+= "Point "; str+= i; str+= "is "; str+= getPoints()[i].toString(); } str += "hight is "; str += _h; str += "\n"; return str; }private: Point _p; float _h;};class OcNode{public: typedef vector<OcNode*> NodeLst; Cube _c; bool _full; NodeLst _NodeList; OcNode(Cube c):_c(c),_full(false) { _NodeList = vector<OcNode*>(8); } OcNode(Point p,float h):_full(false) { OcNode(Cube(p,h)); } void addNode(OcNode* node); bool isEmpty(){return !_full;} bool isFukk(){return _full;} void setEmpty(){_full = false;} void setFull(){_full = true;} void Split8Ways(Cube c); string toString() { string f =_full?"is full\n":"is empty\n"; string thisCube = _c.toString(); string nodes[8]={"","","","","","","",""}; for (int i=7;i>=0;i--) { nodes[i]+="Node "; nodes[i]+=i; nodes[i]+="is "; if (_NodeList.at(i)== NULL) nodes[i]+="empty \n"; else nodes[i] += _NodeList.at(i)->toString(); } string str = ""; str +="the cobe "; str += thisCube; str+= f; for (int i=0;i<8;i++) str+= nodes[i]; return str; }};#endifוהשימוש אמור להית משהו כמו#include "OcTree.h"void OcNode::addNode(OcNode* node){ _NodeList.push_back(node);}void OcNode::Split8Ways(Cube c){ for (int i=0;i<8;i++) addNode(&OcNode(Cube(c.getPoints()[i],c.getPoints()[8]._x)));}int main(){ int i; Point a = Point(1,1,1); Cube b = Cube(a,3); OcNode c = OcNode(b); cout << c.toString() <<endl; cin >> i;}הקוד לא עובד נכון, כי המרה מ float ל string לא נכונה. איך עושים את ההמרה ?הצעות נוספות יתקבלו בברכה.הקטע הרלונטי :string toString() { string str=""; str+=" x = "; str+=_x; str+=" y = "; str+=_y; str+=" z = "; str+=_z; str+="\n"; return str; }אשר נמצא ב Point .כבר שאני פה, יש פה מישהו שמכיר אולי מימוש טוב ל OcTree בC++./Java ?מטי.
פורסם 2006 בפברואר 2219 שנים אני הייתי ממליץ לך להשתמש בטיפוס בשם stringstream שנמצא בספריה <sstream>השימוש בו הוא משהו כזה:#include <sstream>#include <string>float x,y,z;stringstream strsTemp;strsTemp << "X is: " << x << "Y is: " << y << "Z is: " << z;string sMyString = strsTemp.str();זה ממש נוח. אגב, אפשרות אחרת היא להשתמש ב-sprintf - אתה משתמש בזה כמו ב-printf אבל אתה נותן לו STRING שאליו הוא מוציא את הפלט. כמובן, האפשרות הזאת היא בעצם C ולא C++...
ארכיון
דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.