Need urgent help with C++ solution

I am trying to solve this problem in C++. JAVA code is given in the below link, but I don't understand JAVA.
Need to store and check if total of any 3 elements added is the provided total.

https://stackoverflow.com/questions/51346110/data-structure-that-can-efficiently-store-and-check-if-the-total-of-any-three-su

Below is my code, so far.
Need your inputs here. I am stuck with below code compile errors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>

class Solution
{

public:
	std::vector<int> i = std::vector<int>();
	std::vector<int> sum = std::vector<int>();
	int add = 0;
	virtual void append(std::vector<int> &list);
	virtual bool has(int total);
	static void main(std::vector<std::wstring> &args);
};

void Solution::append(std::vector<int> &list)
{

	for (int l = 0;l < list.size();l++)
	{
		if (i.size() > 0 && l <= i.size() - 1)
		{
			add -= i.erase(i.begin() + l);
		}
		i.push_back(list[l]);
		add += list[l];
	}
	sum.push_back(add);
}

bool Solution::has(int total)
{
	if (sum.empty())
	{
		return false;
	}
	else
	{
		return std::find(sum.begin(), sum.end(), total) != sum.end();
	}
}

void Solution::main(std::vector<std::wstring> &args)
{
	Solution *solution = new Solution();

	solution->append(std::vector<int> {1, 2, 3});

	std::wcout << solution->has(6) << std::endl;
	std::wcout << solution->has(9) << std::endl;

	solution->append(std::vector<int> {4});

	std::wcout << solution->has(9) << std::endl;

	delete solution;
}

int main()
{
	std::vector<std::wstring> args(argv + 1, argv + argc);
	Solution::main(args);
	
}


OUTPUT

 In member function 'virtual void Solution::append(std::vector<int>&)':
21:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
23:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
25:8: error: no match for 'operator-=' (operand types are 'int' and 'std::vector<int>::iterator {aka __gnu_cxx::__normal_iterator<int*, std::vector<int> >}')
 In static member function 'static void Solution::main(std::vector<std::basic_string<wchar_t> >&)':
49:45: error: no matching function for call to 'Solution::append(std::vector<int>)'
49:45: note: candidate is:
18:6: note: virtual void Solution::append(std::vector<int>&)
18:6: note:   no known conversion for argument 1 from 'std::vector<int>' to 'std::vector<int>&'
54:39: error: no matching function for call to 'Solution::append(std::vector<int>)'
54:39: note: candidate is:
18:6: note: virtual void Solution::append(std::vector<int>&)
18:6: note:   no known conversion for argument 1 from 'std::vector<int>' to 'std::vector<int>&'
58:9: warning: deleting object of polymorphic class type 'Solution' which has non-virtual destructor might cause undefined behaviour [-Wdelete-non-virtual-dtor]
 In function 'int main()':
63:33: error: 'argv' was not declared in this scope
63:50: error: 'argc' was not declared in this scope
Last edited on
Do you realize that, unlike Java, C++ doesn't require everything to be inside some class? In fact in most cases having everything inside a class is usually not the best fit for C++ programs.

Below is my code, so far. Please note I haven't compiled it yet.

Why not? Compiling code in C++ should be done early and often to make finding mistakes easier.

I am stuck with below code.

The first thing you need to do is compile the code, fix any compile errors and then thoroughly describe the problems you find with your code. It would also be helpful if you were to describe the purpose of the code.

I have modified and compiled code and facing lot of errors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>

class Solution
{

public:
	std::vector<int> i = std::vector<int>();
	std::vector<int> sum = std::vector<int>();
	int add = 0;
	virtual void append(std::vector<int> &list);
	virtual bool has(int total);
	static void main(std::vector<std::wstring> &args);
};

void Solution::append(std::vector<int> &list)
{

	for (int l = 0;l < list.size();l++)
	{
		if (i.size() > 0 && l <= i.size() - 1)
		{
			add -= i.erase(i.begin() + l);
		}
		i.push_back(list[l]);
		add += list[l];
	}
	sum.push_back(add);
}

bool Solution::has(int total)
{
	if (sum.empty())
	{
		return false;
	}
	else
	{
		return std::find(sum.begin(), sum.end(), total) != sum.end();
	}
}

void Solution::main(std::vector<std::wstring> &args)
{
	Solution *solution = new Solution();

	solution->append(std::vector<int> {1, 2, 3});

	std::wcout << solution->has(6) << std::endl;
	std::wcout << solution->has(9) << std::endl;

	solution->append(std::vector<int> {4});

	std::wcout << solution->has(9) << std::endl;

	delete solution;
}

int main(int argc, char **argv)
{
	std::vector<std::wstring> args(argv + 1, argv + argc);
	Solution::main(args);
	
}


Last edited on
Below are list of errors seen with above modified code.

In member function 'virtual void Solution::append(std::vector<int>&)':
21:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
23:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
25:8: error: no match for 'operator-=' (operand types are 'int' and 'std::vector<int>::iterator {aka __gnu_cxx::__normal_iterator<int*, std::vector<int> >}')
In static member function 'static void Solution::main(std::vector<std::basic_string<wchar_t> >&)':
49:45: error: no matching function for call to 'Solution::append(std::vector<int>)'
49:45: note: candidate is:
18:6: note: virtual void Solution::append(std::vector<int>&)
18:6: note: no known conversion for argument 1 from 'std::vector<int>' to 'std::vector<int>&'
54:39: error: no matching function for call to 'Solution::append(std::vector<int>)'
54:39: note: candidate is:
18:6: note: virtual void Solution::append(std::vector<int>&)
18:6: note: no known conversion for argument 1 from 'std::vector<int>' to 'std::vector<int>&'
58:9: warning: deleting object of polymorphic class type 'Solution' which has non-virtual destructor might cause undefined behaviour [-Wdelete-non-virtual-dtor]
In file included from /usr/include/c++/4.9/vector:62:0,
from 2:
/usr/include/c++/4.9/bits/stl_construct.h: In instantiation of 'void std::_Construct(_T1*, _Args&& ...) [with _T1 = std::basic_string<wchar_t>; _Args = {char*&}]':
/usr/include/c++/4.9/bits/stl_uninitialized.h:75:53: required from 'static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = char**; _ForwardIterator = std::basic_string<wchar_t>*; bool _TrivialValueTypes = false]'
/usr/include/c++/4.9/bits/stl_uninitialized.h:125:41: required from '_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = char**; _ForwardIterator = std::basic_string<wchar_t>*]'
/usr/include/c++/4.9/bits/stl_uninitialized.h:278:63: required from '_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = char**; _ForwardIterator = std::basic_string<wchar_t>*; _Tp = std::basic_string<wchar_t>]'
/usr/include/c++/4.9/bits/stl_vector.h:1292:27: required from 'void std::vector<_Tp, _Alloc>::_M_range_initialize(_ForwardIterator, _ForwardIterator, std::forward_iterator_tag) [with _ForwardIterator = char**; _Tp = std::basic_string<wchar_t>; _Alloc = std::allocator<std::basic_string<wchar_t> >]'
/usr/include/c++/4.9/bits/stl_vector.h:1263:56: required from 'void std::vector<_Tp, _Alloc>::_M_initialize_dispatch(_InputIterator, _InputIterator, std::__false_type) [with _InputIterator = char**; _Tp = std::basic_string<wchar_t>; _Alloc = std::allocator<std::basic_string<wchar_t> >]'
/usr/include/c++/4.9/bits/stl_vector.h:404:65: required from 'std::vector<_Tp, _Alloc>::vector(_InputIterator, _InputIterator, const allocator_type&) [with _InputIterator = char**; <template-parameter-2-2> = void; _Tp = std::basic_string<wchar_t>; _Alloc = std::allocator<std::basic_string<wchar_t> >; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<std::basic_string<wchar_t> >]'
63:54: required from here
/usr/include/c++/4.9/bits/stl_construct.h:75:7: error: no matching function for call to 'std::basic_string<wchar_t>::basic_string(char*&)'
{ ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
^
/usr/include/c++/4.9/bits/stl_construct.h:75:7: note: candidates are:
In file included from /usr/include/c++/4.9/string:52:0,
from 1:
/usr/include/c++/4.9/bits/basic_string.h:540:9: note: template<class _InputIterator> std::basic_string<_CharT, _Traits, _Alloc>::basic_string(_InputIterator, _InputIterator, const _Alloc&)
basic_string(_InputIterator __beg, _InputIterator __end,
^
/usr/include/c++/4.9/bits/basic_string.h:540:9: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/4.9/vector:62:0,
from 2:
/usr/include/c++/4.9/bits/stl_construct.h:75:7: note: candidate expects 3 arguments, 1 provided
{ ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
^
In file included from /usr/include/c++/4.9/string:52:0,
from 1:
/usr/include/c++/4.9/bits/basic_string.h:530:7: note: std::basic_string<_CharT, _Traits, _Alloc>::basic_string(std::initializer_list<_Tp>, const _Alloc&) [with _CharT = wchar_t; _Traits = std::char_traits<wchar_t>; _Alloc = std::allocator<wchar_t>]
basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc());
I have modified and compiled code and facing lot of errors.

Such as?


By the way I really suggest you start by discarding that horrible "Java" code and start a new program in C++. Write your own code, don't try to find something on the web.

Have modified below line and ended up with lot of new errors. Above C++ is my own code written new, but has issues and not working as expected.
int main(int argc, char **argv)
Last edited on
but has issues and not working as expected.

Yes, as I stated earlier you're trying to force a java program into C++ without really knowing how the differences in the languages are going to affect the outcome.

I would recommend you start with a simple main() function without any classes and without command line arguments.

Something more like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <vector>


int main()
{
   std::vector<int> values{2,4,6,8,3};

   for(auto& current : values)
        std::cout << current << ' ';

   std::cout << std::endl;

}


Edit: Please stop modifying previous posts, and create a new post with the modified content.

Also the number and types of errors you have posted is because of not compiling early and often.
Last edited on
I have reverted back my latest change and below is the original code, which has below set of errors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>

class Solution
{

public:
	std::vector<int> i = std::vector<int>();
	std::vector<int> sum = std::vector<int>();
	int add = 0;
	virtual void append(std::vector<int> &list);
	virtual bool has(int total);
	static void main(std::vector<std::wstring> &args);
};

void Solution::append(std::vector<int> &list)
{

	for (int l = 0;l < list.size();l++)
	{
		if (i.size() > 0 && l <= i.size() - 1)
		{
			add -= i.erase(i.begin() + l);
		}
		i.push_back(list[l]);
		add += list[l];
	}
	sum.push_back(add);
}

bool Solution::has(int total)
{
	if (sum.empty())
	{
		return false;
	}
	else
	{
		return std::find(sum.begin(), sum.end(), total) != sum.end();
	}
}

void Solution::main(std::vector<std::wstring> &args)
{
	Solution *solution = new Solution();

	solution->append(std::vector<int> {1, 2, 3});

	std::wcout << solution->has(6) << std::endl;
	std::wcout << solution->has(9) << std::endl;

	solution->append(std::vector<int> {4});

	std::wcout << solution->has(9) << std::endl;

	delete solution;
}

int main()
{
	std::vector<std::wstring> args(argv + 1, argv + argc);
	Solution::main(args);
	
}


OUTPUT

In member function 'virtual void Solution::append(std::vector<int>&)':
21:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
23:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
25:8: error: no match for 'operator-=' (operand types are 'int' and 'std::vector<int>::iterator {aka __gnu_cxx::__normal_iterator<int*, std::vector<int> >}')
 In static member function 'static void Solution::main(std::vector<std::basic_string<wchar_t> >&)':
49:45: error: no matching function for call to 'Solution::append(std::vector<int>)'
49:45: note: candidate is:
18:6: note: virtual void Solution::append(std::vector<int>&)
18:6: note:   no known conversion for argument 1 from 'std::vector<int>' to 'std::vector<int>&'
54:39: error: no matching function for call to 'Solution::append(std::vector<int>)'
54:39: note: candidate is:
18:6: note: virtual void Solution::append(std::vector<int>&)
18:6: note:   no known conversion for argument 1 from 'std::vector<int>' to 'std::vector<int>&'
58:9: warning: deleting object of polymorphic class type 'Solution' which has non-virtual destructor might cause undefined behaviour [-Wdelete-non-virtual-dtor]
 In function 'int main()':
63:33: error: 'argv' was not declared in this scope
63:50: error: 'argc' was not declared in this scope
Your program looks too much like Java, throw that crappy class in the bin, and start over in C++. Forget, for now that you can create a static class function, forget about new/delete. If you must have a class just create a basic class that contains the data and functions that operate on that data. DON'T TRY TO FORCE EVERYTHING INTO A CLASS.

Other than the first couple of warnings all of the errors are because you are either missing operator overloads, constructors, destructors, and trying to use variables that have not been defined.

Also trying to force everything into a class for this problem is a big mistake.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <vector>
#include <set>
using namespace std;

set<int> successive_3_sum( const vector<int> &V )
{
   set<int> S;
   if ( V.size() >= 3 )
   {
      int sum = V[0] + V[1] + V[2];
      S.insert( sum );
      for ( int i = 3; i < V.size(); i++ )
      {
         sum += V[i] - V[i-3];
         S.insert( sum );
      }
   }
   return S;
}

int main()
{
   vector<int> V = { 1, 2, 3, 4 };
   set<int> S = successive_3_sum( V );
   cout << "The successive-three-element sums are: ";
   for ( auto e : S ) cout << e << " ";
}


The successive-three-element sums are: 6 9 
Last edited on
Hi lastchance

Thanks for your inputs. As far as example mentioned in that JAVA link,
the two totals (one+two+three = six and two+three+four =nine). At this point contains(six) and contains(nine) should return True, while contains(seven) should return False.
Last edited on
@leo2008 - I gave you a routine that finds all the sums of three successive elements and puts them in a set. All you need to test a single value is ONE LINE of code to see if that value is in the set! Just use S.find()

You don't need a thumping great class for that.

I think you need to forget java - all your codes seem to carry the baggage associated with it.
Last edited on
@lastchance
I was noticing that in other posts too. So sad. Another young mind ruined by Java.
I was noticing that in other posts too. So sad. Another young mind ruined by Java.
I was noticing that in other posts too. So sad. Another young mind ruined by Java.
Thanks everyone for all your valuable inputs.
Topic archived. No new replies allowed.