我试图在头文件中声明一个指向Node类/对象的指针数组,然后在我想要实例化数组大小的类的构造函数中。然后我希望用Node对象初始化数组。
首次在SOM.h文件中声明Node数组时,我遇到的问题与正确的语法有关。我试过让它成为Node* nodeArray
和Node* nodeArray[][some constant number]
。不确定这些中的一个或两个是否是正确的方法。
然后在SOM.cpp构造函数中,我以这种方式初始化nodeArray = Node[Config::NODE_GRID_HEIGHT][Config::NODE_GRID_WIDTH]
然后我为节点指针数组运行初始化函数
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;
}
}
}
对于我尝试访问modelVec的3次中的每一次,我得到&#34; Field&#34; modelVec&#34;无法解决&#34;来自日食。为什么?我没有正确地声明指针数组,没有正确初始化,没有正确访问。也许eclipse只是讨厌我。
这里有更多代码可供查看。
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];
}