0

I'm struggling with some basic errors I simply don't understand.

utils.cuh

#pragma once

#include "stdafx.cuh"
#include "NCMForest.cuh"
#include "NCMTree.cuh"

int readCSV(int Nrow, int Ncol, char *sep, char *filename, double **data, char **fields_names);
int closest_centroid(c_Node **pnodes, double *element, int nb_centroids, int nb_features);

NCMForest.cuh

#pragma once

#include "utils.cuh"
#include "stdafx.cuh"
#include "NCMTree.cuh"

class c_Forest
{
    int nb_trees;
    int max_depth;
    int max_features;
    int min_samples_split;

    c_Tree **trees;
};

NCMTree.cuh

#pragma once

#include "utils.cuh"
#include "stdafx.cuh"
#include "NCMForest.cuh"

class c_Node;
class c_Tree;

class c_Node
{
public:
    float *centroid;        //Centroid coords
    bool is_leaf;           //Is it a leaf
    int nb_children;        //How many children
    c_Node **children;      //Node children

    c_Node();
    int update_centroid();  //Update the centroids

    double **current_data;
};


class c_Tree
{
public:
    int ID;                 //Tree ID
    c_Node *root;           //Root node

    c_Tree(int p_features_per_node, int p_max_depth, int p_min_samples_split, int p_min_sample_leaf, int p_nb_features);
    ~c_Tree();
    int train(int nb_labels, c_Node *currentNode);
    int classify(double *element, c_Node *Node);
};

The first one is identifier "c_Tree" is undefined" at the line 14 of NCMForest.cuh. And the second one is identifier "c_Node" is undefined" at the line 8 of utils.cuh

I don't understand why I'm having this errors. The headers that defines this classes are included in the files. Any clue ? :/

Robert Crovella
  • 143,785
  • 11
  • 213
  • 257
Lolom
  • 1
  • 1
    Forest includes tree, and tree includes forest, but you `#pragma once` so one of those includes doesn't happen. When you can, if you don't need the definition, just use a forward declaration (like `class c_Node;` ) instead of a #include. – Gem Taylor Feb 20 '20 at 15:35
  • So I'm a bit confused. Do you want me to add `class c_Node;` in _utils.h_, and `class c_Tree;` in _Forest.h_ ? It does fix the error, but I just want to make sure I'll not have errors later by redefining the classes elsewhere. – Lolom Feb 20 '20 at 16:09
  • Try using include guards instead of pragma once. – Doliprane Feb 20 '20 at 16:27
  • Would be the same issue with include guards. Yes, when you reference a class only by "name" (pointer or reference) in a header just do the forward declare, not the #include. If you want to access functionality of the class from one header then you do need to include the other header. You will realise that this makes it very difficult for 2 classes to reference each other's functionality, and generally you only need to do that in the implementation. – Gem Taylor Feb 20 '20 at 16:51

0 Answers0