Issue with global variable in class - c++

With the following code, it works "correctly." Meaning it creates the foot instance of WalkerJoint.
#include <WalkerJoint.h>
class WalkerLeg {
//WalkerJoint foot;
// if this is uncommented and the below is reversed it fails
public:
WalkerLeg();
void initLeg();
};
WalkerLeg::WalkerLeg(void) {
WalkerJoint foot(2,3,4,5,6);
//foot(2,3,4,5,6);
//if uncommented with above it fails
}
void initLeg() {
Serial.print("leg init\n");
}
void setup() {
Serial.begin(115200);
while (!Serial) {}
// wait for Serial comms to become ready
}
void loop() {
int bom=0;
WalkerLeg frontLeft;
bom=0;
while(bom++<100000){}
exit(0);
}
However, I need foot to be global so other methods can use it. When I move its declaration into the Class definition it gives the following error:
/Users/bin/Documents/Arduino/WalkerLeg.cpp/WalkerLeg.cpp.ino: In constructor 'WalkerLeg::WalkerLeg()':
WalkerLeg.cpp:14: error: no matching function for call to 'WalkerJoint::WalkerJoint()'
WalkerLeg::WalkerLeg(void)
^
/Users/bin/Documents/Arduino/WalkerLeg.cpp/WalkerLeg.cpp.ino:14:26: note: candidates are:
In file included from /Users/bin/Documents/Arduino/WalkerLeg.cpp/WalkerLeg.cpp.ino:1:0:
/Users/bin/Documents/Arduino/libraries/WalkerJoint.cpp/WalkerJoint.h:23:3: note: WalkerJoint::WalkerJoint(int)
WalkerJoint(int); //for using analog inputs
^
/Users/bin/Documents/Arduino/libraries/WalkerJoint.cpp/WalkerJoint.h:23:3: note: candidate expects 1 argument, 0 provided
/Users/bin/Documents/Arduino/libraries/WalkerJoint.cpp/WalkerJoint.h:22:3: note: WalkerJoint::WalkerJoint(int, int, int, int, int)
WalkerJoint(int, int, int, int, int);
^
/Users/bin/Documents/Arduino/libraries/WalkerJoint.cpp/WalkerJoint.h:22:3: note: candidate expects 5 arguments, 0 provided
/Users/bin/Documents/Arduino/libraries/WalkerJoint.cpp/WalkerJoint.h:17:7: note: constexpr WalkerJoint::WalkerJoint(const WalkerJoint&)
class WalkerJoint
^
/Users/bin/Documents/Arduino/libraries/WalkerJoint.cpp/WalkerJoint.h:17:7: note: candidate expects 1 argument, 0 provided
/Users/bin/Documents/Arduino/libraries/WalkerJoint.cpp/WalkerJoint.h:17:7: note: constexpr WalkerJoint::WalkerJoint(WalkerJoint&&)
/Users/bin/Documents/Arduino/libraries/WalkerJoint.cpp/WalkerJoint.h:17:7: note: candidate expects 1 argument, 0 provided
WalkerLeg.cpp:18: error: no match for call to '(WalkerJoint) (int, int, int, int, int)'
foot(2,3,4,5,6);
^
exit status 1
no matching function for call to 'WalkerJoint::WalkerJoint()'

Related

Constructor of virtual genetic class [duplicate]

This question already has answers here:
error: use of deleted function
(6 answers)
Why can templates only be implemented in the header file?
(17 answers)
Closed 3 months ago.
I have this code:
sensor.h:
template<class T>
class Sensor {
public:
uint8_t address;
T data;
virtual void collectData() = 0;
Sensor(uint8_t address);
};
class TemperatureSensor: public Sensor<float> {
void collectData();
};
sensor.cpp:
template<typename T>
Sensor<T>::Sensor(uint8_t address) {
this->address = address;
}
void TemperatureSensor::collectData() {
//some code for collecitng data
}
main function:
TemperatureSensor sensor;
Serial.printf("%d", sensor.address);
Error:
src\sensor.cpp: In function 'void test()':
src\sensor.cpp:11:23: error: use of deleted function 'TemperatureSensor::TemperatureSensor()'
11 | TemperatureSensor sensor;
| ^~~~~~
In file included from src\sensor.cpp:1:
src/sensor.h:14:7: note: 'TemperatureSensor::TemperatureSensor()' is implicitly deleted because the default definition would be ill-formed:
14 | class TemperatureSensor: public Sensor<float> {
| ^~~~~~~~~~~~~~~~~
src/sensor.h:14:7: error: no matching function for call to 'Sensor<float>::Sensor()'
src/sensor.h:11:9: note: candidate: 'Sensor<T>::Sensor(uint8_t) [with T = float; uint8_t = unsigned char]'
11 | Sensor(uint8_t address);
| ^~~~~~
src/sensor.h:11:9: note: candidate expects 1 argument, 0 provided
src/sensor.h:6:7: note: candidate: 'constexpr Sensor<float>::Sensor(const Sensor<float>&)'
6 | class Sensor {
| ^~~~~~
src/sensor.h:6:7: note: candidate expects 1 argument, 0 provided
src/sensor.h:6:7: note: candidate: 'constexpr Sensor<float>::Sensor(Sensor<float>&&)'
src/sensor.h:6:7: note: candidate expects 1 argument, 0 provided
*** [.pio\build\nodemcuv2\src\sensor.cpp.o] Error 1
I want to have multiplte options of same base class(Sensor class) and extend it(I think this is rigth name). I cant create new instance of TemperatureSensor, from error i asume that i need to pass reference of Sensor, but I cant create new Sensor, beacuse it is virtual. Also this is not expected by me behavior. I want to create TemperatureSensor using constructor defined by Sensor ex: TemperatureSensor sensor(0xbeef/*address*/)
I want to create TemperatureSensor using constructor defined by Sensor ex: TemperatureSensor sensor(0xbeef/*address*/)
If you want to use base class constructor directly, you can use using
class TemperatureSensor: public Sensor<float> {
using Sensor::Sensor;
void collectData();
};

Error: no matching function for call to ‘foo::foo()’

I have the following two files
foobar.h
#ifndef FOOBAR_H
#define FOOBAR_H
#include <cstring>
class Foo {
public:
int x;
Foo(int x);
};
class Bar {
public:
char* name;
Foo foo;
Bar(Foo foo);
};
#endif // FOOBAR_H
and foobar.cpp
#include "foobar.h"
Foo::Foo(int x) {
// Do something
}
Bar::Bar(Foo foo) {
// Do something
}
Attempting to compile these with g++ -c foobar.cpp -o foobar.o results in the following error:
foobar.cpp: In constructor ‘Bar::Bar(Foo)’:
foobar.cpp:9:17: error: no matching function for call to ‘Foo::Foo()’
Bar::Bar(Foo foo) {
^
foobar.cpp:5:1: note: candidate: ‘Foo::Foo(int)’
Foo::Foo(int x) {
^~~
foobar.cpp:5:1: note: candidate expects 1 argument, 0 provided
In file included from foobar.cpp:1:
foobar.h:5:7: note: candidate: ‘constexpr Foo::Foo(const Foo&)’
class Foo {
^~~
foobar.h:5:7: note: candidate expects 1 argument, 0 provided
foobar.h:5:7: note: candidate: ‘constexpr Foo::Foo(Foo&&)’
foobar.h:5:7: note: candidate expects 1 argument, 0 provided
As far as I understand the output of g++ is that it requires me to have a default constructor for foo. Why? I wish to pass a foo object to the constructor of bar, why would it need to invoke a default constructor anywhere? I do not want to have a no-args constructor anyways, since I NEED that x has a specific user defined value.
You are trying to default construct a Foo here:
Bar::Bar(Foo foo) {
// the member variable `foo` would have been default constructed here
// Do something
}
But Foo doesn't have a default constructor. One possible solution would be to initialize it in the member initializer list:
Bar::Bar(Foo foo) : foo(std::move(foo)) { // now uses the move constructor instead
// Do something
}

Problems on throwing an object as an Exception: Keeping the same Constructor of parent Exception class

As my first C++ program I decided to make a simple TCP server based on previous knowledge from C and utilizing POSIX socket API:
Therefore, I made the following header file network.h:
#ifndef HTTP_NETWORK
#define HTTP_NETWORK
#include<string>
#include <arpa/inet.h>
//Dummy Value to be changed
#define MAXPENDING 5
class Exception {
public:
Exception(std::string message):message(message){}
std::string getMessage();
private:
std::string message;
};
class NetworkException:public Exception {};
class TCPServer{
public:
TCPServer(int port,std::string address);
~TCPServer();
void listen();
private:
int port;
//Socket file Descriptor
int servSock;
struct sockaddr_in ServAddr;
};
#endif
And afterwards I made and the network.cpp:
#include"network.h"
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include<iostream>
#include<cstring>
#include<string>
std::string Exception::getMessage(){
return this->message;
}
TCPServer::TCPServer(int port,std::string address)
:port(port){
if ((this->servSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
throw NetworkException(std::string("SOCKET Error: could not create basic socket"));
}
memset(&this->ServAddr,0,sizeof(this->ServAddr));
ServAddr.sin_family = AF_INET;
ServAddr.sin_addr.s_addr = inet_addr(address.c_str());
ServAddr.sin_port = htons(port);
if (bind(this->servSock, (struct sockaddr *) &this->ServAddr, sizeof(this->ServAddr)) < 0) {
throw NetworkException(std::string("SOCKET Error: Failed to bind a socket"));
}
if (::listen(this->servSock, MAXPENDING) < 0) {
throw NetworkException(std::string("SOCKET Error: Failed to Listen"));
}
}
void TCPServer::listen(){
struct sockaddr_in ClntAddr; /* Client address */
socklen_t clntLen= (socklen_t)sizeof(ClntAddr);
int clntSock; /* Socket descriptor for client */
//#todo Dummy Logic Depedency Inject Socket Handler
for (;;) {
if ((clntSock = accept(servSock, (struct sockaddr *) &ClntAddr, &clntLen)) < 0) {
std::cout<<"Failed to fetch"<<std::endl;
}
send(clntSock, "12345\n", 6, 0);
std::cout << "Handling client %s\n" << inet_ntoa(ClntAddr.sin_addr) << std::endl;
close(clntSock);
}
}
TCPServer::~TCPServer(){
close(this->servSock);
}
But somehow I have hard time to throw an object as an exception:
./src/socket/network.h:18:7: note: candidate: NetworkException::NetworkException(const NetworkException&)
class NetworkException:public Exception {};
^~~~~~~~~~~~~~~~
./src/socket/network.h:18:7: note: no known conversion for argument 1 from ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘const NetworkException&’
./src/socket/network.h:18:7: note: candidate: NetworkException::NetworkException(NetworkException&&)
./src/socket/network.h:18:7: note: no known conversion for argument 1 from ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘NetworkException&&’
./src/socket/network.cpp:31:84: error: no matching function for call to ‘NetworkException::NetworkException(std::__cxx11::string)’
throw NetworkException(std::string("SOCKET Error: Failed to bind a socket"));
^
In file included from ./src/socket/network.cpp:1:0:
./src/socket/network.h:18:7: note: candidate: NetworkException::NetworkException(const NetworkException&)
class NetworkException:public Exception {};
^~~~~~~~~~~~~~~~
./src/socket/network.h:18:7: note: no known conversion for argument 1 from ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘const NetworkException&’
./src/socket/network.h:18:7: note: candidate: NetworkException::NetworkException(NetworkException&&)
./src/socket/network.h:18:7: note: no known conversion for argument 1 from ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘NetworkException&&’
./src/socket/network.cpp:35:77: error: no matching function for call to ‘NetworkException::NetworkException(std::__cxx11::string)’
throw NetworkException(std::string("SOCKET Error: Failed to Listen"));
^
In file included from ./src/socket/network.cpp:1:0:
./src/socket/network.h:18:7: note: candidate: NetworkException::NetworkException(const NetworkException&)
class NetworkException:public Exception {};
^~~~~~~~~~~~~~~~
./src/socket/network.h:18:7: note: no known conversion for argument 1 from ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘const NetworkException&’
./src/socket/network.h:18:7: note: candidate: NetworkException::NetworkException(NetworkException&&)
./src/socket/network.h:18:7: note: no known conversion for argument 1 from ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘NetworkException&&’
By first issue is that I have hard time to extend the Exception Class into something more Specific. Usually in other languages a generic class has been provided and I could be able to throw it as an exception. In C++ AFAIK any type can be thrownas an error hence I decided to have a class to be thrown.
But for now the plan backfires and I find myself hard to thow an anonymous Object of NetworkException I also tried to mitigate the issue by changing the NetworkException via:
class NetworkException:public Exception {
public:
NetworkException(std::string message){}
};
And still get errors:
In file included from ./src/main.cpp:4:0:
./src/socket/network.h: In constructor ‘NetworkException::NetworkException(std::__cxx11::string)’:
./src/socket/network.h:20:42: error: no matching function for call to ‘Exception::Exception()’
NetworkException(std::string message){}
^
./src/socket/network.h:12:5: note: candidate: Exception::Exception(std::__cxx11::string)
Exception(std::string message):message(message){}
^~~~~~~~~
./src/socket/network.h:12:5: note: candidate expects 1 argument, 0 provided
./src/socket/network.h:10:7: note: candidate: Exception::Exception(const Exception&)
class Exception {
^~~~~~~~~
./src/socket/network.h:10:7: note: candidate expects 1 argument, 0 provided
./src/socket/network.h:10:7: note: candidate: Exception::Exception(Exception&&)
./src/socket/network.h:10:7: note: candidate expects 1 argument, 0 provided
In file included from ./src/socket/network.cpp:1:0:
./src/socket/network.h: In constructor ‘NetworkException::NetworkException(std::__cxx11::string)’:
./src/socket/network.h:20:42: error: no matching function for call to ‘Exception::Exception()’
NetworkException(std::string message){}
^
./src/socket/network.h:12:5: note: candidate: Exception::Exception(std::__cxx11::string)
Exception(std::string message):message(message){}
^~~~~~~~~
./src/socket/network.h:12:5: note: candidate expects 1 argument, 0 provided
./src/socket/network.h:10:7: note: candidate: Exception::Exception(const Exception&)
class Exception {
^~~~~~~~~
./src/socket/network.h:10:7: note: candidate expects 1 argument, 0 provided
./src/socket/network.h:10:7: note: candidate: Exception::Exception(Exception&&)
./src/socket/network.h:10:7: note: candidate expects 1 argument, 0 provided
You need to call the constructor of base class:
class NetworkException:public Exception {
public:
NetworkException(std::string message)
: Exception(message)
{}
};
before the body of ctor of derived class is performed, all data members must be constructed. Exception has only one ctor which takes string, and you have to call it explicitly.

No matching function call call to constructor in header file

I have seen similar questions asked and tried their solutions but the answers to them do not seem to work. I have the following code:
.h
#include <iostream>
#include <vector>
#include <string>
using std::string; using std::vector;
struct DialogueNode;
struct DialogueOption {
string text;
DialogueNode *next_node;
int return_code;
DialogueOption(string t, int rc, DialogueNode * nn) : text{t},
return_code{rc}, next_node{nn} {}
};
struct DialogueNode {
string text;
vector <DialogueOption> dialogue_options;
DialogueNode();
DialogueNode(const string &);
};
struct DialogueTree {
DialogueTree() {}
void init();
void destroyTree();
int performDialogue();
private:
vector <DialogueNode*> dialogue_nodes;
};
.cpp
#include "dialogue_tree.h"
DialogueNode::DialogueNode(const string &t) : text{t} {}
void DialogueTree::init() {
string s = "Hello";
for(int i = 0; i < 5; i++) {
DialogueNode *node = new DialogueNode(s);
dialogue_nodes.push_back(node);
delete node;
}
}
void DialogueTree::destroyTree() {
}
int DialogueTree::performDialogue() {
return 0;
}
int main() {
return 0;
}
I get the error: error: no matching function for call to ‘DialogueNode:: DialogueNode(std::__cxx11::string&)’ DialogueNode *node = new DialogueNode(s);
EDIT additional notes on error
dialogue_tree.h:17:8: note: candidate: DialogueNode::DialogueNode()
dialogue_tree.h:17:8: note: candidate expects 0 arguments, 1 provided
dialogue_tree.h:17:8: note: candidate: DialogueNode::DialogueNode(const DialogueNode&)
dialogue_tree.h:17:8: note: no known conversion for argument 1 from ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘const DialogueNode&’
dialogue_tree.h:17:8: note: candidate: DialogueNode::DialogueNode(DialogueNode&&)
dialogue_tree.h:17:8: note: no known conversion for argument 1 from ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘DialogueNode&&’
Which makes no sense to me because I have the constructor defined to take a string as an argument.
You've declared your constructor as:
DialogueNode(const string);
But defined it as:
DialogueNode(const string &t);
Those two aren't the same; the former takes a const string while the latter takes a const string reference. You'll have to add the & to specify a reference argument:
DialogueNode(const string &);
it is because in the constructor you are specifying that the parameter will be a string of constant type and when creating an object you are passing a string. The type mismatch is the problem, either fix the constructor parameter to string or change when you are creating an object.

c++ multithreading class methods

I have following problem.
vector<thread> vThreads;
list<Crob *> lRobs;
list<Crob *>::iterator i;
for(i = lRobs.begin(); i != lRobs.end(); i++)
{
vThreads.push_back(thread((*i)->findPath));
}
I want to pass the method findPath to a thread, but I just get a lot of errors...
> labrob.cpp: In function ‘int main(int, char**)’:
labrob.cpp:72:43: error: no matching function for call to ‘std::thread::thread(<unresolved overloaded function type>)’
labrob.cpp:72:43: note: candidates are:
In file included from labrob.cpp:14:0:
/usr/include/c++/4.7/thread:131:7: note: std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = int (Crob::*)(); _Args = {}]
/usr/include/c++/4.7/thread:131:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘int (Crob::*&&)()’
/usr/include/c++/4.7/thread:126:5: note: std::thread::thread(std::thread&&)
/usr/include/c++/4.7/thread:126:5: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘std::thread&&’
/usr/include/c++/4.7/thread:122:5: note: std::thread::thread()
/usr/include/c++/4.7/thread:122:5: note: candidate expects 0 arguments, 1 provided
make: *** [labrob.o] Error 1
I have already tried to pass local functions and that worked without problems...
Added CRob header
#pragma once
#include "point.hpp"
#include "lab.hpp"
class Crob
{
protected:
Cpoint *pos;
int steps;
Clab *labfind;
string direction;
public:
Crob(Clab *lab);
virtual ~Crob();
virtual void findPath();
void moveTo(int x, int y);
void moveToPrint(int x, int y);
int getSteps(void);
void checkDirection();
};
Looks like you're trying to pass a non-static method to the std::thread constructor. You cannot do that: a non-static methods needs a object so it can be called. Looks like you want:
for(i = lRobs.begin(); i != lRobs.end(); i++)
{
vThreads.push_back(std::thread(&Crob::findPath, *i));
}