-3
// In my Class A, I have many nodes and every node data is stored in a struct like this:
Class A 
{
private:
  struct BriteNodeInfo
  {
    int nodeId;
    double xCoordinate;
    double yCoordinate;
    int inDegree;
    int outDegree;
    int asId;
    std::string type;
  };
};

  // Each node instance is stored in a vector like this:

  typedef std::vector<BriteNodeInfo> BriteNodeInfoList;
  BriteNodeInfoList m_briteNodeInfoList;

//And then, here is the function that I want to implent down below

void SaveNodeData (std::string fname);
};

Problem: How do I implent that SaveNodeData() function to save my nodes data in .txt file like this?:

  nodeId0   yCoordinate0   xCoordinate0
  nodeId1   yCoordinate1   xCoordinate1
  nodeId2   yCoordinate2   xCoordinate2
  nodeId3   yCoordinate3   xCoordinate3
   etc...

I have tried but my iteration syntax is not good enough. Here is my function, please help:

Here is my failed function:

void SaveNodeData (std::string fname)
{
  ofstream os(fname.c_str(), ios::trunc);

  vector<BriteNodeInfo> BriteNodeInfoList;
  BriteNodeInfoList m_briteNodeInfoList;

  for (BriteNodeInfoList::Iterator i = m_briteNodeInfoList.Begin(); i != m_briteNodeInfoList.End(); ++i)
    {  
        os << BriteNodeInfo[i].nodeId "\t" << "\t" << BriteNodeInfo[i].yCoordinate; << "\t"BriteNodeInfo[i].xCoordinate<< "\n";
    }

    os << "\n";
 }
Yummy2014
  • 1
  • 1
  • This is completely incomprehensible. There's no such `std::vector` method called "Begin()". There's a method called "begin()", but, as you know, method names are case sensitive. Then, there's a private, inner class called "BriteNodeInfo", of class "A". Which has nothing to do with some strange class name "BriteNodeInfo". Finally, "BriteNodeInfo[i]", where "BriteNodeInfo" is a class name, is nonsensical, in C++. Looks to me like [you either need to read a good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), or show real code instead of fake code. – Sam Varshavchik Oct 27 '17 at 01:46
  • 1
    Your variable is named `m_briteNodeInfoList`, so what you expect `BriteNodeInfo[i].nodeId` to do is unclear. An iterator is also not an index, so if you want to iterate by index use an int. `for(const auto& node : m_briteNodeInfoList) { use node here...}` is an easy way to iterate over all of them if you do not need to add or remove items while iterating. – Retired Ninja Oct 27 '17 at 01:48
  • @SamVarshavchik I think the closing brace for `A` was put in the wrong place, and this "mysterious class" is still being accessed from *within `A`*. If you're going to be snarky about a non-complete non-verifiable example, please at least link to the [page on Minimal, Complete, and Verifiable examples](https://stackoverflow.com/help/mcve) somewhere in your comment. – Daniel H Oct 27 '17 at 01:49
  • What do you mean by _failed function_. Failed to work as you expected, failed to compile? It seems like you're implementing a local function, whereas you may have meant to implement `A::SaveNodeData` – Tas Oct 27 '17 at 01:55
  • The shown code cannot be real, for the reasons I enumerated. – Sam Varshavchik Oct 27 '17 at 02:11
  • No, it is not used "in the same way". It is a method of some nonstandard class called "NodeContainer". Just because some class called "NodeContainer" has a method called "Begin()" doesn't mean that `std::vector`, from the C++ library, has one. In fact, it doesn't. Trying to learn C++ by blindly copy-pasting random code off the Internet, without understand how it's used, is not going to accomplish much. End of story. – Sam Varshavchik Oct 27 '17 at 02:23
  • @SamVarshavchik The real code is here on line 200 and line 278 : https://github.com/cawka/ns-3-dev-ndnSIM/blob/ndnSIM-v2/src/brite/helper/brite-topology-helper.h The file in which I want to implent my function is here: https://github.com/cawka/ns-3-dev-ndnSIM/blob/ndnSIM-v2/src/brite/helper/brite-topology-helper.cc – Yummy2014 Oct 27 '17 at 02:34
  • I repeat: "trying to learn C++ by blindly copy-pasting random code off the Interner, without understanding how it's used, is not going to accomplish much". – Sam Varshavchik Oct 27 '17 at 02:38
  • Thanks @Kimby, at least you are telling how to do things,which is much better than some guy here acting like we are war enemies. – Yummy2014 Oct 27 '17 at 03:09

2 Answers2

0

Before starting, this code as written will clearly have some compile errors. But assuming you can manage to fix those issues, there's one huge flaw.

Your function SaveNodeData creates an empty BriteNodeInfoList, and then tries to read from it. The for loop you have written will always simply exit.

What you need to do is create and populate a BriteNodeInfoList somewhere that this function reads. You could pass it in as an argument to the function, have it as a private variable for class A (assuming SaveNodeData is made into a member of class A). Or you could make it a static member variable of class A (not really recommended: static member variables of objects have some serious problems).

Kimby
  • 124
  • 1
  • 4
0
void SaveNodeData (std::string fname) // file path
{
    ofstream os(fname.c_str(), ios::trunc);

    vector<BriteNodeInfo> BriteNodeInfoList = m_briteNodeInfoList;

    for (std::vector<BriteNodeInfoList>::Iterator it = BriteNodeInfoList.Begin(); it != BriteNodeInfoList.End(); ++it)
      {  
          os << (*it).nodeId << "\t" << (*it).yCoordinate << "\t" << (*it).xCoordinate << "\n";
      }
 }
Yummy2014
  • 1
  • 1