Returning an empty smart pointer [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a class that looks like this,
class A
{
std::shared_ptr<Type> ret;
public:
A()
{
ret=std::shared_ptr<Type>(new Type);
}
std::shared_ptr<Type> GetTypeA(){return ret;}
A (const A&a)
{
....
ret=a.ret;
}
};
class Type
{
A aa;
public:
Type(A*a):aa(*a){}
};
Somewhere in the client code, I call the method GetTypeA like this
void func(A*pA)
{
...
std::shared_ptr<Type> spT=pA->GetTypeA();
...
}
Debugging shows me that spT=empty after the call. But inside pA, ret value is NOT empty.

I notice some mistakes in your code :
A()
{
ret=std::shared_ptr<Type>(new Type);
}
"new Type" means you call default constructor for Type (Type::Type()), and you didn't write it in your sample. Try "new Type(*this)" to use your own constructor.
But to do this you need to change your Type class to:
class Type
{
A* aa; // Use a pointer
public:
Type::Type(A&a) :aa(&a){} // Use references
};
The problem is it's not resolving the "recursive aspect", depend your needs, I would use a static reference to A in the Type class...

Related

Move unique_ptr ownership from one class to another [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to create a unique pointer in one class, class A, then pass on the ownership to another class, class B. Am I ok doing such a thing?
The code below gives me error in getC:
error: call to deleted constructor of 'std::unique_ptr<C>
What am I doing wrong?
class A {
...
void func(shared_ptr<B> Bptr) {
A_pass = make_unique<C>();
Bptr->setPass(move(A_pass));
}
unique_ptr<C> getC()
{
return A_pass;
}
unique_ptr<C> A_pass;
};
class B {
...
void setPass(unique_ptr<C> pass_ptr){
B_pass = move(pass_ptr);
}
unique_ptr<C> B_pass;
}
edit: update the question
Your question does not state where the compiler error occurs, but I would guess it’s the getC() member function:
class A {
...
unique_ptr<C> getC()
{
return A_pass;
}
unique_ptr<C> A_pass;
};
The function as written is attempting to copy A_pass, which of course is not possible for the std::unique_ptr<T> type.
You can rewrite it to explicitly move from the source (I’m not able to test this):
unique_ptr<C> A::getC() {
return A_pass.release();
// alternative: return unique_ptr<C>(std::move(A_pass));
}

Cannot call member function, tried to do it properly still fails [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I implemented a new class (ProtoType) in my header file. Which looks like this:
class ProtoType : public Test
{
public:
uint32_t test();
};
class RealProtoType : public Real
{
public:
uint32_t real();
};
Then in C++ file I made this
uint32_t ProtoType::test()
{
return 5;
}
uint32_t RealProtoType::real()
{
uint32_t holder = ProtoType::test();
}
Then I get this error when compiling
error: cannot call member function ‘uint32_t ProtoType::test()’
without object uint32_t ProtoType::test();
But I still fail, how can I resolve this?
Since ProtoType::test() is a non-static member function you need an object of type ProtoType to call the function upon:
uint32_t RealProtoType::real()
{
ProtoType foo;
uint32_t holder = foo.test();
return 42;
}

memory access violation using std::map as local member [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have a memory access violation error on the line m_Lights=tmp; during the call of the method v_init() in Algo::Init(). Shouldn't map m_Lights be created at the instantiation of m_LightsManager? Why do I have this error?
class LightManager
{
private:
std::map<sint32,Light> m_Lights;
public:
LightManager (void);
~LightManager (void);
void v_init();
};
LightManager ::LightManager (void)
{
}
LightManager ::~LightManager (void)
{
}
void LightManager ::v_init()
{
Light tL;
std::memset(&tL,0,sizeof(Light));
std::map<sint32,Light> tmp;
tmp.insert(std::pair<sint32,Light> (-1,tL));
m_Lights=tmp;
}
class Algo
{
private:
LightsManager m_LightsManager;
....
public:
Algo();
void Init();
};
Algo::Algo()
{
Init();
}
void Algo::Init()
{
m_LightsManager.v_init();
}
If Light is not trivially-copyable then
std::memset(&tL,0,sizeof(Light));
is undefined behavior. This is likely the cause of your error.
In addition to Vittorios answer: Do all the initialization in Lights constructor, instead of relying on an external memset call. And don't use std::memset in C++ to initialize all member variables of an object in one statement, do it explicitely for every variable (usually using a simple assignment; in C++11 you can do that even in the declaration/header, where it IMHO belongs).
Reason: Light may be derived, and the base class(es) define their own data, which you overwrite by recklessly nulling the whole object.
I solved the error, the problem came from a wrong declared pointer in other part of the soft that overlaped with the memory space where the object m_LightsManager where initiated ,

C++ access violation when writing to typdef struct [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a struct defined in a header file. Then I have a singleton class where I am trying to use the struct. When I call ResetVars() from another class I get an access violation when it hits the line that says test.numResponses = "TEST". I am assuming this has something to do with initialization but I haven't been able to solve it. I am new to c++ and I have no idea how to get around this. Thanks for any help.
struct.h
typedef struct POLL_DATA
{
std::string numResponses;
std::string type;
std::string question;
} POLL_DATA;
ControlPolls.h
class ControlPolls
{
private:
static bool instanceFlag;
static ControlExitPolls *controlSingle;
ControlExitPolls();
POLL_DATA test;
public:
static ControlExitPolls* getInstance();
void ResetVars();
};
ControlPolls.cpp
#include "ControlPolls.h"
bool ControlPolls::instanceFlag = false;
ControlPolls* ControlPolls::controlSingle = NULL;
//Private Constructor
ControlExitPolls::ControlExitPolls()
{
};
//Get instance
ControlPolls* ControlPolls::getInstance()
{
if(!instanceFlag)
{
controlSingle = &ControlPolls();
instanceFlag = true;
return controlSingle;
}
else
{
return controlSingle;
}
}
void ControlExitPolls::ResetVars()
{
test.numResponses = "TEST";
}
callingClass.cpp
ControlPolls *controlSingleton;
controlSingleton = ControlPolls::getInstance();
controlSingleton->getInstance()->ResetVars();
You've been struck by C++'s Most Vexing Parse, a compiler rule that says anything that could be a function declaration is a function declaration. The culprit is this line:
POLL_DATA testPoll();
testPoll is treated as the declaration of a function with return type POLL_DATA. Try removing the brackets, or writing simply POLL_DATA testPoll; which implicitly calls the compiler-generated default constructor.
Another larger problem is that testPoll is a member of A, but you've hidden it and declared a local variable in your constructor, A::A(). I suggest you remove the constructor altogether because the implicit constructor will suffice.
Some more notes on your code:
You've declared your class a but refer to it later as A.
You've written an implementation of a constructor for A without declaring it like a proper forward declaration.
Also, typedef struct is not needed in C++. It is sufficient and encouraged to write:
struct POLLDATA {
...
};

C++: accessing private members of the class [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Still getting my head around C++ as I'm new to it, but I'm trying to extend some existing code I've got that is expecting me to make use of the std::vector.
The following is declared in the header (shortened for simplicity):
class WindowManager
{
private:
std::vector<Item*> m_itemlist;
}
My problem is how I'm meant to access this from the .cpp? I'd like to use it to have an array of Item type but I don't understand how to actually get to the point where I can add a newly instantiated Item, let's say button, to the array?
A bit of a rudimentary question but I've not had much luck with tutorials that cover std::vector.
If possible avoid using vector of pointers to Item. Use vector of Item directly.
class WindowManager
{
void addItem(Item const& item) { m_itemlist.push_back(item); }
private:
std::vector<Item> m_itemlist;
};
int main()
{
WindowManager wm;
Item i;
wm.addItem(i);
}
To add an item you could use a member function like this:
class WindowManager
{
private:
std::vector<Item *> m_itemlist;
public:
void addItem(Item *newItem);
}
in window_manager.cpp:
void WindowManager::addItem(Item *newItem)
{
m_itemlist.push_back(newItem);
}
see std::vector::push_back()