【问题标题】:Calling a function in a member method of a class, the two are in different files在类的成员方法中调用函数,两者在不同的文件中
【发布时间】:2022-12-05 21:43:19
【问题描述】:

我有这个功能支配性的定义在检查CDS.cpp并在检查CDS.h. 我试图在成员方法中调用它初始化Individual 类的定义在个人.cpp并在个人.h

我只包括我们感兴趣的代码部分。

`

//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) {`

【问题讨论】:

    标签: c++


    【解决方案1】:

    看起来错误消息告诉您表达式 isDominating(col) 不是有效的函数调用,因为 isDominating 不是函数。

    出现这个错误的原因是你的Individual.cpp文件中没有包含头文件CheckCDS.h,所以编译器不知道isDominating是一个函数。要解决此问题,您需要在 Individual.cpp 文件的顶部包含以下行:

    #include "CheckCDS.h"
    

    这将告诉编译器有关 isDominating 函数的信息,并允许您在初始化方法中调用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-23
      • 2011-04-02
      • 2021-10-17
      • 1970-01-01
      相关资源
      最近更新 更多