Search code examples
c++functionsyntax-errorcall

No matching function for call to 'ANDingOctets'


I'm getting the error "no matching function for call to 'ANDingOctets' for some reason, wanna help my understand why? The function looks okay to me... (It's not a complete function though yet.)

//
//  ANDingOctets.cpp
//  Egetskojs
//
//  Created by Axel Kennedal on 2014-02-12.
//  Copyright (c) 2014 Axel Kennedal. All rights reserved.
//

#include <iostream>
#include <string>
using namespace std;

string ANDingOctets(string octetIP, string & octetSubNet);

int main(){

    /* EXAMPLE ADDRESSES
    string IPaddress = "192.168.0.12";
    string subNetMask = "255.255.255.0";*/

    string netID = ANDingOctets("10110110","11111000");
    cout << netID << endl;

    return 0;
}

string ANDingOctets(string octetIP, string & octetSubNet){
    int subNetBits;


    for (char index = 0; index < 8; index++) {
        if (octetSubNet.at(index) == '1') {
            subNetBits++;
        }
        else break;
    }
    cout << subNetBits << endl;
    return "test";
}

Solution

  • The second argument is a reference:

    string ANDingOctets(string octetIP, string & octetSubNet);
                                               ^
    

    and you can't pass a temporary string by non-constant reference - that's a slightly quirky language rule intended to prevent you accidentally modifying the wrong thing.

    If the function is not intended to modify the argument, then pass by value (like the first argument) or constant reference (const string &).

    If it is intended to modify it (which doesn't seem to be the case), then you'll need to pass a non-temporary variable:

    string subnet = "11111000";
    string netID = ANDingOctets("10110110",subnet);