sreeram Sreekrishna
sreeram Sreekrishna

Reputation: 109

how to write the definition for this method i am getting error

thanks in advance i am trying to write the definition for the method

Customer* getMemberFromID(std::string);

this is the definition i wrote but i am getting some error saying [Error] 'Customer' in 'class Store' does not name a type

Store:: Customer* getMemberFromID(std::string id)
{
    for(int i = 0; i < members.size(); i++)
    {
        Customer* c = members.at(i);
        if(c->getAccountID() == id)
        {

            return c;
        }
    }
    return NULL;
}

Upvotes: 1

Views: 72

Answers (3)

BoBTFish
BoBTFish

Reputation: 19767

Try

Customer* Store::getMemberFromId(std::string const& id)

Hard to say with a full Minimal, Complete, and Verifiable example (which you should really provide), but I'm guess getMemberFromId is a member function of Store, but Customer is not a member of Store, and you just got confused by the naming rules.

Note I swapped (std::string id) for (std::string const& id) to avoid an extra copy of the string, when you didn't need it.

Then you could just use the standard algorithm find_if:

Customer* Store::getMemberFromId(std::string const& id)
{
    auto foundPos = std::find_if(members.begin(),
                                 members.end(),
                                 [&](Customer* c){ return c->getAccountId() == id; });
    return (foundPos==members.end())
                ? nullptr
                : *foundPos;
}

Upvotes: 4

Yousaf
Yousaf

Reputation: 29312

Your function prototype is wrong.

Change

Store:: Customer* getMemberFromID(std::string id)

to

Customer* store::getMemberFromID(std::string id)

Upvotes: 1

msc
msc

Reputation: 34628

Function definition rule,

<return type>  <class name> :: <function name><Arguments>

corrected here:

Customer* Store::getMemberFromID(std::string id)
{
// code
}

Upvotes: 2

Related Questions