Reputation: 13
I have written a code for Knapsack problem.The code is working perfectly when i hardcode the inputs but now i want to take the inputs from the user in the array of structures but it is showing error.How can i achieve this? original code:
#include<iostream>
#include<algorithm>
using namespace std;
struct Item{
int value;
int weight;
Item(int value,int weight):value(value),weight(weight){
}
};
bool cmp(struct Item a,struct Item b){
float r1=(float)a.value/a.weight;
float r2=(float)b.value/b.weight;
//cout<<r1<<" "<<r2<<endl;
return r1>r2;
}
void knapsack(int m,int n,struct Item arr[]){
sort(arr,arr+n,cmp);
/* for(int i=0;i<n;i++){ //new sorted array
cout<<arr[i].value<<" "<<arr[i].weight<<" ";
}*/
cout<<endl;
float result[n];
for(int i=0;i<n;i++){
result[i]=0.0;
}
int rem=m;
int j=0;
for(j=0;j<n;j++){
if(arr[j].weight>rem){
break;
}
else{
result[j]=1;
rem=rem-arr[j].weight;
}
}
if(j<n){
result[j]=(float)rem/arr[j].weight;
}
for(int k=0;k<n;k++){
cout<<result[k]<<" ";
}
}
int main(){
struct Item arr[]={{25,18},{24,15},{15,10}};
knapsack(20,3,arr);
return 0;
}
Now i want to take user input in the array of structures but it shows an error "no matching function to call to"
#include<iostream>
#include<algorithm>
using namespace std;
struct Item{
int value;
int weight;
Item(int value,int weight):value(value),weight(weight){
}
};
bool cmp(struct Item a,struct Item b){
float r1=(float)a.value/a.weight;
float r2=(float)b.value/b.weight;
//cout<<r1<<" "<<r2<<endl;
return r1>r2;
}
void knapsack(int m,int n,struct Item arr[]){
sort(arr,arr+n,cmp);
/* for(int i=0;i<n;i++){ //new sorted array
cout<<arr[i].value<<" "<<arr[i].weight<<" ";
}*/
cout<<endl;
float result[n];
for(int i=0;i<n;i++){
result[i]=0.0;
}
int rem=m;
int j=0;
for(j=0;j<n;j++){
if(arr[j].weight>rem){
break;
}
else{
result[j]=1;
rem=rem-arr[j].weight;
}
}
if(j<n){
result[j]=(float)rem/arr[j].weight;
}
for(int k=0;k<n;k++){
cout<<result[k]<<" ";
}
}
int main(){
struct Item arr[10];
int n,m;
cin>>m>>n;
for(int i=0;i<n;i++){
cin>>arr[i].value>>arr[i].weight;
}
knapsack(m,n,arr);
return 0;
}
Upvotes: 1
Views: 89
Reputation: 1033
You have provided parameterized constructor Item(int value,int weight)
, so compiler won't generate default constructor by itself.
You have to define the default constructor explicitly to make the below statement work since it needs default constructor.
struct Item arr[10];
But the below line of code uses the parameterized constructor you have provided.
struct Item arr[]={{25,18},{24,15},{15,10}};
Upvotes: 1
Reputation: 4106
Check below code:
struct Item{
int value;
int weight;
Item(int value,int weight):value(value),weight(weight){
}
};
There is no default constructor. So the below line fails:
struct Item arr[10];
But the following line works:
struct Item arr[]={{25,18},{24,15},{15,10}};
Solution[1]: Provide a default constructor or remove the explicit constructor.
[1] There may be more problems in the code, but I looked into the one you pointed in your question.
Upvotes: 2