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:
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
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
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
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);
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);
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);
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.
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 :