im new to vectors, am i pushing bach the same thing i have created?

Pages: 12
im not sure if the objest i created with class namegenerator is the same one im popping on my vector, by hoping the same name will be the same object...is obviously bad practice but i dont know better yet


the other points i am having issues with are on my code with ///

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <vector>

using namespace std;

class bunny
{
    public:

        bunny (int h = 0): age (h) {}

        int ageing (int h)
        {
            age++;
            return age;
        }

        void setsex (char s)
        {
            sex = s;
        }
        char getsex ()
        {
            return sex;
        }

        bunny  (int x=5, int y=5): posx (x), posy (y) {}

        void setname (string  x)
        {
            name = x;
        }
        string getname ()
        {
            return name;
        }
        int setposx (int x)
        {
            posx=x;
        }
        int setposy (int y)
        {
            posy=y;
        }
        int getposx ()
        {
            return posx;
        }
        int getposy ()
        {
            return posy;
        }
    string name;
    int posx;
    int posy;
    char bunicon;
    char sex;
    int age;
    };



char z = '*';
char x = ' ';
char brdray [10] [10] = {  {z,z,z,z,z,z,z,z,z,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z}, {z,z,z,z,z,z,z,z,z,z} };

int getrand ();
char definesex ();
void getboard ();
string namelist ();
void createobj ();
//////////////////////////////////////////////////////////////////////////////
int main()
{
srand(time(0));
vector <bunny>bunnies;
bunny (namelist()); ///is this bunny im creating here (iwould also like to set the objects name to the setname)
bunnies.push_back (namelist()); ///the same as the one im pushing back
bunnies[0].setsex(definesex());
cout << bunnies[0].getsex()<<endl;
bunnies[0].setname (namelist());///how could i use the namelist to set the name

}

//////////////////////////////////////////////////////////////////////////////
string namelist ()
{
string namearray [18] = {"bon","fur","fluff","foof","bax","tig","uru","ira","esa","afe","ige","ifi","floof","tix","dar","vini","ter", "von"};
///will two objects that end up with the same name be a problem too?
string none = namearray [getrand()%18];
string ntwo =namearray [getrand()*4%18];
string nthree=namearray [getrand()*7%18];
string fullname=none+ntwo+nthree;
return fullname;
}

void getboard ()
{
for (int a = 0;a<10;a++)
 {
     for (int b =0;b<10;b++)
        cout << brdray [a][b];
        cout<<endl;
 }
}

int getrand ()
{
return 1+(rand()%6);
}

int getposx (int x)
{
for (int a = 0;a<10;a++)
 {
     for (int b =0;b<10;b++)

 {
  brdray [a] [b];
  if (brdray[a] [b]);///here is a half started function for finding the position
                      ///it was very flawed, the aim was to check the ///position of other objects in relation to itself but im only going to
///have chars that represent objects on the board (brdray)

}
}
}

char definesex ()
{
    char m = 'm';
    char f = 'f';
    int a;
    int b;
    a=getrand();
    b=getrand();
    if (a<b)
    {
        return 'm';
    }
    else if (a>b)
    {
        return 'f';
    }
}




do you also know if its posible to create a member function that knows where its char symbol (bunnicon or sex will be the symbol on the array board brdray) is in relation to brdray? i dont know how to have the objects themselves represented on da board (brdray)...

lastly i hope im not posting too much im not good with internet etiquet i totaly pissed off the dream in code losers (wayyy to serious) but i would hate lose all access to this site, i also would like to know if my problem was different enough to start a new thread...my issue here was vectors but before it was clas get and set and the other was random name generation...

here is some comedy rap that i enjoyed and hope you do, maybe im putting something back :) http://www.youtube.com/watch?v=zn7-fVtT16k (einstine vrs hawking)
Last edited on
how can i get the name of my object?
SHould i put this question in advanced C++ or is it far far too dumb for even beginners, no ones replying maybe its my advertising...i can go on unless i get help EEP
Why am i getting cool ascii symbols on the board when i run the code??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <vector>

using namespace std;

class bunny
{
    public:

        bunny (int h = 0): age (h) {}

        int ageing (int h)
        {
            age++;
            return age;
        }

        void setsex (char s)
        {
            sex = s;
        }
        char getsex ()
        {
            return sex;
        }

        bunny  (int x=5, int y=5): posx (x), posy (y) {}

        void setname (string  x)
        {
            name = x;
        }
        string getname ()
        {
            return name;
        }
        int setposx (int x)
        {
            posx=x;
        }
        int setposy (int y)
        {
            posy=y;
        }
        int getposx ()
        {
            return posx;
        }
        int getposy ()
        {
            return posy;
        }
    string name;
    int posx;
    int posy;
    char bunicon;
    char sex;
    int age;
    };



char z = '*';
char x = ' ';
char brdray [10] [10] = {  {z,z,z,z,z,z,z,z,z,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z}, {z,z,z,z,z,z,z,z,z,z} };

int getrand ();
char definesex ();
void getboard ();
string namelist ();
void createobj ();
//////////////////////////////////////////////////////////////////////////////
int main()
{
srand(time(0));
vector <bunny>bunnies;
bunny (namelist());
bunnies.push_back (namelist());
bunnies[0].setsex(definesex());
brdray [getrand()][getrand()]=bunnies[0].getsex();

bunny (namelist());
bunnies.push_back (namelist());
bunnies[1].setsex(definesex());
brdray [getrand()][getrand()]=bunnies[1].getsex();

bunny (namelist());
bunnies.push_back (namelist());
bunnies[2].setsex(definesex());
brdray [getrand()][getrand()]=bunnies[2].getsex();

bunny (namelist());
bunnies.push_back (namelist());
bunnies[3].setsex(definesex());
brdray [getrand()][getrand()]=bunnies[3].getsex();

bunny (namelist());
bunnies.push_back (namelist());
bunnies[4].setsex(definesex());
brdray [getrand()][getrand()]=bunnies[4].getsex();

bunny (namelist());
bunnies.push_back (namelist());
bunnies[5].setsex(definesex());
brdray [getrand()][getrand()]=bunnies[5].getsex();

getboard();
}

//////////////////////////////////////////////////////////////////////////////
string namelist ()
{
string namearray [18] = {"bon","fur","fluff","foof","bax","tig","uru","ira","esa","afe","ige","ifi","floof","tix","dar","vini","ter", "von"};

string none = namearray [getrand()%18];
string ntwo =namearray [getrand()*4%18];
string nthree=namearray [getrand()*7%18];
string fullname=none+ntwo+nthree;
return fullname;
}

void getboard ()
{
for (int a = 0;a<10;a++)
 {
     for (int b =0;b<10;b++)
        cout << brdray [a][b];
        cout<<endl;
 }
}

int getrand ()
{
return 1+(rand()%6);
}

int getposx (int x)
{
for (int a = 0;a<10;a++)
 {
     for (int b =0;b<10;b++)

 {
  brdray [a] [b];
  if (brdray[a] [b]);

}
}
}

char definesex ()
{
    char m = 'm';
    char f = 'f';
    int a;
    int b;
    a=getrand();
    b=getrand();
    if (a<b)
    {
        return 'm';
    }
    else if (a>b)
    {
        return 'f';
    }
}


its not like i can get them when i try XD
When you have a problem with code and you want to get help, produce a minimal compilable example that exhibits or illustrates the problem you are having. Do not post everything you've written and say "look for the ///" if you want a high chance of someone helping. In reducing code to a minimal compilable example, you will often discover the reason for your problem.

bunny (namelist()); is a function declaration, and it conflicts with the declaration of the namelist() function you've already declared. Since this should keep the code from compiling, I'm guessing this is not the actual code you're using.
Posting compiler output, is another way of attracting help.
oh okay i will remember that...i can tell you its running fine...just i dont know if its doing what i think its doing, i think im creating an object and giving it a name, popping one of its member variables in a vector...i had a lot of trouble pasing a vector by reference, i gave up for now anyway
An better thing is to learn to use the debugger. If you have an IDE - it should have one built in. Create a watch list of variables, set break points, step through the code 1 line at a time.

That is what IT professionals do, can you imagine them spending all day at work, asking their workmates to help them with their problems?

This is a bit like "Give someone some fish" versus "Teach them how to fish"
What compiler are you using?
codeblox...is really nice of you to help as much as you have, although you need a bit of amunition to think about things for yourself...hence teach him to fish, and you guys are pretty much just teaching me so i will suss out the debugger...but i didnt know how to use the debuggereither so thank you i will mess with it



Codeblox isn't a compiler, but if you downloaded a package with a compiler in it, there's a pretty good chance you're using gcc.

Surprisingly the version of gcc I have installed does compile this code (without so much as a warning.)

Reduced to the minimum:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <vector>

class A{};

void func() {}

int main()
{
    std::vector<A> v ;
    A func() ;

    v.push_back(func()) ;
}


Very odd.
Even more strange... I have the same version of GCC (with CodeBlocks) and it doesn't compile code like that.
wierd i discovered an anomaly,could you advice on the correct way a respectable coder would go about this

in my experience though someone who digs up anomalies doesnt have the ideal type of brain for whatever system theyre involved in ... some poor souls that applies to life in general.

@cire
clang++ has nice error messages:


test.cc:10:11: warning: empty parentheses interpreted as a function declaration
      [-Wvexing-parse]
    A func() ;
          ^~
test.cc:10:11: note: remove parentheses to declare a variable
    A func() ;
          ^~
test.cc:10:7: error: functions that differ only in their return type cannot be
      overloaded
    A func() ;
      ^
test.cc:5:6: note: previous definition is here
void func() {}
     ^
test.cc:12:7: error: no matching member function for call to 'push_back'
    v.push_back(func()) ;
    ~~^~~~~~~~~


Last edited on
@cire does this make me odd? (cos i did it)

but maybe i shouldnt carry on like it, you know teach yurself c++ fine but learning to program sideways?

what should one do to conjure up random objects from the misty blooh?
Last edited on
Even more strange... I have the same version of GCC (with CodeBlocks) and it doesn't compile code like that.


Depending on when you downloaded it, you may not have the same version. I had 4.6.2.1 installed. Just updated to 4.7.0 , and I see no difference. Still compiles/links with no warnings/errors.

clang++ has nice error messages:


Yes, it does.
i tried enough on my own to justify asking for help.. here...

How can i pop a new object created in a for loop onto the vector? i tried to initialize the vector first but..?
it wouldnt let me...any ideas??

1
2
3
4
5
6
7
8
9
10
vector <bunny>bunnies;
for (int aa = 0;aa>5;aa++)
{
bunnies[aa].setname(namelist());
bunnies[aa].setsex(definesex());
bunnies[aa].setposx (getrand());
bunnies[aa].setposy (getrand());
brdray [bunnies[aa].getposx()] [bunnies[aa].getposy()] = bunnies[aa].getsex();

}
1
2
3
4
5
6
7
8
9
    std::vector <bunny>bunnies;
    for (int aa = 0;aa>5;aa++)
   {
        bunnies.push_back(bunny()) ;  //push back a default constructed bunny.
        bunny& workingBunny = bunnies.back() ;   // manipulate the last bunny in the vector
        workingBunny.setname(namelist()) ;
        workingBunny.setsex(definesex()) ;
        // ...
    }
Use the less than operator in the for loop end condition.

for (int aa = 0;aa < 5;aa++)

The for loop ends when the middle part becomes false.
thanks cire for me that was like a goldmine of knowledge, i make that mistake all the time ideas man, i search for other things and try everything and realize i got that the wrong away round
Pages: 12