0

我试图在头文件中声明一个指向我的 Node 类/对象的指针数组,然后在我想要实例化数组大小的类的构造函数中。然后我希望用 Node 对象初始化数组。

第一次在 SOM.h 文件中声明节点数组时,我遇到的问题与正确的语法有关。我试过让它成为一个Node* nodeArrayand 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 次中的每一次,我从 Eclipse 中得到“无法解析字段“modelVec”。为什么?我是否没有正确声明指针数组,没有正确初始化,没有正确访问。也许日食只是讨厌我。

这是更多要查看的代码。

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

节点.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

节点.cpp

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



Node::Node(){
    modelVec = uchar[Config::vectorLength];
}
4

0 回答 0