noexcept while defining a share ptr [duplicate] - c++

This question already has answers here:
Difference in make_shared and normal shared_ptr in C++
(8 answers)
Closed 3 months ago.
I got this error :
error: no matching function for call to 'Company::Company(Company*)'
145 | noexcept(noexcept(::new((void )__p)
| ^~~~~~~~~~~~~~~~~~ 146 | _Up(std::forward<_Args>(__args)...)))
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Test.cpp:20:5: note: candidate: 'Company::Company(int, int)' 20 | Company(int
companyId,int value):companyId(companyId),value(value){
| ^~~~~~~ Test.cpp:20:5: note: candidate expects 2 arguments, 1 provided Test.cpp:9:7: note: candidate: 'constexpr
Company::Company(const Company&)'
9 | class Company{
| ^~~~~~~ Test.cpp:9:7: note: no known conversion for argument 1 from 'Company' to 'const Company&' Test.cpp:9:7: note:
candidate: 'constexpr Company::Company(Company&&)' Test.cpp:9:7: note:
no known conversion for argument 1 from 'Company*' to 'Company&&'
The shared pointer gives me an error, I have no idea what the reason is, any idea please?
Code:
#include <iostream>
#include <string>
#include <memory>
using namespace std;
class Company{
public:
int companyId;
int value;
int HighestSalaryEmployee;
int highestSalary;
int LowestIDHighestSalaryEmployee;
int lowestSalary;
int numEmployees;
public:
Company(int companyId,int value):companyId(companyId),value(value){
HighestSalaryEmployee=11;
highestSalary=0;
LowestIDHighestSalaryEmployee=112;
numEmployees=0;
}
};
int main()
{
shared_ptr<Company> ptr1 = make_shared<Company>(new Company(123456,100));
}

This has nothing to do with noexcept and everything to do with using std::make_shared correctly. The way std::make_shared works is similar to how various emplace functions work: You pass the arguments used to construct the specified type, and it constructs them.
So what the error message is trying to tell you is that it's attempting to construct a Company out of a Company*, and Company doesn't have any constructors that go from a Company* to a Company.
The solution is to drop the new Company(...) and simply pass the arguments directly:
std::shared_ptr<Company> ptr1 = std::make_shared<Company>(123456,100);

Related

How to define an object in the header file and initialize it in the source file

I have an object qp that is defined as
#include <proxsuite/proxqp/dense/dense.hpp>
namespace fr3_ros {
class WaypointController : public controller_interface::MultiInterfaceController<franka_hw::FrankaModelInterface,
hardware_interface::EffortJointInterface,
franka_hw::FrankaStateInterface> {
...
private:
proxsuite::proxqp::isize dim = 14;
proxsuite::proxqp::isize n_eq = 7;
proxsuite::proxqp::isize n_in = 0;
proxsuite::proxqp::dense::QP<double> qp(dim, n_eq, n_in);
...
}
From the documetation here controller_interface::MultiInterfaceController has the constructor
MultiInterfaceController (bool allow_optional_interfaces=false)
Since I did not define a new constructor, that would be the only constructor WaypointController has.
However, if I put this in the header file it would give me an error when compiling:
In file included from /home/bolun/bolun_ws/src/fr3_ros/fr3_ros/src/waypoint_controller.cpp:1:
/home/bolun/bolun_ws/src/fr3_ros/fr3_ros/include/fr3_ros/waypoint_controller.h:113:43: error: ‘dim’ is not a type
113 | proxsuite::proxqp::dense::QP<double> qp(dim, n_eq, n_in);
| ^~~
/home/bolun/bolun_ws/src/fr3_ros/fr3_ros/include/fr3_ros/waypoint_controller.h:113:48: error: ‘n_eq’ is not a type
113 | proxsuite::proxqp::dense::QP<double> qp(dim, n_eq, n_in);
| ^~~~
/home/bolun/bolun_ws/src/fr3_ros/fr3_ros/include/fr3_ros/waypoint_controller.h:113:54: error: ‘n_in’ is not a type
113 | proxsuite::proxqp::dense::QP<double> qp(dim, n_eq, n_in);
| ^~~~
/home/bolun/bolun_ws/src/fr3_ros/fr3_ros/src/waypoint_controller.cpp: In member function ‘virtual void fr3_ros::WaypointController::update(const ros::Time&, const ros::Duration&)’:
/home/bolun/bolun_ws/src/fr3_ros/fr3_ros/src/waypoint_controller.cpp:225:5: error: invalid use of member function ‘proxsuite::proxqp::dense::QP<double> fr3_ros::WaypointController::qp(int, int, int)’ (did you forget the ‘()’ ?)
225 | qp.update(qp_H, qp_g, qp_A, qp_b, std::nullopt, std::nullopt, std::nullopt);
| ^~
/home/bolun/bolun_ws/src/fr3_ros/fr3_ros/src/waypoint_controller.cpp:227:5: error: invalid use of member function ‘proxsuite::proxqp::dense::QP<double> fr3_ros::WaypointController::qp(int, int, int)’ (did you forget the ‘()’ ?)
227 | qp.init(qp_H, qp_g, qp_A, qp_b, std::nullopt, std::nullopt, std::nullopt);
| ^~
/home/bolun/bolun_ws/src/fr3_ros/fr3_ros/src/waypoint_controller.cpp:230:3: error: invalid use of member function ‘proxsuite::proxqp::dense::QP<double> fr3_ros::WaypointController::qp(int, int, int)’ (did you forget the ‘()’ ?)
230 | qp.solve();
| ^~
/home/bolun/bolun_ws/src/fr3_ros/fr3_ros/src/waypoint_controller.cpp:232:13: error: invalid use of member function ‘proxsuite::proxqp::dense::QP<double> fr3_ros::WaypointController::qp(int, int, int)’ (did you forget the ‘()’ ?)
232 | torques = qp.results.x.bottomLeftCorner(7, 1);
| ^~
I also tried separating the declaration and definition, in the header file I have
proxsuite::proxqp::isize dim = 14;
proxsuite::proxqp::isize n_eq = 7;
proxsuite::proxqp::isize n_in = 0;
proxsuite::proxqp::dense::QP<double> qp;
and in the source file I have
qp = proxsuite::proxqp::dense::QP<double>(dim, n_eq, n_in);
this gives me another error
In file included from /opt/ros/noetic/include/class_loader/class_loader_core.hpp:45,
from /opt/ros/noetic/include/class_loader/class_loader.hpp:46,
from /opt/ros/noetic/include/pluginlib/class_list_macros.hpp:40,
from /opt/ros/noetic/include/pluginlib/class_list_macros.h:35,
from /home/bolun/bolun_ws/src/fr3_ros/fr3_ros/src/waypoint_controller.cpp:13:
/opt/ros/noetic/include/class_loader/meta_object.hpp: In instantiation of ‘B* class_loader::impl::MetaObject<C, B>::create() const [with C = fr3_ros::WaypointController; B = controller_interface::ControllerBase]’:
/opt/ros/noetic/include/class_loader/meta_object.hpp:196:7: required from here
/opt/ros/noetic/include/class_loader/meta_object.hpp:198:12: error: use of deleted function ‘fr3_ros::WaypointController::WaypointController()’
198 | return new C;
| ^~~~~
In file included from /home/bolun/bolun_ws/src/fr3_ros/fr3_ros/src/waypoint_controller.cpp:1:
/home/bolun/bolun_ws/src/fr3_ros/fr3_ros/include/fr3_ros/waypoint_controller.h:36:7: note: ‘fr3_ros::WaypointController::WaypointController()’ is implicitly deleted because the default definition would be ill-formed:
36 | class WaypointController : public controller_interface::MultiInterfaceController<franka_hw::FrankaModelInterface,
| ^~~~~~~~~~~~~~~~~~
/home/bolun/bolun_ws/src/fr3_ros/fr3_ros/include/fr3_ros/waypoint_controller.h:36:7: error: no matching function for call to ‘proxsuite::proxqp::dense::QP<double>::QP()’
In file included from /home/bolun/miniconda3/envs/fr3_env/include/proxsuite/proxqp/dense/dense.hpp:8,
from /home/bolun/bolun_ws/src/fr3_ros/fr3_ros/include/fr3_ros/waypoint_controller.h:31,
from /home/bolun/bolun_ws/src/fr3_ros/fr3_ros/src/waypoint_controller.cpp:1:
/home/bolun/miniconda3/envs/fr3_env/include/proxsuite/proxqp/dense/wrapper.hpp:94:3: note: candidate: ‘proxsuite::proxqp::dense::QP<T>::QP(proxsuite::linalg::veg::isize, proxsuite::linalg::veg::isize, proxsuite::linalg::veg::isize) [with T = double; proxsuite::linalg::veg::isize = long int]’
94 | QP(isize _dim, isize _n_eq, isize _n_in)
| ^~
/home/bolun/miniconda3/envs/fr3_env/include/proxsuite/proxqp/dense/wrapper.hpp:94:3: note: candidate expects 3 arguments, 0 provided
/home/bolun/miniconda3/envs/fr3_env/include/proxsuite/proxqp/dense/wrapper.hpp:81:8: note: candidate: ‘proxsuite::proxqp::dense::QP<double>::QP(const proxsuite::proxqp::dense::QP<double>&)’
81 | struct QP
| ^~
/home/bolun/miniconda3/envs/fr3_env/include/proxsuite/proxqp/dense/wrapper.hpp:81:8: note: candidate expects 1 argument, 0 provided
/home/bolun/miniconda3/envs/fr3_env/include/proxsuite/proxqp/dense/wrapper.hpp:81:8: note: candidate: ‘proxsuite::proxqp::dense::QP<double>::QP(proxsuite::proxqp::dense::QP<double>&&)’
/home/bolun/miniconda3/envs/fr3_env/include/proxsuite/proxqp/dense/wrapper.hpp:81:8: note: candidate expects 1 argument, 0 provided
If I add extern in the header file, I get the error
In file included from /home/bolun/bolun_ws/src/fr3_ros/fr3_ros/src/waypoint_controller.cpp:1:
/home/bolun/bolun_ws/src/fr3_ros/fr3_ros/include/fr3_ros/waypoint_controller.h:113:3: error: storage class specified for ‘qp’
113 | extern proxsuite::proxqp::dense::QP<double> qp;
| ^~~~~~
In file included from /opt/ros/noetic/include/class_loader/class_loader_core.hpp:45,
from /opt/ros/noetic/include/class_loader/class_loader.hpp:46,
from /opt/ros/noetic/include/pluginlib/class_list_macros.hpp:40,
from /opt/ros/noetic/include/pluginlib/class_list_macros.h:35,
from /home/bolun/bolun_ws/src/fr3_ros/fr3_ros/src/waypoint_controller.cpp:13:
/opt/ros/noetic/include/class_loader/meta_object.hpp: In instantiation of ‘B* class_loader::impl::MetaObject<C, B>::create() const [with C = fr3_ros::WaypointController; B = controller_interface::ControllerBase]’:
/opt/ros/noetic/include/class_loader/meta_object.hpp:196:7: required from here
/opt/ros/noetic/include/class_loader/meta_object.hpp:198:12: error: use of deleted function ‘fr3_ros::WaypointController::WaypointController()’
198 | return new C;
| ^~~~~
In file included from /home/bolun/bolun_ws/src/fr3_ros/fr3_ros/src/waypoint_controller.cpp:1:
/home/bolun/bolun_ws/src/fr3_ros/fr3_ros/include/fr3_ros/waypoint_controller.h:36:7: note: ‘fr3_ros::WaypointController::WaypointController()’ is implicitly deleted because the default definition would be ill-formed:
36 | class WaypointController : public controller_interface::MultiInterfaceController<franka_hw::FrankaModelInterface,
| ^~~~~~~~~~~~~~~~~~
/home/bolun/bolun_ws/src/fr3_ros/fr3_ros/include/fr3_ros/waypoint_controller.h:36:7: error: no matching function for call to ‘proxsuite::proxqp::dense::QP<double>::QP()’
In file included from /home/bolun/miniconda3/envs/fr3_env/include/proxsuite/proxqp/dense/dense.hpp:8,
from /home/bolun/bolun_ws/src/fr3_ros/fr3_ros/include/fr3_ros/waypoint_controller.h:31,
from /home/bolun/bolun_ws/src/fr3_ros/fr3_ros/src/waypoint_controller.cpp:1:
/home/bolun/miniconda3/envs/fr3_env/include/proxsuite/proxqp/dense/wrapper.hpp:94:3: note: candidate: ‘proxsuite::proxqp::dense::QP<T>::QP(proxsuite::linalg::veg::isize, proxsuite::linalg::veg::isize, proxsuite::linalg::veg::isize) [with T = double; proxsuite::linalg::veg::isize = long int]’
94 | QP(isize _dim, isize _n_eq, isize _n_in)
| ^~
/home/bolun/miniconda3/envs/fr3_env/include/proxsuite/proxqp/dense/wrapper.hpp:94:3: note: candidate expects 3 arguments, 0 provided
/home/bolun/miniconda3/envs/fr3_env/include/proxsuite/proxqp/dense/wrapper.hpp:81:8: note: candidate: ‘proxsuite::proxqp::dense::QP<double>::QP(const proxsuite::proxqp::dense::QP<double>&)’
81 | struct QP
| ^~
/home/bolun/miniconda3/envs/fr3_env/include/proxsuite/proxqp/dense/wrapper.hpp:81:8: note: candidate expects 1 argument, 0 provided
/home/bolun/miniconda3/envs/fr3_env/include/proxsuite/proxqp/dense/wrapper.hpp:81:8: note: candidate: ‘proxsuite::proxqp::dense::QP<double>::QP(proxsuite::proxqp::dense::QP<double>&&)’
/home/bolun/miniconda3/envs/fr3_env/include/proxsuite/proxqp/dense/wrapper.hpp:81:8: note: candidate expects 1 argument, 0 provided
Is it possible for someone to help me understand what is the issue here? Is the a universal issue in C++ or is it just this package?
Thanks in advance!
If I were to intialize qp using a member initialize list, should it look like:
public:
WaypointController()
: dim(14), neq(7), n_in(0), qp(dim, n_eq, n_in)
{
}
The error message you're getting is not particularly helpful here, but let me explain it anyways, so you understand what's going on.
C++ believes that you're trying to declare a function, not define a variable.
proxsuite::proxqp::dense::QP<double> qp(dim, n_eq, n_in);
The return type is proxsuite::proxqp::dense::QP<double>, the function's name is qp, and it takes 3 parameters, of types dim, n_eq, and n_in.
The fact that this is not what you intended in entirely irrelevant.
Knowing this, your error messages make a lot more sense.
The solution to this is to declare the field in the header file, like so:
proxsuite::proxqp::dense::QP<double> qp;
And then to you need to initialize qp in your constructor. Use a member initialization list for this.

To solve ambiguity of overloaded function without either adding typecasting or remove the overloaded functions

I made a class Dummy, which is just a wrapper of primitive type double. The purpose of the class is to test potential problems before I introduce another class that substitutes the double class.
Dummy.h:
class Dummy{
private:
double content;
public:
Dummy(double _content) :content{ _content } {};
operator long int() {return (long int)content;};
}
test.cpp:
#include <math.h>
int main(){
Dummy a = Dummy(1.1);
double aa = fabs(a);
}
It reports:
<source>:17:27: error: call of overloaded 'fabs(Dummy&)' is ambiguous
17 | std::cout << std::fabs(foo);
| ~~~~~~~~~^~~~~ In file included from /usr/include/features.h:461,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/x86_64-linux-gnu/bits/os_defines.h:39,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/x86_64-linux-gnu/bits/c++config.h:586,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/cmath:41,
from <source>:1: /usr/include/x86_64-linux-gnu/bits/mathcalls.h:162:1: note: candidate:
'double fabs(double)'
162 | __MATHCALLX (fabs,, (_Mdouble_ __x),
(__const__));
| ^~~~~~~~~~~ In file included from <source>:1: /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/cmath:241:3:
note: candidate: 'constexpr float std::fabs(float)'
241 |
fabs(float __x)
| ^~~~ /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/cmath:245:3:
note: candidate: 'constexpr long double std::fabs(long double)'
245 | fabs(long double __x)
If I add a type conversion operator:
operator double(){
return content;
}
It somehow only works under Visual Studio, not g++ 9.3.0, which is my target compiler.
During the compiling with g++ 9.3.0, the following errors are reported:
I have read the information here, unfortunately I can't remove the overloaded functions in this case. Neither can I add typecasting, because it doesn't make sense for my purpose.
I also check here, but I am not so familiar with templates. Hence my question is:
Can I eliminate the ambiguity of overloaded function within the class, without adding typecasting, or removing the overloaded functions?
The Dummy class you have shown is only convertible to long int, but fabs() does not accept long int, it accepts either float, double, or long double. Since long int is implicitly convertible to float and double, the call was ambiguous until you added the double conversion operator.
If you don't want to do that, then you have to be explicit about which overload of fabs() you want to call. Either by:
casting the result of the Dummy conversion:
double aa = fabs(static_cast<double>(a));
assigning/casting fabs() to a function pointer of the desired signature:
double (*fabs_ptr)(double) = fabs;
fabs_ptr(a);
using fabs_ptr = double (*)(double);
static_cast<fabs_ptr>(fabs)(a);
Online Demo

C++ programs not compiling

I wrote a simple hello world program:
#include<bits/stdc++.h>
using namespace std;
int main(){
cout<<"Hello";
}
But it is reporting an error:
In file included from c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algobase.h:64,
from c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\char_traits.h:39,
from c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\ios:40,
from c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\istream:38,
from c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\sstream:38,
from c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\complex:45,
from c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\ccomplex:39,
from c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\mingw32\bits\stdc++.h:54,
from AAY.cpp:1:
c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_pair.h:214:11: error: expected unqualified-id before numeric constant
214 | _T1 7
| ^
c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_pair.h: In constructor 'constexpr std::pair<_T1, _T2>::pair()':
c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_pair.h:245:9: error: class 'std::pair<_T1, _T2>' does not have any field named 'first'
245 | : first(), second() { }
and so on.
Basically, it says that std::pair does not have a member named first although I haven't used any. Does anyone have a solution?
You should not use
#include<bits/stdc++.h>
This is not a C++ compliant header file.
Please use #include <iostream> instead and it should be sufficient.
Of course you may have also a problem with your compiler installation.

Compilation error 'is implicitly deleted because the default definition would be ill-formed'

I know that similar questions have been asked already, but I could not find the answer by looking at similar posts. Here is a minimal working example of my problem with the following C++ code:
#include <iostream>
#include <cstdio>
#include <fstream>
using namespace std;
class File{
public:
fstream value;
string name;
unsigned int number_of_lines;
};
void print_filename(File file){
cout << "Name of file is " << file.name << "\n";
}
int main(void){
File file;
print_filename(file);
cout << "\n";
return(0);
}
When I compile, I get the error:
example.cpp: In function ‘int main()’:
example.cpp:28:22: error: use of deleted function ‘File::File(const File&)’
print_filename(file);
^
example.cpp:7:7: note: ‘File::File(const File&)’ is implicitly deleted because the default definition would be ill-formed:
class File{
^~~~
example.cpp:7:7: error: use of deleted function ‘std::basic_fstream<_CharT, _Traits>::basic_fstream(const std::basic_fstream<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits<char>]’
In file included from example.cpp:3:0:
/usr/local/include/c++/7.2.0/fstream:925:7: note: declared here
basic_fstream(const basic_fstream&) = delete;
^~~~~~~~~~~~~
example.cpp:18:6: note: initializing argument 1 of ‘void print_filename(File)’
void print_filename(File file){
^~~~~~~~~~~~~~
Do you know why?
Thank you for your help
Being able to read an error is a valuable skill! Let's do it.
error: use of deleted function ‘File::File(const File&)’
You are calling File's copy constructor, which doesn't exist.
note: ‘File::File(const File&)’ is implicitly deleted
The compiler has implicitly chosen to forbid copy construction of File.
error: use of deleted function ‘basic_fstream(const std::basic_fstream&)
It's because a copy constructor would need fstream's copy constructor, which has been deleted.
note: declared here
basic_fstream(const basic_fstream&) = delete;
^~~~~~~~~~~~~
That's the code that explicitly states that copy construction is not allowed.
note: initializing argument 1 of ‘void print_filename(File)’
void print_filename(File file){
Here is where the problem exists in your code.
The solution, as commented, is to not make a copy. It's not needed.
Pass by reference instead.

Compiling basic program gives ridiculously long output

The code :
#include <bits/stdc++.h>
using namespace std;
int main()
{
cout << "this file works";
return 0;
}
Compiler output was ridiculously long. Some 114000 characters long. This is only a portion of it.
Compiler output on compilation :
C:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/fs_path.h:174:5: note: no known conversion for argument 1 from 'std::basic_string_view<wchar_t>' to 'std::filesystem::__cxx11::path::string_type&&' {aka 'std::__cxx11::basic_string<wchar_t>&&'}
C:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/fs_path.h:167:5: note: candidate: 'std::filesystem::__cxx11::path::path(std::filesystem::__cxx11::path&&)'
path(path&& __p) noexcept
^~~~
C:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/fs_path.h:167:5: note: no known conversion for argument 1 from 'std::basic_string_view<wchar_t>' to 'std::filesystem::__cxx11::path&&'
C:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/fs_path.h:165:5: note: candidate: 'std::filesystem::__cxx11::path::path(const std::filesystem::__cxx11::path&)'
path(const path& __p) = default;
^~~~
C:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/fs_path.h:165:5: note: no known conversion for argument 1 from 'std::basic_string_view<wchar_t>' to 'const std::filesystem::__cxx11::path&'
C:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/fs_path.h:163:5: note: candidate: 'std::filesystem::__cxx11::path::path()'
path() noexcept { }
^~~~
C:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/fs_path.h:163:5: note: candidate expects 0 arguments, 1 provided
The terminal process terminated with exit code: 1
Terminal will be reused by tasks, press any key to close it.
So, what you are seeing is an error message, not an output.
If the file bits/stdc++.h is not a file that you created,
then what you need to do is to replace
#include <bits/stdc++.h>
with
#include <iostream>
and that's gonna solve it for you.