Home > Software engineering >  How do I pass and modify an optional argument to a function through a header file in C ?
How do I pass and modify an optional argument to a function through a header file in C ?

Time:08-20

I am trying to create a function with an optional argument, where the optional argument gets modified/initialized in the function. This is part of a much larger program so I am using a header file. I am getting a compiler error and not sure how to fix it. Here is my code

Main

#include "header.h"
#include <iostream>
#include <stdlib.h>
#include <array>
#include <numbers>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

int main(){
    int nr,np,nsize;
    nr = 3;
    nsize = nr  ;
    vector<double> b(nsize);
    vector<double> x(nsize);
    vector<vector<double>> A(nsize, vector<double>(nsize));
    func(nr,b,x,A,np);
    cout<< np <<endl;
}

Header

//same include statements, omitting for clarity
#ifndef HEADER_H
#define HEADER_H
     void func(int n,vector<double>& b,vector<double>& x,vector<vector<double>>& A,int np = -1);
#endif

func

#include "header.h"

void func(int n,vector<double> &b,vector<double> &x,
vector<vector<double>> &A,int &np){

    np =2;

}

I can get it to compile without the ampersand before np in func, but then the return value of np does not change in main. I also tried having the ampersand on np in the header file but that also gives me an error. Any advice would be appreciated.

Errors

Error with ampersand included in header.h:
header.h:24:97: error: cannot bind non-const lvalue reference of type 'int&' to an rvalue of type 'int'
24 | void gausspivot(int n,vector& b,vector& x,vector<vector>& A,int &np = -1)

Error with ampersand in func:
undefined reference to `func(int, std::vector<double, std::allocator >&, std::vector<double, std::allocator >&, std::vector<std::vector<double, std::allocator >, std::allocator<std::vector<double, std::allocator > > >&, int)' collect2.exe: error: ld returned 1 exit status

CodePudding user response:

You achieve that by function overloading, i.e. having the same function with different parameters. The function with less parameters calls the function with more parameters and provides the default value.

#include <iostream>
#include <vector>

void func(int n, std::vector<double>& b, std::vector<double>& x,
    std::vector<std::vector<double>>& A, int& np) {
    np = 2;
}

void func(int n, std::vector<double>& b, std::vector<double>& x,
    std::vector<std::vector<double>>& A) {
    std::cout << "You called the function with default value of -1\n";
    int dummy = -1;
    func(n, b, x, A, dummy);
    std::cout << "The value is now " << dummy <<"\n";
}

int main()
{
    std::vector<double> b;
    std::vector< std::vector<double>> A;
    func(0, b, b, A);
}

CodePudding user response:

One way to get this to compile is to have a global variable that serves as the default argument

#ifndef HEADER_H
#define HEADER_H

extern int np_default;

void func(int n, vector<double>& b,
     vector<double>& x, vector<vector<double>>& A,
     int& np = np_default);

#endif

Now this is ugly. Is the ugliness worth it? Only you can judge that.

  • Related