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;
}
Related
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 try to bind a class member function with param as rval to boost::function.
But it doesn't work.
my sample false code :
class Class1
{
int Foo1(int&& b)
{
return b;
}
void foo2()
{
boost::function<int(int&&)> fc(boost::bind(&Class1::Foo1, this, _1)
}
};
Use a lambda expression:
boost::function<int(int&&)> fc = [this](int&& x)
{
return Foo1(x);
};
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 6 years ago.
Improve this question
I am trying to humbly port "FastLED + ESP8266 Web Server" by Jason Coon: https://github.com/jasoncoon/esp8266-fastled-webserver to ESPAsyncWebServer, with a few extra goodies...
My project is located here https://github.com/kelexel/esp8266-fastled-async-webserver-2.0/
My main work on this project is to wrap everything into nice Classes, and try to afford some kind of modularity.
EDIT: As many pointed out in the comments bellow, what I try to accomplish is clearly wrongly illustrated. All I am looking for are ideas, code sample, docs, references, on what should be done, so I can learn and improve...
I am totally new to C++, so please, be indulgent..
What I am trying to accomplish is obtain what I, neophyte, can only explain as a collection of objects, each objects having a String name and method pattern. Said pattern should be a method of the ESPLedDriver.cpp Class
The issue resides in the fact that in ESPLedDriver.h and ESPLedDriver.cpp, I try to create a Struct that references ESPLedDriver::
I made a snippet of the code to illustrate the issue:
ESPLedDriver.cpp
void ESPLedDriver::colorwaves()
{
// ...
}
void ESPLedDriver::palettetest()
{
// ...
}
void ESPLedDriver::setPatterns()
{
// const uint8_t patternCount;
_patterns = {
// ERROR: src/ESPLedDriver.cpp:225:3: error: cannot convert 'ESPLedDriver::colorwaves' from type 'void (ESPLedDriver::)()' to type 'ESPLedDriver::Pattern {aka void (*)()}'
{ colorwaves, "Color Waves" },
// ERROR: src/ESPLedDriver.cpp:225:3: error: cannot convert 'ESPLedDriver::palettetest' from type 'void (ESPLedDriver::)()' to type 'ESPLedDriver::Pattern {aka void (*)()}'
{ this->palettetest, "Palette Test" },
};
}
ESPLedDriver.h
#include "FastLED.h"
class ESPLedDriver
{
public:
ESPLedDriver();
// ...
private:
void setPatterns();
typedef void (*Pattern)();
typedef Pattern PatternList[];
typedef struct {
Pattern pattern;
String name;
} PatternAndName;
typedef PatternAndName PatternAndNameList[];
/* Patterns */
void colorwaves();
void palettetest();
PatternAndNameList _patterns;
uint8_t _patternCount;
}
(Or if you prefer, as a gist:
https://gist.github.com/kelexel/ab5687cf83e376c709e49fbfbcfc100b )
If all of the objects are string and pattern, you can model them as an abstract base class:
class String_Pattern
{
public:
virtual void pattern_method(ESPLedDriver& driver_to_use) = 0;
private:
std::string text;
};
Note that this design requires passing the driver to the string pattern object. Think of this giving the object a driver to use.
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 {
...
};
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...
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to call my SocketConnection constructor from inside the definition of the node class, but I'm failing to understand the compile error I'm getting - I fail to see why the compiler thinks the constructor I declared for SocketConnection is not a constructor.
Here are the main parts of .h file code:
class Node
{
public:
Node() ;
int OnStart() ;
friend class SocketConnection ;
} ;
class SocketConnection
{
public:
Node * m_nptr ;
int m_sockfd ;
SocketConnection(Node * nptr) ;
};
Here are the main parts of .cpp file:
int Node::OnStart()
{
SocketConnection newConnection(this) ;
return 0 ;
}
SocketConnection::SocketConection(Node * nptr): m_nptr(nptr)
{
}
On compilation, I get:
error: ISO C++ forbids declaration of ‘SocketConection’ with no type
error: no ‘int SocketConnection::SocketConection(Node*)’ member function declared in class ‘SocketConnection’
In member function ‘int SocketConnection::SocketConection(Node*)’:
error: only constructors take base initializers
Can someone help me understand this ?
Cheers,
N.
You have a typo:
SocketConnection::SocketConection(Node * nptr): m_nptr(nptr)
// ^
Change it into:
SocketConnection::SocketConnection(Node * nptr): m_nptr(nptr)
// ^^