using vectors in C file

Hi all

I am trying to get the vector's functionality in C ! . But i am getting below error (compiling with g++). I am not able to locate where i am going wrong .. Do in need to use the iterator to use push_back method ??

rdbin.c:47:29: error: no matching function for call to ‘std::vector<mazegen>::push_back(Mymaze*&)

Here is my code.

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <deque>
#include <algorithm>
#include "Maze.h"
#include <string.h>

typedef struct mazegen
{
char rname[50];
int R1,R2,Wno;
char symbol[5];
struct mazegen *room;

}Mymaze;


int main(int argc, char* argv[])
{

unsigned char buff[8];
int w,h; /* w-> columns and h->Rows) */
int RW; /* RW-> Total number of removable walls */

w=atoi(argv[1]);
h=atoi(argv[2]);

FILE *fp=fopen(argv[argc-1],"rb");;
RW=((w-1)*h)+((h-1)*w);
//printf("RW is %d\n",RW);

Mymaze *wallinfo;
std::vector<Mymaze> rwalls;
int k,j,i,count=0;

//std::vector<Mymaze>::iterator wallno;
for(k=0;k<(h-1);k++)
{
for(i=0;i<(w-1);i++)
{
strcpy(wallinfo->symbol,"|");
wallinfo->R1=(k*10)+i;
wallinfo->R2=(k*10)+(i+1);
wallinfo->Wno=count;
rwalls.push_back(wallinfo);
count++;
}
for(j=0;j<w;j++)
{
strcpy(wallinfo->symbol,"-");
wallinfo->R1=(k*10)+j;
wallinfo->R2=((k+1)*10)+j;
wallinfo->Wno=count;
rwalls.push_back(wallinfo);
count++;
}
}
for(i=0;i<w-1;i++)
{
strcpy(wallinfo->symbol,"|");
wallinfo->R1=(k*10)+i;
wallinfo->R2=(k*10)+(i+1);
wallinfo->Wno=count;
rwalls.push_back(wallinfo);
count++;
}


Thanks for any kind of help. !!
1
2
// Mymaze *wallinfo;
Mymaze wallinfo;

And then:
1
2
3
4
5
// ...
// wallinfo->R1=(k*10)+i;
wallinfo.R1=(k*10)+i;
// etc...
rwalls.push_back(wallinfo); 



> compiling with g++

Ergo, C++.
Last edited on
What JLBorges is hinting at, is you have a C file and are trying to do C++ code in it.

Change the file to a .cpp file - and it might compile.

However it is considered bad form to mix C and C++. So use cin & cout instead of scanf & printf, strings instead of array of char (No strcpy function calls), use file streams instead of fopen.

And always use code tags - the <> button on the right So that it looks like JLBorges post above. It makes your code much easier to read, and you could well get more replies.
Thanks for your help ...

However I made this to work using the following..


Mymaze *wallinfo;
std::vector<maze*> rwalls;

the vector type was different I was passing a pointer but the type was not ! . just changed the maze to maze* and it worked...

Afterwards I am having followin problem ...


<char icon[30];
for(k=0;k<(h-1);k++)
{
for(i=0;i<(w-1);i++)
{
int m=(k*10)+i;
strcpy(icon,"|");
strcpy(wallinfo->symbol,icon);

wallinfo->R1=m;

wallinfo->R2=(k*10)+(i+1);

wallinfo->Wno=count;

rwalls.push_back (wallinfo);
count++;

}

for(j=0;j<w;j++)
{
strcpy(wallinfo->symbol,"-");

wallinfo->R1=(k*10)+j;

wallinfo->R2=((k+1)*10)+j;

wallinfo->Wno=count;

rwalls.push_back(wallinfo);
count++;

}



}
for(i=0;i<w-1;i++)
{
strcpy(wallinfo->symbol,"|");

wallinfo->R1=(k*10)+i;

wallinfo->R2=(k*10)+(i+1);

wallinfo->Wno=count;

rwalls.push_back(wallinfo);
count++;

}

std::vector<Mymaze*>::iterator wallno;
Mymaze *temp;
for(wallno=rwalls.begin();wallno<rwalls.end();wallno++)
{
temp=*wallno;
printf("%d %s %d %d \n ",temp->Wno,temp->symbol,temp->R1,temp->R2);
}



While printing the values it is displaying only the last value for the size of vector !.... I think everytime push_back function is overwriting all the previous entries as well .. No Idea how !! .. However I did set breakpoints to check the actual values and they all were correct before push_back .. but on display all messed up .. Following is the sampleoutput...


input before push back
| 0 1 0
| 1 2 1

output after push_back
| 1 2 1
| 1 2 1

input before push back
- 0 10 2
- 1 11 3


output after push_back
4 - 2 12
4 - 2 12
4 - 2 12
4 - 2 12
4 - 2 12

| 10 11 5
| 11 12 6

6 | 11 12


- 10 20 7
- 11 21 8
- 12 22 9


9 - 12 22
9 - 12 22
9 - 12 22
9 - 12 22
9 - 12 22
9 - 12 22
9 - 12 22
9 - 12 22
9 - 12 22
9 - 12 22

>
Any Idea please help...

THANKS AGAIN ....
I got it ...

I was copying the same buffer everytime .. "wallinfo "

This has been corrected now .. by allocating the new buffer as follow.

for(i=0;i<(w-1);i++)
{
[ Mymaze *wallinfo=(Mymaze*)malloc(sizeof(Mymaze)); ]
int m=(k*10)+i;
strcpy(icon,"|");
strcpy(wallinfo->symbol,icon);
wallinfo->R1=m;
wallinfo->R2=(k*10)+(i+1);
wallinfo->Wno=count;
rwalls.push_back (wallinfo);
count++;
}
for(j=0;j<w;j++)
{
[ Mymaze *wallinfo=(Mymaze*)malloc(sizeof(Mymaze)); ]
strcpy(wallinfo->symbol,"-");
wallinfo->R1=(k*10)+j;
wallinfo->R2=((k+1)*10)+j;
wallinfo->Wno=count;
rwalls.push_back (wallinfo);
count++;

..
Topic archived. No new replies allowed.