Hi

Pages: 12
i am a quite new and self tough of c++ and i have the following problem.
i am building a class to use a vector and store some value from a txt file(notepad).
class tile{
public:
vector <int*> tileIDlist;
public:
void pushback();
void acess();
}
so far i manage to make working the push back function:

void tile::pushback(){
char* File;
tileIDlist.clear();

FILE* FileHandle = fopen("C:/wrconv 0.01/bin/Debug/maps/map.txt", "r");

//the map width it is the number of column of the file while the heigh the line

for(int Y = 0;Y < MAP_HEIGHT;Y++) {
for(int X = 0;X < MAP_WIDTH;X++){




fscanf(FileHandle, "%d:%d ", &TileID);


tileIDlist.push_back(&TileID);



}
fscanf(FileHandle, "\n");

}
fclose(FileHandle);

}

so now i want to acess of the element in another function can anyone help me?
my attempt:

void tile::acess() {
int ID = 0;

for( ID = 0;ID < MAP_WIDTH;ID++) int TilesetX = ( tileIDlist.at(ID) % TilesetWidth) * TILE_SIZE;

}
The program receive signall sigill illegal istruction.
thanks anyone
hope in good
I take it you're storing a collection of ints, so the container must be declared as:
 
vector<int> tileIDlist;
and you add to it with:
 
tileIDlist.push_back(TileID);

You can use it like this:
1
2
3
4
5
6
7
void tile::acess()
{
    for (int ID = 0; ID < MAP_WIDTH; ++ID)
    {
        int TilesetX = (tileIDlist[ID] % TilesetWidth) * TILE_SIZE;
    }
}
thanks for the reply tried
FILE* pFile = fopen ("C:/wrconv 0.01/myfile.txt","w");

for (int ID = 0; ID < MAP_WIDTH; ++ID)
{
//int TilesetX = (tileIDlist[ID] % TilesetWidth) * TILE_SIZE;
fprintf (pFile,"%d", tileIDlist[ID] );
}
fclose (pFile);
to see what's going on inside the vector but i have segmentation fault
but the vector seems to be empty the file it is empty
You're obviously accessing beyond the end of the vector. Are you putting anything in it?

The safe way to traverse the vector is:
1
2
3
4
5
for (int ID = 0; ID < tileIDlist.size(); ++ID)
{
//  int TilesetX = (tileIDlist[ID] % TilesetWidth) * TILE_SIZE;
    fprintf (pFile,"%d", tileIDlist[ID] );
}
ok i made easier the problem
class tile {
public:
vector<int> myvector;
public:
void pushback();
void acess();
}
//-------------------------------------------------------------------------
if i do
void tile :: pushback(){
for (int i=1; i<=5; i++) myvector.push_back(i);

FILE* pFile = fopen ("C:/wrconv 0.01/myfile.txt","w");
for (int ID = 0; ID < MAP_WIDTH; ++ID)
{
int TilesetX = (myvector[ID]);
fprintf (pFile, "%d", TilesetX);

}
fclose (pFile);

}

in the txt write 12345-1163005939
//-----------------------------------------------------------------------------------
if i separate
void tile::pushback(){for (int i=1; i<=5; i++) myvector.push_back(i);}
void tile::acess(){ FILE* pFile = fopen ("C:/wrconv 0.01/myfile.txt","w");
for (int ID = 0; ID < MAP_WIDTH; ++ID)
{
int TilesetX = (myvector[ID]);
fprintf (pFile, "%d", TilesetX);

}
fclose (pFile);
}
i have segmentation fault near fprint
What does myvector[ID] mean when ID is larger than the size of the vector?
thanks again for you help and all your answer but i would like you to bare with me once more because it is still not work now i do not have any error but the vector it is empty that's the problem.
separating
void tile::pushback(){for (int i=1; i<=5; i++) myvector.push_back(i);}
void tile::acess(){ FILE* pFile = fopen ("C:/wrconv 0.01/myfile.txt","w");
for (int ID = 0; ID < myvector,size(); ++ID)
{
int TilesetX = (myvector[ID]);
fprintf (pFile, "%d", TilesetX);

}
fclose (pFile);
}
the file it is empty while if i put all in one void tile::pushback{} function it works any idea?
thanks again
You need to understand why the vector is empty.
yes i need to understand why the vector it is empting splitting in two function and it is not if i keep in one but probably i a am just not aloud to split like this?
do not thinks so one you create a vector you shouldbe able to call where ever.
do not know
tryied even now

class tile {
public:
vector<tile> myvector;
int i;
public:
void pushback();
void acess();
}
void tile::load_tile(){

tile temp;


for (temp.i=1; temp.i<=5; temp.i++) myvector.push_back(temp);

FILE* pFile = fopen ("C:/wrconv 0.01/myfile.txt","w");

for (int ID = 0; ID < myvector.size(); ++ID)
{
int TilesetX = (myvector[ID].i);
fprintf (pFile,"%d", TilesetX);

}
fclose (pFile);


}
good if it is in one not if i split :-(
I don't think I understand what you just said.

It would help if you posted the code as you have it, rather than posting extracts. The code you're posting doesn't make much sense and won't compile.

If the code you have doesn't compile, then please post it anyway and say what you're trying to do.
my code it is all posted (this is a small program to learn some teory to apply in something bigger) i have a class tile posted above and then i have the oopurtinity to choose or i hope so between two function void.
that what i would like to do declaring a vector in a class like above and then use two function void one to push back some values in it and one
void to acess to it.
ok what you are missing it is the int main( int argc, char* args[] ) {
tile login;
login.pushback();
login.acess();
}
i am trying to explain even better thanks for the patience the following program give you a txt file with value 1 to 5


class tile {
public:
vector<tile> myvector;// my vector
int i;
public:
void pushback();// first function void
void acess();// second function void
}
void tile::pushback(){

tile temp;


for (temp.i=1; temp.i<=5; temp.i++) myvector.push_back(temp);

FILE* pFile = fopen ("C:/wrconv 0.01/myfile.txt","w");

for (int ID = 0; ID < myvector.size(); ++ID)
{
int TilesetX = (myvector[ID].i);
fprintf (pFile,"%d", TilesetX);

}
fclose (pFile);
}

int main( int argc, char* args[] ) {
tile login;
login.pushback();
login.acess();
}






the following program(same as above but with two function give you a txt file empty because the vector seem somehow empty or the value pushed in


class tile {
public:
vector<tile> myvector;// my vector
int i;
public:
void pushback();// first function void
void acess();// second function void
}
void tile::pushback(){

tile temp;


for (temp.i=1; temp.i<=5; temp.i++) myvector.push_back(temp);


}
void tile::acess(){
FILE* pFile = fopen ("C:/wrconv 0.01/myfile.txt","w");

for (int ID = 0; ID < myvector.size(); ++ID)
{
int TilesetX = (myvector[ID].i);// i am calling the member" i" of my vector so should be 1 2 3 4 5

fprintf (pFile,"%d", TilesetX);

}
fclose (pFile);
}

int main( int argc, char* args[] ) {
tile login;
login.pushback();
login.acess();
}

i want to have something similar to the second program thanks
i can do something like this but i do not like once i create a vector i should be able to call at any point in the program

#include "tile.h"
#include"wrconv 0.01.h"

tile::tile() {


i = 0 ;

}

void tile::load_tile(){




for (i=1; i<=5; i++) myvector.push_back(i);




}


void tile::render_tile(SDL_Surface *screen) {

load_tile();//recall the previous function :-(
FILE* pFile = fopen ("C:/wrconv 0.01/myfile.txt","w");

for (int ID = 0; ID < myvector.size(); ID++)

{
int Tilesetx = (myvector[ID]);
fprintf (pFile,"%d",Tilesetx);

}
fclose (pFile);


}
any other ideas?
thanks much appreciate
The code you're posting has syntax errors, so I can't tell if that's what you're compiling or not. You really need to post your actual code.

i need to understand why the vector it is empting splitting in two function and it is not if i keep in one but probably i a am just not aloud to split like this?
I'll try to explain by adding comments to the code you've posted.
1
2
3
4
5
6
7
8
9
10
11
class tile {
public:  // should be private
    vector<tile> myvector;    // why do you have a vector of tile in class tile?
                              // This is a recursive definition and thus illegal
    int i;
public:
    void pushback();
    void acess();
}   // missing ;

// There's no much point wasting time on the code based on this illegal definition. 


You haven't said what you are trying to do. What is the format of the file? What do you want to do with the content?

You still haven't posted code that compiles, but don't bother just yet. We need to understand what you're trying to do first.
Last edited on
i solve the problem or seem so but doesn t work in the main program there is a mistake somewhereelse i will think.
so goal : reading text file with some values and store in a vector then reading it.
step1:: create a txt file to be read called map with note pad mine it is
1:0 1:0 1:0 1:0 1:0 1:0 1:0 1:0 1:0
1:0 1:0 1:0 1:0 2:0 1:0 1:0 1:0 1:0
1:0 1:0 1:0 1:0 1:0 3:0 1:0 1:0 1:0
step2: create a header file mine i called class ok mine it is :

#ifndef CLASS_H_INCLUDED
#define CLASS_H_INCLUDED

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <vector>
#define ncolumn 9
#define nline 3

using namespace std;


class tile {

public:
vector<tile> myvector;
int a;
int b;

public:
tile();
void pushback();
void acess();


};


#endif // CLASS_H_INCLUDED
no major change if the vector it is private or public

step 3:
create a class cpp program
#include "class.h"


tile::tile() {
a = 0 ;
b = 0 ;

}

void tile::pushback(){



char* File;
myvector.clear();
//FILE* pFile = fopen ("C:/test/myfile.txt","w");
FILE* FileHandle = fopen("C:/test/map.txt", "r");

for(int Y = 0;Y < nline;Y++) {
for(int X = 0;X < ncolumn;X++){

tile temp;

fscanf(FileHandle, "%d:%d ", &temp.a, &temp.b);
//fprintf (pFile, "%d", temp.a);
myvector.push_back(temp);
}
fscanf(FileHandle, "\n");
}
fclose(FileHandle);
//fclose (pFile);


}


void tile::acess() {


FILE* pFile = fopen ("C:/test/test.txt","w");

for (int ID = 0; ID < myvector.size(); ID++)

{
int Tilesetx = (myvector[ID].b);
fprintf (pFile,"%d",Tilesetx);

}
fclose (pFile);


}
step 4another cpp finally the main::

#include "class.h"


int main( int argc, char* args[] ){
tile pippo;
pippo.pushback();
pippo.acess();


}
thanks a lot
Pages: 12