• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
black_darkness

Can you create a Vector of structs in C++? I need help with a unknown error pertaining to this subject.

22 posts in this topic

I am painting 64 squares onto a screen using Fast Light ToolKit. In order to save time I created a loop to do this automatically. Since I need to dynamically create new objects I decided to push the struct Rectangle into a vector. However when I run the program I get a single error.
[CODE]error C2248: 'Graph_lib::Shape::Shape' : cannot access private member declared in class 'Graph_lib::Shape'[/CODE]


Here is the main loop
[CODE]
int main()
{
Point tl(100,100); //top left corner of window
Simple_window win(tl,400,400,"My Window");
vector<Rectanglee> rects; //Here is where I think the problem lies.

for (int i=0;i<8;i++){
int xa=(i*50)-1;
for(int j=0;j<8;j++){
int ya=(j*50)-1;
Rectanglee rect(Point(i,j),50,50);
rects.push_back(rect);
}
}
for(int i=0;i<64;i++){
win.attach(rects[i]); //attach rect to screen
}
win.wait_for_button(); //wait until user clicks the next button to close window
}
[/CODE]



Here is the rectanglee struct.
[CODE]
struct Rectanglee : Shape {
Rectanglee(Point xy, int ww, int hh) : w(ww), h(hh)
{
add(xy);
if (h<=0 || w<=0) error("Bad rectangle: non-positive side");
}
Rectanglee(Point x, Point y) : w(y.x-x.x), h(y.y-x.y)
{
add(x);
if (h<=0 || w<=0) error("Bad rectangle: non-positive width or height");
}
void draw_lines() const;
int height() const { return h; }
int width() const { return w; }
private:
int h; // height
int w; // width
};
[/CODE]
0

Share this post


Link to post
Share on other sites
[quote name='Hodgman' timestamp='1355282192' post='5009672']
That error says that the [font=courier new,courier,monospace]Shape[/font] constructor is private. Can you post the code for [font=courier new,courier,monospace]Shape[/font]?
[/quote]

Yes.

Here are the headers I am using http://www.stroustrup.com/Programming/Graphics/ . The shape is in the graph.h file I think.


Anyways here is the Shape class.
[CODE]
class Shape { // deals with color and style, and holds sequence of lines
public:
void draw() const; // deal with color and draw lines
virtual void move(int dx, int dy); // move the shape +=dx and +=dy
void set_color(Color col) { lcolor = col; }
Color color() const { return lcolor; }
void set_style(Line_style sty) { ls = sty; }
Line_style style() const { return ls; }
void set_fill_color(Color col) { fcolor = col; }
Color fill_color() const { return fcolor; }
Point point(int i) const { return points[i]; } // read only access to points
int number_of_points() const { return int(points.size()); }
virtual ~Shape() { }
protected:
Shape();
virtual void draw_lines() const; // draw the appropriate lines
void add(Point p); // add p to points
void set_point(int i,Point p); // points[i]=p;
private:
vector<Point> points; // not used by all shapes
Color lcolor; // color for lines and characters
Line_style ls;
Color fcolor; // fill color
Shape(const Shape&); // prevent copying
Shape& operator=(const Shape&);
};
[/CODE] Edited by black_darkness
0

Share this post


Link to post
Share on other sites
May i suggest (read the comments, i changed them:

[source lang="cpp"]int main() {
Point tl(100,100);
Simple_window win(tl,400,400,"My Window");
vector<Rectanglee*> rects; // Pointer Magic!

for (int i=0;i<8;i++){
int xa=(i*50)-1;
for(int j=0;j<8;j++){
int ya=(j*50)-1;
rects.push_back(new Point(i,j),50,50);
}
}
for(int i=0;i<64;i++){
win.attach(*(rects[i])); //Remember to de-reference
}
win.wait_for_button(); //wait until user clicks the next button to close window

// Clean up at some point!
for (unsigned int i = 0, size = rects.size(); i < size; ++i)
delete rects[i];
}[/source]
2

Share this post


Link to post
Share on other sites
vector <int*> names; works, so then wouldn't you have a vector of structs or classes if the int* referred to the addresses in memory of different data structs?
0

Share this post


Link to post
Share on other sites
CatmanFS: Not quite sure, what you mean, but if you want to have a vector<int*>'s element point to instances of anything different than ints, you will have to use an explicit cast. This will work maybe, but is more or less one of the most evil things to do.

Also, as Cornstalks already said, using raw pointers instead of smart pointer creates a lot of potential problems later on.

Edit: you ninja, BitMaster Edited by rnlf
2

Share this post


Link to post
Share on other sites
[quote name='uglybdavis' timestamp='1355283813' post='5009685']
May i suggest (read the comments, i changed them:

[source lang="cpp"]int main() {
Point tl(100,100);
Simple_window win(tl,400,400,"My Window");
vector<Rectanglee*> rects; // Pointer Magic!

for (int i=0;i<8;i++){
int xa=(i*50)-1;
for(int j=0;j<8;j++){
int ya=(j*50)-1;
rects.push_back(new Point(i,j),50,50);
}
}
for(int i=0;i<64;i++){
win.attach(*(rects[i])); //Remember to de-reference
}
win.wait_for_button(); //wait until user clicks the next button to close window

// Clean up at some point!
for (unsigned int i = 0, size = rects.size(); i < size; ++i)
delete rects[i];
}[/source]
[/quote]

I read this article [url="http://www.cplusplus.com/doc/tutorial/pointers/"]http://www.cplusplus...orial/pointers/[/url] . And from what I understand making rects a pointer would make it incapable of using push_back which is the whole reason I am using a vector in the first place.

I suppose if I did something like this. It might work.

[CODE]
for (int i=0;i<8;i++){
int xa=(i*50)-1;
for(int j=0;j<8;j++){
int ya=(j*50)-1;
(*rects).push_back(new Point(i,j),50,50);
}
}
[/CODE] Edited by black_darkness
0

Share this post


Link to post
Share on other sites
Don't make rects a pointer, make it a vector containing pointers. Preferably smart pointers, but regular pointers would also work. Either way it's legal to store both regular and smart pointers inside a vector, including using push_back().
0

Share this post


Link to post
Share on other sites
[quote name='SiCrane' timestamp='1355333123' post='5009892']
Don't make rects a pointer, make it a vector containing pointers. Preferably smart pointers, but regular pointers would also work. Either way it's legal to store both regular and smart pointers inside a vector, including using push_back().
[/quote]

So something like this. I have never used smart pointers. But I will look into it.

[CODE]
for (int i=0;i<8;i++){
int xa=(i*50)-1;
for(int j=0;j<8;j++){
int ya=(j*50)-1;
Rectanglee rect(Point(i,j),50,50);
rects.push_back(&rect);
}
}
[/CODE] Edited by black_darkness
0

Share this post


Link to post
Share on other sites
You shouldn't store a pointer to an object on the stack in a vector. Use new to create your object and store that pointer instead.
0

Share this post


Link to post
Share on other sites
[quote name='black_darkness' timestamp='1355333417' post='5009894']
[quote name='SiCrane' timestamp='1355333123' post='5009892']
Don't make rects a pointer, make it a vector containing pointers. Preferably smart pointers, but regular pointers would also work. Either way it's legal to store both regular and smart pointers inside a vector, including using push_back().
[/quote]

So something like this. I have never used smart pointers. But I will look into it.

[CODE]
for (int i=0;i<8;i++){
int xa=(i*50)-1;
for(int j=0;j<8;j++){
int ya=(j*50)-1;
Rectanglee rect(Point(i,j),50,50);
rects.push_back(&rect);
}
}
[/CODE]
[/quote]

that is a crash man. You cannot grab the address of a stack variable like that just as you cannot return a pointer to a variable declared into a function. Once it goes out of scope you'll be pointing at garbage memory.

rects.push_back(new Rectangle(bbla bla bla));
0

Share this post


Link to post
Share on other sites
[quote name='kunos' timestamp='1355333690' post='5009896']
[quote name='black_darkness' timestamp='1355333417' post='5009894']
[quote name='SiCrane' timestamp='1355333123' post='5009892']
Don't make rects a pointer, make it a vector containing pointers. Preferably smart pointers, but regular pointers would also work. Either way it's legal to store both regular and smart pointers inside a vector, including using push_back().
[/quote]

So something like this. I have never used smart pointers. But I will look into it.

[CODE]
for (int i=0;i<8;i++){
int xa=(i*50)-1;
for(int j=0;j<8;j++){
int ya=(j*50)-1;
Rectanglee rect(Point(i,j),50,50);
rects.push_back(&rect);
}
}
[/CODE]
[/quote]

that is a crash man. You cannot grab the address of a stack variable like that just as you cannot return a pointer to a variable declared into a function. Once it goes out of scope you'll be pointing at garbage memory.

rects.push_back(new Rectangle(bbla bla bla));
[/quote]


I tried that, but it says Rectanglee has no default constructor. I can see now that this problem is too advanced for me. I give up now. Thanks for the help.
0

Share this post


Link to post
Share on other sites
[b]@black_darkness[/b]

[source lang="cpp"]vector<SomeData>* pVector; // Pointer to a vector
pVector = new Vector<SomeData(); // Make new vector
pVector->push_back(something);
(*pVector).push_back(something);
delete pVector; // Delete the vectors

vector<SomeData*> Vector; // Vector storing pointers
Vector.push_back(new SoemData()); // Make new data
delete Vector[0]; // Delete the elements

vector<SomeData*>* pVector; // Vector pointer to pointers
pVector = new vector<SomeData*>(); // Make the data structure
pVector->push_back(new SomeData()); // Make new data
(*pVector).push_back(new SomeData()); // Just syntax difference
delete pVector[0]; // Delete elements first
delete pVector[1]; // We added two elements
delete pVector; // Delete the vector[/source]

Seems like you are confused about what a vector is / how memory is managed. Also seems that you don't fully understand what the stack / heap are. The beauty of C++ is that YOU are responsible for the memory. Forget about all the fancy smart pointer work, you use C++ in games for the speed, don't add overhead. Learn how to manage your memory. Seriously, learn memory management it's a very important skill.

Does the code sample above shed some light as to what's going on? Edited by uglybdavis
-1

Share this post


Link to post
Share on other sites
i think you forgot the "bbla bla bla" part [img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img]

There you go:

vector<Rectangle*> rects;

rects.push_back(new Rectangle(Point(i,j),50,50));

This will work.. but I agree with you, it's a good thing for you to step back and get more familiar with C++ .
0

Share this post


Link to post
Share on other sites
[quote name='uglybdavis' timestamp='1355335965' post='5009911']
[b]@black_darkness[/b]

[source lang="cpp"]vector<SomeData>* pVector; // Pointer to a vector
pVector = new Vector<SomeData(); // Make new vector
pVector->push_back(something);
(*pVector).push_back(something);
delete pVector; // Delete the vectors

vector<SomeData*> Vector; // Vector storing pointers
Vector.push_back(new SoemData()); // Make new data
delete Vector[0]; // Delete the elements

vector<SomeData*>* pVector; // Vector pointer to pointers
pVector = new vector<SomeData*>(); // Make the data structure
pVector->push_back(new SomeData()); // Make new data
(*pVector).push_back(new SomeData()); // Just syntax difference
delete pVector[0]; // Delete elements first
delete pVector[1]; // We added two elements
delete pVector; // Delete the vector[/source]

Seems like you are confused about what a vector is / how memory is managed. Also seems that you don't fully understand what the stack / heap are. The beauty of C++ is that YOU are responsible for the memory. Forget about all the fancy smart pointer work, you use C++ in games for the speed, don't add overhead. Learn how to manage your memory. Seriously, learn memory management it's a very important skill.

Does the code sample above shed some light as to what's going on?
[/quote]

At this moment no. That probably has to do with a head-ache I have at the moment. I will come back to this thread and post when the head-ache goes away. Thanks for the help and excuse me for all the trouble.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0