Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

C++ Assisstance Please


  • Please log in to reply
2 replies to this topic

#1 MadDawg

MadDawg

  • Members
  • 453 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Houston, TX
  • Local time:12:25 PM

Posted 02 May 2011 - 05:15 PM

ALright, I'm trying to make a checkerboard in FLTK, but I'm having a little bit of trouble; I'm currently hopelessly bad with references and pointers, so I get a Segmentation fault when I try to compile my code. Here's the piece I'm currently stuck on:

/*
*
*
* Class definition here...
*
*
*/
void draw_checkerboard(testWindow& dw) //function causing issues
{
	Vector_ref<Rectangle> chb;
	for (int i = 0; i < 8; ++i)
	{
		for (int j = 0; j < 8; ++j)
		{
			chb.push_back(new Rectangle(Point(i*50,j*50),50,50));
			chb[chb.size()-1].set_fill_color(i*16+j);
			dw.attach(chb[chb.size()-1]);
		}
	}	

}

int main(){

cout << "\nDisplaying window...\n";

testWindow win(Point(100,100),800,600,"check");
draw_checkerboard(win);

return gui_main();
}
The program compiles fine with draw_checkerboard omitted. Any tips? Any help is appreciated.

Also, here's the implementation of Vector_ref:
 //The following is provided by Bjarne Stroustrup
template<class T> class Vector_ref {

    vector<T*> v;

    vector<T*> owned;

public:

    Vector_ref() {}

    Vector_ref(T& a) { push_back(a); }

    Vector_ref(T& a, T& <img src='http://www.bleepingcomputer.com/forums/public/style_emoticons/<#EMO_DIR#>/cool.gif' class='bbc_emoticon' alt='B)' />;

    Vector_ref(T& a, T& b, T& c);

    Vector_ref(T* a, T* b = 0, T* c = 0, T* d = 0)

    {

        if (a) push_back(a);

        if (<img src='http://www.bleepingcomputer.com/forums/public/style_emoticons/<#EMO_DIR#>/cool.gif' class='bbc_emoticon' alt='B)' /> push_back(<img src='http://www.bleepingcomputer.com/forums/public/style_emoticons/<#EMO_DIR#>/cool.gif' class='bbc_emoticon' alt='B)' />;

        if (c) push_back(c);

        if (d) push_back(d);

    }



    ~Vector_ref() { for (int i=0; i<owned.size(); ++i) delete owned[i]; }



    void push_back(T& s) { v.push_back(&s); }

    void push_back(T* p) { v.push_back(p); owned.push_back(p); }



    T& operator[](int i) { return *v[i]; }

    const T& operator[](int i) const { return *v[i]; }



    int size() const { return v.size(); }

};

Edited by MadDawg, 02 May 2011 - 05:53 PM.

A penguin broke my windows with a half-eaten apple!

BC AdBot (Login to Remove)

 


#2 EdKeith

EdKeith

  • Members
  • 4 posts
  • OFFLINE
  •  
  • Local time:12:25 PM

Posted 14 July 2011 - 11:11 AM

Sorry this response is so late in coming, but I just joined up last night.

If you are getting a segfault when compiling the problem is not with your code, but with the compiler (a problem with you code could be exposing the defect in the compiler, but the cause of the segfault is still a defect in the compiler).

What compiler are you using?

-EdK

#3 MadDawg

MadDawg
  • Topic Starter

  • Members
  • 453 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Houston, TX
  • Local time:12:25 PM

Posted 31 July 2011 - 07:55 PM

Sorry about the late replay. I believe the compiler I used for this was g++ 4.4.3.

I haven't looked at this code since shortly after I've posted it, but I remember using a rather messy workaround. It was for an assignment that I was working on, so I don't really care about it anymore. If you want, I'll see if I can dig up the source files and attach them. I could use the information for reference down the road. Thanks.
A penguin broke my windows with a half-eaten apple!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users