This question already has an answer here:
Using references to access class objects C++
(1 answer)
Closed 7 years ago.
I have this lines:
class ModulePeople : public Module {
public:
std::list<People> & list_people;
std::list<People>::iterator it;
}
When I try to compile I have this error:
Compiler Error C2758
'ModulePeople': a member of reference type must be initialized
This error appears in this line for example:
it = list_people.begin();
But I don't really know how to inicializate this kind of variable, because i can't do it to NULL. If somebody can help me it would be very grateful.
A reference is an alias for an existing object with storage space somewhere. This means that a reference must reference (no pun intended) an existing object, it's not like a pointer which can has value of nullptr.
This implies that if you use a reference as a member field of a class definition then you must initialize it through any available constructor of such object, eg:
class ModulePeople : public Module {
public:
std::list<People> & list_people;
std::list<People>::iterator it;
ModulePeople(decltype(list_people) list_people) : list_people(list_people) { }
}
If you really want to be able to let it point to nothing then could use a pointer instead, eg std::list<People>*.
Related
This question already has answers here:
Non-const reference bound to temporary, Visual Studio bug?
(2 answers)
Use a temporary as default argument in constructor
(4 answers)
Closed 4 years ago.
I have been looking for a reply for this a lot, but I cannot find nothing. My compiler doesn't give me any error or warning, but maybe there could be any danger into doing this:
class Dog
{
Dog(): x(0) {}
int x;
};
If I have a simple class, creating a function in another class like this:
class PetHouse
{
void addDog(Dog& animal = Dog())
{
// Anything...
}
};
Is the addDog() declaration right? I have an argument which is a reference and it's default value is a Dog() object, instead an existing object.
Is there any danger?
Thanks for reading!
No, it isn't. It should not even compile, because non-const lvalue references do not bind to temporaries like Dog().
As #StoryTeller hints, you are probably using MSVC without /permissive-.
This question already has answers here:
Function pointer to member function
(8 answers)
Closed 4 years ago.
I'm trying to create a function pointer. My code:
Header file:
#pragma once
#include <stdio.h>
class my_class
{
private:
int function(int x);
int *(*foo)(int);
public:
my_class();
~my_class();
};
css file:
#include "my_class.h"
int my_class::function(int x) {
return 1;
}
my_class::my_class() {
foo = &function;
}
my_class::~my_class()
{
}
But the line inside my_class::my_class() gives this error:
error C2276: '&': illegal operation on bound member function expression
Putting the mouse cursor over the = in said line makes the following tooltip appear:
a value of type "int (my_class::*)(int x)" cannot be assigned to an entity of type "int *(*)int"
How can I get it to work?
What you think the compiler will do for you is an implicit type cast from a pointer to a member function to a pointer to a normal function. But this is not what happens.
A pointer to a member function is usually not used, because the instance of an object is what is assigned a certain amount of memory and thereby a pointer to a member function might not exactly point to the function and might lead to erroneous results.
You can fix it in two ways:
Define your member function as a static function. Static member functions are not attached to any particular object and can be used without the scope resolution operator.
Another probable fix you can use is to use the this pointer to create a pointer to a member function.
This question already has answers here:
How come a non-const reference cannot bind to a temporary object?
(11 answers)
Closed 7 years ago.
Let's say I have a struct :
struct structazauras
{
string bla;
}
And some function (in my case this function is actually a constructor of some class but I don't think this is the issue):
void myfunc(structazauras& mystruct) { }
then some where i call myfunc :
..
myfunc(structazauras());
..
I get an error:
no matching function for call to myfunc(structazauras) candidates are myfunc(structazauras&)
If I change the code to :
structazauras tmp;
myfunc(tmp);
It will work fine.
I feel that he (the compiler) has a problem passing a reference to the anonymous instance if structazauras, but why ? the anonymous instance exist on the stack of the calling function.
That is because you cannot bind a temporary to a non-const reference. Mark your reference as const and it will work.
Also, you are using a standard C++ keyword (struct) in the definition
void myfunc(structazauras& struct) { }
Change the name to something else. Or maybe you meant
void myfunc(struct structazauras&) { }
but the additional struct is superfluous in C++.
This question already has answers here:
Reference as class member initialization
(4 answers)
Closed 9 years ago.
I am sorry if this meight be a really simple question but i am new to c++ and working on a simple vocable trainer for understanding c++. (come from java..)
I'd like to pass a const FileManager as reference to my Logic. But i don't get it work. I don't want to have a copy or such.
So i tried it like this: (the main)
FileManager& file = FileManager();
Logic logic = Logic(file);
Inside of the Logic i'd like to store the reference:
class Logic
{
public:
Logic(const FileManager& manager);
~Logic();
private:
const FileManager& m_fileManager;
};
Logic::Logic(const FileManager& manager) :
{
m_fileManager = manager;
}
Thanks
Once the body of a constructor is entered, all member variables have already been initialized. Thereafter, you can only assign to them. This can't work with references - as we know, they need to be initialized when their lifetime begins.
You need to use member initializer list:
Logic::Logic(const FileManager& manager)
: m_fileManager(manager) // m_fileManager is initialized here
{
}
Consider if you really want a reference member. For one, they make your class non-assignable. A smart pointer might be a better choice.
Your example has three flaws. The first, is that you try to initialize a none const reference with a temporary object:
FileManager& file = FileManager();
Most likely you just want to use a FileManager instance here:
FileManager file;
Second, references must be initialized. You achieve this by using the initializer list syntax:
Logic::Logic(const FileManager& manager)
: m_fileManager( manager )
{
}
In addition, the initializing, you use for Logic requires that Logic is assignable. Simply use:
Logic logic(file);
If you have reference members in a class, objects of that class are not assignable by default.
This question already has answers here:
Meaning of 'const' last in a function declaration of a class?
(12 answers)
Closed 9 years ago.
I am learning C++ in Qt environment and I was going through one of the sample code online.
Can anyone please explain this syntax to me?
const TicTacToe * GetTicTacToe() const { return m_tictactoe.get(); }
Why is there a const before the opening bracket of a function? Is it a pointer or multiplication?
the full class is as follows, but the syntax of the instructions mentioned above is not clear to me
class QtTicTacToeWidget : public QWidget
{
Q_OBJECT
public:
explicit QtTicTacToeWidget(QWidget *parent = 0);
const TicTacToe * GetTicTacToe() const { return m_tictactoe.get(); }
void Restart();
The first const is to signify that the variable pointer TicTacToe can't be changed. The second constant after the function declaration says that anything that happens inside this function will not change any member variable inside the class. Because it effectively does not change memory data on the class, it can be used when you use any constant object of that class. For example:
const QtTicTacToeWidget myConstObject;
// Since myConstObject is a constant, I am not allowed to change anything inside
// the class or call any functions that may change its data. A constant function
// is a function that does not change its own data which means I can do this:
myConstObject.GetTicTacToe();
// But I can not do the next statement because the function is not constant
// and therefore may potentially change its own data:
myConstObject.Restart();
The const between before the opening bracket signifies that the function is a const member function. It essentially says that it guarantees to not modify the class and therefore can be called on an object declared as const.
Well that, and it also allows the function to modify mutable variables in a const class.