Why does it have to be so complicated. |
Programming in C++ and C and other such languages demand a more precise way of thinking, and understanding what you're creating. Let's look at the first error:
1 2
|
redlib/Composer.cpp:17:23: error: cannot convert 'Scene' to 'Scene*' in initialization
Scene* scn = *itr;
|
What kind of object is
scn ? It is a pointer-to-scene.
What kind of object is
*itr?
itr is an iterator, and the * operator applied to it gives you the object it is associated with in the container, and in this case that object is a Scene object. So *itr is a Scene object.
So you're trying to make a pointer (which is a single number, representing a memory location) the same as a Scene object, which is a big class. Makes no sense, hence the error. Also, very very simple. You just have to understand what kind of objects you have.
For the next one, seeing your scene.h file would be helpful.
You might also find understanding how code is turned into binary in C++ helpful;
https://www.daniweb.com/programming/software-development/tutorials/466177/understanding-c-from-source-to-binaries
I'm seeing a lot of unnecessary use of
this->
, and I'm seeing a lot of
delete without much sign of corresponding
new. Managing your own memory in such a manual fashion is to be avoided; I'm not convinced it's necessary here. In fact, it looks like you're just plain using it incorrectly.
delete means "this object I allocated with
new; please run the object's destructor and I don't care what you do with the memory after that". If you are applying delete to an object that wasn't created with
new, it's a mistake.
Your headers look pretty messed up.
string.h is for C-style strings.
<string> is for C++. I see classes trying to use
std::cout that don't include <iostream>.
stdio.h is a legacy C header providing functions you're not using.
*this->lastScene->hide();
I've got no idea what's going on here.
-> allows you to dereference a pointer and then call a function on it, but you're dereferencing the pointer yourself and then applying -> anyway.
*this->lastScene->hide();
should be
lastScene->hide();
, on the assumption that
lastScene is a pointer. Is it?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#include "Scene.h"
Scene::Scene(std::string sceneName){
this->name = sceneName;
Composer::addToScenes(this);
}
Scene::~Scene(){
delete name;
}
std::string Scene::getName(){
if (this->name != NULL){
return this->name;
}
}
|
This looks very wrong. In the constructor,
name is implied to be an object of type std::string. But in the function getName, you're checking if it's NULL. You're trying to check if it's a NULL pointer. Is it a pointer or is it a string? As it is, NULL is old-school code that causes problems in situations like this, because it's sometimes just 0. Do yourself a favour; use nullptr for null pointers. Then, in the destructor, you're trying to delete it, so it must be a pointer, but when was it created with new? This all suggests you're using pointers,
new and
delete without understanding what they are and what they do.