|
|
|
|
![]() ![]() |
Feb 6 2006, 11:36 AM
Post
#1
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 407 Joined: 13-December 04 Member No.: 2,696 |
I'm playing around wtih openGL and trying to get the hang of c/c++(very frustrating at times). I pass a pointer to an array from an object to my main via a get method. If I print out(cout) the pointer(p) and the value pointed to (*p) from main, *p prints the decimal equivilent of the memory address p, not the value that p should be pointing to. Within the object, everything is peachy, its just when I pass the pointer to to main that this happens. The arrays plist[] and vlist[] in meshloader are the ones im having trouble with.
Any ideas? Thanks in advance. Feel free to tell me how bad my code is in general! Code: CODE class MeshLoader { public: MeshLoader(){}; ~MeshLoader(); float * loadMesh(); int getvsize(); int getpsize(); float * getvlist(); int * getplist(); private: int vsize; int psize; float vlist[]; int plist[]; }; Code: CODE #include <iostream> #include <fstream> #include <math.h> #include <stdlib.h> using namespace std; #include "meshloader.h" MeshLoader::~MeshLoader() { } float* MeshLoader::loadMesh() { int i = 0; int j = 0; vsize = 0; psize = 0; vlist[100]; plist[100]; char line[255]; char tag[255]; ifstream fin("xml.x"); if(fin.is_open()) { while(fin.getline(line, 255) != NULL) { strcpy(tag, line); if(strcmp("<model>", strtok(tag, " ")) == 0) { } strcpy(tag, line); if(strcmp("<vlist>", strtok(tag, " ")) == 0) { vsize = atoi(strtok(NULL, " ")); for(i=0;i<vsize;i++) { fin.getline(line, 255); vlist[3*i+0] = (float) atof(strtok(line, " ")); vlist[3*i+1] = (float) atof(strtok(NULL, " ")); vlist[3*i+2] = (float) atof(strtok(NULL, " ")); } } strcpy(tag, line); if(strcmp("<plist>", strtok(tag, " ")) == 0) { psize = (int) atoi(strtok(NULL, " ")); for(i=0;i<psize;i++) { fin.getline(line, 255); plist[3*i+0] = (int) atoi(strtok(line, " ")); plist[3*i+1] = (int) atoi(strtok(NULL, " ")); plist[3*i+2] = (int) atoi(strtok(NULL, " ")); } } } } cout << &plist[0] << ":"; cout << *&plist[0] << ":"; return 0; } int MeshLoader::getvsize() { return vsize; } int MeshLoader::getpsize() { return psize; } float * MeshLoader::getvlist() { return &vlist[0]; } int * MeshLoader::getplist() { cout << &plist[0] << ":"; cout << *&plist[0] << ":"; return &plist[0]; } the relevant part of main: Code: CODE #include <cstdlib> #include <iostream> #include <string> #include <sstream> #include <stdio.h> #include <math.h> #include <time.h> using namespace std; #include <GL/glut.h> #include "meshloader.h" MeshLoader myScene; int * p; float * v; int main(int argc, char **argv) { p = myScene.getplist(); v = myScene.getvlist(); cout << p << ":"; cout << * p << ":"; glutInit(&argc, argv); glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); glutInitWindowPosition(300,100); glutInitWindowSize(640,320); glutCreateWindow("glutton"); glutDisplayFunc(render); glutIdleFunc(render); glutReshapeFunc(reshape); glutMouseFunc(processMouse); glutMotionFunc(processMouseActiveMotion); glutPassiveMotionFunc(Motion); glutEntryFunc(processMouseEntry); glutKeyboardFunc(processNormalKeys); glutSpecialFunc(processSpecialKeys); glEnable(GL_DEPTH_TEST); glutMainLoop(); return 0; } This post has been edited by BuffaloHELP: Feb 8 2006, 08:09 AM |
|
|
|
Feb 8 2006, 07:36 AM
Post
#2
|
|
|
Premium Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 178 Joined: 13-October 04 From: NSW, Australia Member No.: 1,713 |
alot of the time when i start having trouble with C++ i just seek out an alternate route of doing the same thing. When you are using pointers to reference arrays (we'll call ours myPointer), normally *myPointer just returns the first value in the array. On this note, you could try outputting myPointer[0], as that is equivalent to saying *(myPointer + 0).
maybe change the declaration from vlist[] and plist[] to *vlist and *plist respectively and then changing their initialization to vlist = new float[100] and plist = new int[100]. That could help. apart from that, i must congratulate you on how neat your code looks. Good work. Nice and Easy to read. ok good luck mate. |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 6th October 2008 - 06:26 PM |