Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 944929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:40:24+00:00 2026-05-15T22:40:24+00:00

i have a problem with this class. the goal is to make the main

  • 0

i have a problem with this class.
the goal is to make the main function work properly. we were supposed to implement the “And” function object so that the code will work. i can’t find what is the problem with our solution.
(the solution start and end are marked in comments in the code before the “main” function)
can you please help?
thanks

#include <iostream>
#include <algorithm>

using namespace std;

class NotNull
{
    public:
    bool operator()(const char* str) {return str != NULL;}
};

class BeginsWith
{
    char c;
    public:
    BeginsWith(char c) : c(c) {}
    bool operator()(const char* str) {return str[0] == c;}
};

class DividesBy {
    int mod;
    public:
    DividesBy(int mod) : mod(mod) {}
    bool operator()(int n) {return n%mod == 0;}
};

//***** This is where my sulotion starts ******

template <typename Function1, typename Function2, typename T>
class AndFunction
{
    Function1 f1;
    Function2 f2;
    public:
    AndFunction(Function1 g1, Function2 g2) : f1(g1), f2(g2) {}
    bool operator()(T t)
    {
        return (f1(t) && f2(t));
    }
};

template <typename Function1, typename Function2, typename T>
AndFunction <Function1, Function2, T>

bool And(Function1 f1, Function2 f2)
{
    return AndFunction<Function1, Function2, T>(f1, f2);
}

//***** This is where my sulotion ends ******

int main(int argc, char** argv)
{
    int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    char* strings[4] = {"aba", NULL, "air", "boom"};
    cout << count_if(array,array+10,And(DividesBy(2),DividesBy(4))) << endl;
    // prints 2, since 4 and 8 are the only numbers which can be divided by
    // both 2 and 4.
    cout << count_if(strings,strings+4,And(NotNull(),BeginsWith('a'))) <<endl;
    // prints 2, since only "aba" and "air" are both not NULL and begin
    // with the character 'a'.
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T22:40:24+00:00Added an answer on May 15, 2026 at 10:40 pm

    The template parameter T can’t be inferred, it must be specified explicitly:

    template <typename T, typename Function1, typename Function2>
    AndFunction <Function1, Function2, T>
    And(Function1 f1, Function2 f2)
    {
        return AndFunction<Function1, Function2, T>(f1, f2);
    }
    
    //***** This is where my sulotion ends ******
    
    int main(int argc, char** argv)
    {
        int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        char* strings[4] = {"aba", NULL, "air", "boom"};
        cout << count_if(array,array+10,And<int>(DividesBy(2),DividesBy(4))) << endl;
        // prints 2, since 4 and 8 are the only numbers which can be divided by
        // both 2 and 4.
        cout << count_if(strings,strings+4,And<const char*>(NotNull(),BeginsWith('a'))) <<endl;
        // prints 2, since only "aba" and "air" are both not NULL and begin
        // with the character 'a'.
        return 0;
    }
    

    jpalecek’s solution is better and works as follows:

    //***** This is where my sulotion starts ******
    
    template <typename Function1, typename Function2>
    class AndFunction
    {
        Function1 f1;
        Function2 f2;
        public:
        AndFunction(Function1 g1, Function2 g2) : f1(g1), f2(g2) {}
      template<typename T> bool operator()(T)
        {
            return (f1(t) && f2(t));
        }
    };
    
    template <typename Function1, typename Function2>
    AndFunction <Function1, Function2>
    And(Function1 f1, Function2 f2)
    {
        return AndFunction<Function1, Function2>(f1, f2);
    }
    
    //***** This is where my sulotion ends ******
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem, I need to change body of method when this class
i've a problem with my tabularinline field. I have model like this class Product(models.Model):
Ok guys, I have a serious problem with this. I have a static class
This is my problem: I have a player class and SwipeDetector Class in c#,
I have problem with this code: file = tempfile.TemporaryFile(mode='wrb') file.write(base64.b64decode(data)) file.flush() os.fsync(file) # file.seek(0)
Hi i have problem with this code, i found it on the internet and
i am a beginner and i have a problem : this code doesnt compile
I have this problem: I want to generate a new source code file from
Problem: I'd like to specify the main class in a jar file that I
I have an abstract Singleton class. My goal is that any subclasses just have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.