0

I am trying to declare an array of pointers to my Node class/object in a header file and then in the constructor for the class I want to instantiate the size of the array. I then wish to initialize the array with Node object.

The problems that I am having have to do with correct syntax when first declaring the Node array in the SOM.h file. I have tried just having it be a Node* nodeArray and Node* nodeArray[][some constant number]. Not sure if either or both of these are a correct way to do this.

Then in the SOM.cpp constructor I am initializing this this way nodeArray = Node[Config::NODE_GRID_HEIGHT][Config::NODE_GRID_WIDTH]

I then run an initializing function for the array of node pointers

void SOM::RandInitilizeNodeArray(){
    srand (time(NULL));
    for(int i=0; i<10; i++){
        for(int j=0; j<10; j++){
            nodeArray[i][j] = new Node();
            nodeArray[i][j]->modelVec[0] = (rand() % 256)/255;//there is a uniform real distribution that gives better results
            nodeArray[i][j]->modelVec[1] = (rand() % 256)/255;//THE 256 HERE MIGHT NEED TO BE 255
            nodeArray[i][j]->modelVec[2] = (rand() % 256)/255;
        }
    }
}

For each of the 3 times I try to access the modelVec I get "Field "modelVec" could not be resolved" from eclipse. Why? Am I not declaring the array of pointers correctly, not initializing correctly, not accessing correctly. Maybe eclipse just hates me.

Here is more code to look at.

SOM.h

#ifndef SOM_H
#define SOM_H

#include "Node.h"
#include "LoadFile.h"
//#include "LearningFunc.h"
#include "Config.h"


#include <cstdlib>
#include <string>
#include "opencv2/core/core.hpp"
#include <vector>
#include <iostream>
#include <stdio.h>
#include <opencv2/highgui/highgui.hpp>//FOR TESTING
//#include <highgui.h>


class SOM{

public:

// Variables
    Node* nodeArray;//[][Config::NODE_GRID_WIDTH];

    //Node* nodeArray;
    cv::Mat inputImg;
    cv::Mat normalizedInputImg;
    std::string filename;
    cv::Mat unnormalizedInputImg;//FOR TESTING
    cv::Mat outputImg;


    //LearningFunc thislearner();

// Functions
    //Node* FindWinner(std::vector<uchar>);//send in a pixel vector get a winning node in return
    void BatchFindWinner();
    Node* SingleFindWinner(uchar*);
    void NormilizeInput();
    void InitilizeNodeArray();
    void RandInitilizeNodeArray();
    float GetSimilarity(uchar*, uchar*);
    void AllocateNodeArray();
    void OutputNodeArray();
    void UnnormalizeInputImage();
    void DisplayWins();
    cv::Mat OutputSom();
    void MyPause(std::string);//FOR TESTING
    void WriteSomToDisk(std::string);


// Constructors
    SOM(){};
    SOM(std::string);
    ~SOM(){};


private:

};

#endif // SOM_H

SOM.cpp

SOM::SOM(std::string file){
    filename = file;
    inputImg = LoadFile(filename);
    nodeArray = Node[Config::NODE_GRID_HEIGHT][Config::NODE_GRID_WIDTH];
    //nodeArray = new Node[NODE_GRID_HEIGHT][NODE_GRID_WIDTH];
    AllocateNodeArray();
    InitilizeNodeArray();
    //OutputNodeArray();//FOR TESTING
}

void SOM::RandInitilizeNodeArray(){
    srand (time(NULL));
    for(int i=0; i<10; i++){
        for(int j=0; j<10; j++){
            nodeArray[i][j] = new Node();
            nodeArray[i][j]->modelVec[0] = (rand() % 256)/255;//there is a uniform real distribution that gives better results
            nodeArray[i][j]->modelVec[1] = (rand() % 256)/255;//THE 256 HERE MIGHT NEED TO BE 255
            nodeArray[i][j]->modelVec[2] = (rand() % 256)/255;
        }
    }
}

Node.h

#ifndef NODE_H
#define NODE_H

#include <vector>
#include "Config.h"

class Node{

public:
    //variables
    //unsigned int location[2];//location of node in 2d grid
    std::vector<unsigned int> winnerDataPixels;
    uchar* modelVec;


    //functions


    //constructors

    Node();
    ~Node(){};

private:


};


#endif //node.h end define

Node.cpp

#include "Node.h"
#include "Config.h"



Node::Node(){
    modelVec = uchar[Config::vectorLength];
}
aaron burns
  • 267
  • 4
  • 15
  • Is it even compiling?? You are doing modelVec = uchar[Config::vectorLength] without using new operator. It should throw a compile time error – Arpit Aug 29 '14 at 20:50
  • No, not compiling but even when I placed "new" in where you said it did not affect the errors I am getting. – aaron burns Aug 29 '14 at 20:56
  • because other places also you did the same error. What other error are you getting? Fix this type of error at every place and compile again and then tell what other errors are you getting – Arpit Aug 29 '14 at 20:57
  • You just don't use arrays like this in C++. Even if you fix `modelVec` with `new`, `nodeArray` will give you bigger problems because you want it to be two-dimensional. Looks like you need to go back to the basics of using arrays (or preferably `std::vector`s, if you can). If `Config::NODE_GRID_HEIGHT` and the like are constant expressions, then something like `Node nodeArray[Config::NODE_GRID_HEIGHT][Config::NODE_GRID_WIDTH];` in your class definition will do it. – Crowman Aug 29 '14 at 21:00
  • Even doing the 2 things that paul and arpit said I still get the same error in the same place in the RandInitilizeNodeArray function. Does not matter if I use a '.' or '->', i still get that "Field 'modelVec' could not be resolved. – aaron burns Aug 29 '14 at 21:17

0 Answers0