user2129623
user2129623

Reputation: 2257

Add C++ function to different file and call it

I have cpp file f1.cpp, which looks like this

#include "header.h"
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include "Pkins.h"
#include <fstream>
#include <time.h>

using namespace std;

size_t get_num_output_group(void) {
  return 4;
}

size_t get_num_feature(void) {
  return 44;
}

static inline size_t pred_form(float* pred) {
  const int num_class = 4;
  int max_index = 0;
  float max_margin = pred[0];
  int k;
  for (k = 1; k < num_class; ++k) {
    if (pred[k] > max_margin) {
      max_margin = pred[k];
      max_index = k;
    }
  }
  pred[0] = (float)max_index;
  return 1;
}

size_t predict_class(union Entry* data, int pred_margin, float* result) {
  float sum[4] = {0.0f};
  int i;
  unsigned int tmp;
  if (!(data[33].missing != -1) || (data[33].fvalue < 3.3902447)) {
    if (!(data[33].missing != -1) || (data[33].fvalue < 1.7016318)) {
      if (!(data[9].missing != -1) || (data[9].fvalue < 0.75673783)) {
        sum[0] += (float)-0.025704622;
      } else {
        sum[0] += (float)0.0013114753;
      }
    } else {
      if (!(data[6].missing != -1) || (data[6].fvalue < -5781.0889)) {
        if (!(data[34].missing != -1) || (data[34].fvalue < 2.2995086)) {
          sum[0] += (float)-0.02
         ...............

predict_class function is large, with 10k lines.

Currently I am calling it like this from the same file

  predict_class(data,0,result);

How do I add predict_class to other file and import it so that I can call it same way as I am doing currently.

Currently I have created model.cpp and added full content 10k lines of code into it.

And created header file func.h. How should I declare the function in this header file.

size_t predict_multiclass(union Entry* data, int pred_margin, float* result)

If I add this. much in header file, is it fine ?

Upvotes: 0

Views: 73

Answers (1)

Sean
Sean

Reputation: 89

You should have a header file for f1.cpp first, that is f1.hpp. In f1.hpp, declare the function you want to call in another file:

size_t predict_class(union Entry* data, int pred_margin, float* result);

Don't forget to include this header file in f1.cpp. Then in the another file, include f1.hpp first, then you can call predict_class.

Upvotes: 2

Related Questions