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];
}