Error in Target-sum-subsets-official problem | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Error in Target-sum-subsets-official problem

code--> #include<iostream> using namespace std; void printTargetSumSubsets(int arr[], int idx, string set, int sos, int tar, int n) { if(sos > tar) { return ; } if(idx == n){ if(sos == tar) { cout<<set<<"."<<endl; } return ; } printTargetSumSubsets(arr, idx + 1, set + arr[idx] + ",", sos + arr[idx], tar,n); printTargetSumSubsets(arr, idx + 1, set, sos, tar,n); } int main() { int n;cin>>n; int arr[n]; for(int i = 0; i < n; i++) { cin>>arr[i]; } int tar;cin>>tar; printTargetSumSubsets(arr, 0,"", 0, tar, n); } output---> main.cpp: In function bvoid printTargetSumSubsets(int*, int, std::string, int, int, int)b: main.cpp:19:49: error: no match for boperator+b (operand types are bstd::stringb {aka bstd::__cxx11::basic_string<char>b} and bintb) 19 | printTargetSumSubsets(arr, idx + 1, set + arr[idx] + ",", sos + arr[idx], tar,n); | ~~~ ^ ~~~~~~~~ | | | | | int | std::string {aka std::__cxx11::basic_string<char>} In file included from /usr/local/gcc-9.2.0/include/c++/9.2.0/bits/stl_algobase.h:67, from /usr/local/gcc-9.2.0/include/c++/9.2.0/bits/char_traits.h:39, from /usr/local/gcc-9.2.0/include/c++/9.2.0/ios:40, from /usr/local/gcc-9.2.0/include/c++/9.2.0/ostream:38, from /usr/local/gcc-9.2.0/include/c++/9.2.0/iostream:39, from main.cpp:1: /usr/local/gcc-9.2.0/include/c++/9.2.0/bits/stl_iterator.h:423:5: note: candidate: btemplate<class _Iterator> std::reverse_iterator<_Iterator> std::operator+(typename std::rev

16th Sep 2020, 9:57 AM
ravi kamble
ravi kamble - avatar
2 Answers
+ 1
ravi kamble , It'll be easier to solve you problem if you tell what are you trying to do. What are expected results?
16th Sep 2020, 10:20 AM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 1
``` int n; cin>>n; int arr[n]; ``` this is not valid c++. It may compile on some compilers but it's not a standard way to make a dynamic sized array. use new and delete instead to allocate memory dynamically . or use std::vector https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard
16th Sep 2020, 10:27 AM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar