need help to find issue in the code urgent!!

Nov 5, 2022 at 3:11pm
Below code my TestLoop class do not work in the code. What could be wrong here?
Please help me on this.

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
#include <iostream>
#include <unordered_map>
#include <memory>

class TestLoop { class Arguments { public: Arguments(int third, int fourth) { this->third = third; this->fourth = fourth; } int third; int fourth; }; 
public: TestLoop(std::function<int(int, int)> function): function(function) {}
int find(int third, int fourth)
{

 auto arg = std::make_shared<Arguments>(third, fourth);
 auto itr = calcs.find(arg);
 if(itr != calcs.end())
  return itr->fourth;

int calc = function(third, fourth);
calcs[arg] = calc;
return calc;
}

private:
std::unordered_map<std::shared_ptr<Arguments>, int> calcs;
std::function<int(int, int)> function;
};

int mod(int c, int d)
{
  return c % d;
}


int main()
{

 TestLoop loop(mod);
 std::cout << loop.find(5,1) << std::endl;
 std::cout << loop.find(7,5) << std::endl;
 std::cout << loop.find(5,1) << std::endl;
}
Last edited on Nov 5, 2022 at 3:37pm
Nov 5, 2022 at 3:26pm
I cant figure out , what mistake have I done here?
Nov 5, 2022 at 3:58pm
First, indentation/whitespace can make code easier or harder to "see".
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
#include <iostream>
#include <unordered_map>
#include <memory>

class TestLoop {
    class Arguments {
    public:
        Arguments(int third, int fourth)
        { this->third = third; this->fourth = fourth; }
        int third;
        int fourth;
    }; 
public:
    TestLoop(std::function<int(int, int)> function)
    : function(function) {}

    int find(int third, int fourth)
    {
        auto arg = std::make_shared<Arguments>(third, fourth);
        auto itr = calcs.find(arg);
        if(itr != calcs.end())
            return itr->fourth; // error: no member named 'fourth' in 'std::pair<const std::shared_ptr<TestLoop::Arguments>, int>'
        int calc = function(third, fourth);
        calcs[arg] = calc;
        return calc;
    }

private:
    std::unordered_map<std::shared_ptr<Arguments>, int> calcs;
    std::function<int(int, int)> function;
};

int mod(int c, int d)
{
  return c % d;
}

int main()
{

 TestLoop loop(mod);
 std::cout << loop.find(5,1) << std::endl;
 std::cout << loop.find(7,5) << std::endl;
 std::cout << loop.find(5,1) << std::endl;
}

Second, "do not work" does not tell much. Is it perhaps the error that compiler does mention? (See above.)
Last edited on Nov 5, 2022 at 3:59pm
Nov 5, 2022 at 4:01pm
I am not sure why compiler throws error here? the issue here is my TestLoop class implementation doesn't even work. There is some functionality issue apart from this compiler error. But I am not able to figure it out.

main.cpp:22:15: error: no member named 'fourth' in 'std::pair<const std::shared_ptr<TestLoop::Arguments>, int>'
return itr->fourth;
~~~~~^
1 error generated.
Nov 5, 2022 at 5:31pm
Do you mean:

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
#include <iostream>
#include <unordered_map>
#include <memory>
#include <functional>

class TestLoop {
	class Arguments {
	public:
		Arguments(int third, int fourth) {
			this->third = third;
			this->fourth = fourth;
		}

		int third;
		int fourth;
	};

public:
	TestLoop(std::function<int(int, int)> function) : function(function) {}

	int find(int third, int fourth) {
		auto arg = std::make_shared<Arguments>(third, fourth);
		auto itr = calcs.find(arg);

		if (itr != calcs.end())
			return itr->second;

		int calc = function(third, fourth);

		calcs[arg] = calc;
		return calc;
	}

private:
	std::unordered_map<std::shared_ptr<Arguments>, int> calcs;
	std::function<int(int, int)> function;
};

int mod(int c, int d) {
	return c % d;
}


int main() {
	TestLoop loop(mod);
	std::cout << loop.find(5, 1) << std::endl;
	std::cout << loop.find(7, 5) << std::endl;
	std::cout << loop.find(5, 1) << std::endl;
}

Nov 5, 2022 at 10:04pm
I am not sure why compiler throws error here?

So, you see the same error message?

main.cpp:22:15: error: no member named 'fourth' in 'std::pair<const std::shared_ptr<TestLoop::Arguments>, int>'

You essentially have code:
1
2
3
4
5
std::unordered_map<std::shared_ptr<Arguments>, int> calcs;
auto itr = calcs.find(arg);

if (itr != calcs.end())
	return itr->fourth; // error 

The calcs is unordered_map.
Therefore, the itr is a interator to map, because that is what the map::find() returns.
An element of map, is a pair and that the iterator points to.
The compiler points out that a pair (std::pair<const std::shared_ptr<TestLoop::Arguments>, int>) does not have member "fourth".

That is a clear error. You can't tell whether there are any "functionality issues" as long as you can't even compile the program.

The first in that pair is a const std::shared_ptr<TestLoop::Arguments>
The second in that pair is an int
The pair does not have fourth.
Nov 5, 2022 at 10:57pm
making a math function (mod) is just a mess and I recommend against it.
- it bloats your math expressions, harder to read for large expressions
- it redefines a basic operator to do the same thing it already did for no reason
- disrupts normal readability both for the whole expression and also around operator precedence for other coders
- if the compiler can't inline due to flags or internal logic, it may bork performance

this looks like things people do when they come from another language and do not want to learn the new language, they want it to look like what they already know. C++ gives you a lot of tools to make this happen, but you should try to avoid the temptation as the result is always worse in one way or another than doing it in c++ directly.

Edit -- sorry, had the C low level stuff on my mind, brain fail.
Last edited on Nov 5, 2022 at 11:03pm
Nov 6, 2022 at 4:45am
My bad, missed the header file. Realised my mistake.
Thanks a lot seeplus and keskiverto,for your valuable inputs here.
Topic archived. No new replies allowed.