0

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));
}
Algo
  • 185
  • 6
  • See correct way of using `make_shared`: [Difference in make_shared and normal shared_ptr in C++](https://stackoverflow.com/questions/20895648/difference-in-make-shared-and-normal-shared-ptr-in-c) – Jason Nov 21 '22 at 16:42

1 Answers1

0

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);
Nathan Pierson
  • 5,461
  • 1
  • 12
  • 30