I am using the following code (this is the partial code):
bool CWallet::CreateCoinStake(const CKeyStore& keystore, unsigned int nBits, int64_t nSearchInterval, int64_t nFees, CTransaction& txNew, CKey& key)
{
                if(whichType == TX_SCRIPTHASH){//pay to stealth type if exist
//                    std::set<CStealthAddress>::iterator it;
//                    for (it = stealthAddresses.begin(); it != stealthAddresses.end(); ++it){
//                    if(!keystore.GetCScript(stealthAddresses.begin()->Encoded(), key)){
//                        if (fDebug && GetBoolArg("-printcoinstake"))
//                            printf("CreateCoinStake : failed to get key for kernel type=%d\n", whichType);
//                        break;  // unable to find hash of the first stealth address
//                    }
//                    }
                    return true;
                } else {//no stealth address found pay to
    
    
    // Set output amount
    if (txNew.vout.size() == 3)
    {
        txNew.vout[1].nValue = (nCredit / 2 / CENT) * CENT;
        txNew.vout[2].nValue = nCredit - txNew.vout[1].nValue;
    }
    else
        txNew.vout[1].nValue = nCredit;
    // Sign
    int nIn = 0;
    BOOST_FOREACH(const CWalletTx* pcoin, vwtxPrev)
    {
        if (!SignSignature(*this, *pcoin, txNew, nIn++))
            return error("CreateCoinStake : failed to sign coinstake");
    }
    // Limit size
    unsigned int nBytes = ::GetSerializeSize(txNew, SER_NETWORK, PROTOCOL_VERSION);
    if (nBytes >= MAX_BLOCK_SIZE_GEN/5)
        return error("CreateCoinStake : exceeded coinstake size limit");
    // Successfully generated coinstake
    return true;
}
when I send it the rewards, they go to a pub address