Home > Mobile >  Calling a function in a member method of a class, the two are in different files
Calling a function in a member method of a class, the two are in different files

Time:12-06

I have the function isDominating which is defined in CheckCDS.cpp and declared in CheckCDS.h. I'm trying to call it in the member method initialize of the class Individual wchich is defined in Individual.cpp and declared in Individual.h

I only include the part of the code that interests us.

`

//This thi the file CheckCDS.cpp
#include "pch.h"
#include "CheckCDS.h"

using namespace std;

extern int n;

bool isDominating(vector<char>& col){
    for (int i = 0; i < n; i  ) {
        if (col[i] == 'w')
            return false;
    }
    return true;
}
//This this the file Individual.cpp
#include "pch.h"
#include "Individual.h"

using namespace std;

extern int n;
vector<vector<int>> neighbors;

void Individual::initialize()
{
    int v, a, c;
    int test = 0;

    val.resize(n, 0);
    col.resize(n, 'w');
    test = 0;
    while (isDominating(col) == false) {
        do {
            c = 0;
            v = rand() % (n);
            if (test == 0) {
                c = 1;
            }
            else if (test != 0 && col[v] == 'g')
                c = 1;
        } while (c == 0);   //si=0 on repete

        test  ;
        val[v] = 1;
        col[v] = 'b';

        int k = 0;
        while (neighbors[v][k] != -1) {
            a = neighbors[v][k];
            if (col[a] == 'w')
                col[a] = 'g';
            k  ;
        }
    }
}


The message error is **expression preceding parentheses of apparent call must have function type (pointer-to-)**, is appear in this line  ` while (isDominating(col) == false) {`

CodePudding user response:

It looks like the error message is telling you that the expression isDominating(col) is not a valid function call because isDominating is not a function.

The reason for this error is that you have not included the header file CheckCDS.h in your Individual.cpp file, so the compiler does not know that isDominating is a function. To fix this, you need to include the following line at the top of your Individual.cpp file:

#include "CheckCDS.h"

This will tell the compiler about the isDominating function and allow you to call it in your initialize method.

CodePudding user response:

I found the problem in another part of the code and it was solved.

  •  Tags:  
  • c
  • Related