簡體   English   中英

在類中定義私有結構時,如何將其用作函數參數或返回類型?

[英]when defining a private structure in a class how do i use it as a function parameter or return type?

我在graphics.h中創建了graph.h,graph.cpp和main.cpp,並創建了兩個私有結構Edge和Node,並在Node上方首先聲明了Edge,但是Edge有一個node成員。所以我已經向前聲明了Node結構,但是遇到了一個問題,說“ Graph :: Node *”與“ Node *”不兼容,這發生在我的graph.cpp AddEdgeByName(string,string,double)函數中,為什么這會發生嗎? 最重要的是,我不知道為什么我必須在(graphics.cpp)中聲明函數Graph :: Node * Graph :: findByName(string name),就像我想的那樣,我最初試圖像這個Node * Graph那樣聲明它:: findByName(string name)可以,但是給了我一個錯誤。 我正在嘗試制作有向加權圖。

#include <iostream>
#include <string>
#include <vector>



using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::vector;

struct Node;

class Graph{

    private:
        typedef struct edge{
            double edgeWeight;
            Node *pointsTo;

            edge(){}
            edge(const edge & copyObject){
                this->edgeWeight = copyObject.edgeWeight;
                this->pointsTo = copyObject.pointsTo;
            }

        } Edge;

        typedef struct node{
            string name;
            vector<Edge> myEdges;// list of outgoing edges for this node

            node(string myName){
                name = myName;
            }

            node(const node & copyObject){
                this->name = copyObject.name;
                this->myEdges = copyObject.myEdges;
            }
            // adds another edge to this node
            void addEdge(Node *pointTo, double weight){
                Edge newEdge;
                newEdge.edgeWeight = weight;
                newEdge.pointsTo = 
                newEdge.pointsTo = pointTo;
                myEdges.push_back(newEdge);
            }
        } Node;

        vector<Node*> graph; // vector containing all the nodes for the
                             //graph
    public:
        Graph();
        // finds a node in the list of nodes for the graph, using the name
        Node * findByName(string name);

        void add(string name);

        void addEdgeByName(string first, string second,double weight);


};

#endif




// this is graphics.cpp

#include "Graph.h"


Graph::Graph(){



}

Graph::Node * Graph::findByName(string name){



    for(std::vector<Node*>::iterator it = graph.begin(); it != graph.end(); ++it){

        if((*it)->name == name){
            return *it;
        }
    }
    return NULL;
}

void Graph::add(string name){

    Node * newNode = new Node(name); // creates a new node with the given                 //name
    graph.push_back(newNode); // adds this node to our list of nodes in the //graph
}

// this publicly accessible function adds edges to each of the strings

void Graph::addEdgeByName(string first, string second,double weight){

    Node * firstNode = findByName(first);
    Node * secondNode = findByName(second);
    firstNode->addEdge(secondNode,weight);
    secondNode->addEdge(firstNode,weight);

}

不要在C ++中使用typedef struct node { … } Node 那是C。

NodeGraph內部的結構,因此您需要在其中而不是在外部進行聲明:

class Graph {
private:
    struct Node;

    struct Edge {
        …
    };

    struct Node {
        …
    };
};

關於公共/私人問題:您不能。 具有private可見性的結構意味着它僅在代碼的功能內可用。

將其public將是一種解決方案。 另一種選擇是根本不使用嵌套類。 (我個人不喜歡他們)。


您應該避免使用typedef struct edge {....} Edge; 而是使用struct Edge { .... }; 給您的結構兩個名字沒有任何好處。

您的前向聲明struct Node; Graph::Node聲明一個不同的結構。 也許您的意思是struct Node; Graph定義內。 然后它將是Graph::Node的前向聲明。

最后, Graph::需要在Graph::Node * Graph::findByName(string name) ,因為編譯器從左至右讀,所以如果看到Node *那么它不知道你還沒有說什么。 通過使用C ++ 11返回類型語法可以避免此問題:

auto Graph::findByName(string name) -> Node *

->部分中的名稱在Graph范圍中查找,因為編譯器已經看到我們正在實現Graph成員函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM