SFML community forums

Help => Graphics => Topic started by: KasHKoW on August 16, 2011, 10:52:46 pm

Title: Class uses RenderWindow but can't use base constructor?
Post by: KasHKoW on August 16, 2011, 10:52:46 pm
I have a class that inherits public properties from sf::RenderWindow. What's wrong with it if it can't use basic constructor's like the 2 argument sf::VideoMode, string one?

Also i'm about to peak at the RenderWindow.cpp source code, but doesn't anyone know if I can just make my own constructor like that and use the variables used in the orig. constructor etc. Basically make a copy... but I still don't know why the first one wouldn't work if i'm using it correctly. Also this is my only error in compiling this one header with three source files.
Title: Re: Class uses RenderWindow but can't use base constructor?
Post by: Nexus on August 16, 2011, 11:12:38 pm
Quote from: "KasHKoW"
What's wrong with it if it can't use basic constructor's like the 2 argument sf::VideoMode, string one?
Could you describe the problem more clearly? What doesn't work exactly? Please provide a short code.

Same for the other problem, I don't understand your description ;)
Title: Class uses RenderWindow but can't use base constructor?
Post by: KasHKoW on August 17, 2011, 12:30:22 am
hpp file
Code: [Select]

class _2deWindow : public sf::RenderWindow {
//couple variables
public:
//couple functions
};


source file
Code: [Select]

_2de::_2deWindow App(sf::VideoMode(800,600,32), "Boundaries Test");


error
Quote

BoundTests.cpp: In function β€˜int main()’:
BoundTests.cpp:8: error: no matching function for call to β€˜_2de::_2deWindow::_2deWindow(sf::VideoMode, const char [16])’
_2de.hpp:51: note: candidates are: _2de::_2deWindow::_2deWindow()
_2de.hpp:45: note:                 _2de::_2deWindow::_2deWindow(const _2de::_2deWindow&)


I have no constructors what so ever. As you can see, which I just noticed, it's noticing it as a function which is weird.
Title: Class uses RenderWindow but can't use base constructor?
Post by: Nexus on August 17, 2011, 12:42:43 am
Constructors are not inherited. You have to write your own, and forward the arguments to the base class via initializer list.

This should be explained in your C++ book...
Title: Class uses RenderWindow but can't use base constructor?
Post by: KasHKoW on August 17, 2011, 02:13:10 am
I haven't read one. I don't really plan on it either.

They kind of turn me off from coding. I want to read effective c++/accelerated c++, but I really haven't got the time. I'm using cprogramming.com && cplusplus.com's tutorials which are pretty nice.
Title: Class uses RenderWindow but can't use base constructor?
Post by: Haikarainen on August 17, 2011, 05:40:24 am
Code: [Select]
class _2deWindow : public sf::RenderWindow {
public:
_2deWindow(sf::VideoMode a, std::string title):sf::RenderWindow(a,title){}
};


Something like that should work
Title: Class uses RenderWindow but can't use base constructor?
Post by: Nexus on August 17, 2011, 09:55:05 am
I think books are really worth the time, you can't learn C++ effectively by internet tutorials. At least I haven't seen one that covers C++ deeply enough and that doesn't neglect important background information.
Title: Class uses RenderWindow but can't use base constructor?
Post by: thePyro_13 on August 17, 2011, 01:58:32 pm
Quote from: "KasHKoW"
I haven't read one. I don't really plan on it either.

They kind of turn me off from coding. I want to read effective c++/accelerated c++, but I really haven't got the time. I'm using cprogramming.com && cplusplus.com's tutorials which are pretty nice.
I'd recommend trying one, I started with Beginning C++ Game Programming, it's essentially just a C++ beginners book disguised as game coding.

I noticed that I improved and learnt at a much faster rate after swapping to books compared to doing online tutorials. At the very least it's easier than alt-tabbing to compare your code with the examples.

But at least you've already found cplusplus.com, I think it's the best online reference available. Keep it handy when dealing with the STL.  :D
Title: Class uses RenderWindow but can't use base constructor?
Post by: Hiura on August 17, 2011, 02:07:10 pm
Quote from: "Nexus"
I think books are really worth the time
Yes, absolutely! Take this advice seriously into consideration. Moreover, AC++ is a very good book (good example, good approach, etc..)

Quote
cplusplus.com, I think it's the best online reference available.
gloups (http://programmers.stackexchange.com/questions/88241/whats-wrong-with-cplusplus-com) (ok, it isn't the point of the discussion but...)
Title: Class uses RenderWindow but can't use base constructor?
Post by: thePyro_13 on August 17, 2011, 02:38:06 pm
Quote from: "Hiura"
gloups (http://programmers.stackexchange.com/questions/88241/whats-wrong-with-cplusplus-com) (ok, it isn't the point of the discussion but...)
Oh wow. Thanks for that, I redact my earlier statement. And now I have to go hunting for a new reference site.
Title: Class uses RenderWindow but can't use base constructor?
Post by: Hiura on August 17, 2011, 04:17:15 pm
I would recommend this one if you're not afraid of raw stuff : http://www.dinkumware.com/manuals/#Standard%20C++%20Library . It's not the easiest, I agree, but it seems complete and correct (I never heard of problem on this reference site to be precise).
Title: Class uses RenderWindow but can't use base constructor?
Post by: KasHKoW on August 17, 2011, 04:23:55 pm
Yeah The books I was reading was Game Programming All in One third edition; Accelerated C++; cplusplus.com C++ Language Tutorial for quick reference. I haven't completely finished any of them yet. The first two not even close lol. I might start looking at them little by little now.


EDIT: I will take a look at that one.
Title: Class uses RenderWindow but can't use base constructor?
Post by: KasHKoW on August 17, 2011, 07:05:03 pm
Quote from: "thePyro_13"
I noticed that I improved and learnt at a much faster rate after swapping to books compared to doing online tutorials. At the very least it's easier than alt-tabbing to compare your code with the examples.

But at least you've already found cplusplus.com, I think it's the best online reference available. Keep it handy when dealing with the STL.  :D


I've learned the fastest by simply using tutorials writing code, reading others code, and doing major refactoring implementing a bunch of new ways that just did the same stuff as the old ways faster and better with conciseness in mind.

also learning a new language, but not even writing in it, can augment your currently known and in use languages a lot.
Title: Class uses RenderWindow but can't use base constructor?
Post by: Nexus on August 17, 2011, 07:24:31 pm
One problem with cplusplus.com is also that it contains no information about C++0x nor the TR1. I have mostly used MSDN to lookup library features of the new C++ standard, although the navigation on the site is not the best one.
Title: Class uses RenderWindow but can't use base constructor?
Post by: KasHKoW on August 18, 2011, 04:03:35 am
Quote from: "Haikarainen"
Code: [Select]
class _2deWindow : public sf::RenderWindow {
public:
_2deWindow(sf::VideoMode a, std::string title):sf::RenderWindow(a,title){}
};


Something like that should work


That's really nice. I thought I understood how it worked but now I'm confused.. The single colon ":sf::RenderWindow(...)...{}" part is confusing. did you mean a comma and to keep it in the first list or arguments or is that a feature I'm not even aware of?
Title: Class uses RenderWindow but can't use base constructor?
Post by: thePyro_13 on August 18, 2011, 05:01:49 am
The single colon indicates the start of the initialiser list. Like so:

Constructorname(parameters) : namespace::baseConstrustorname(baseconstructorParameters)
{
     body
}

the syntax is similar to how you declare an inherited class:
classname : public baseclass

But they only work for constructors, base constructors and member variables.

What you're doing is passing your argument title, to the normal RenderWindow constructor, that way all the data that Renderwindow normally takes care of(which you also inherited) is still initialised properly.

Look up "initialiser lists" for more info, you can do other stuff with them as well, such as initialising variables more efficiently.
Title: Class uses RenderWindow but can't use base constructor?
Post by: KasHKoW on August 20, 2011, 03:12:39 am
Wow, that's really cool. I finally got my own constructor working and it gives me a seg fault. Run it through gdb but at the same time just try that one.