0

this is my header:

#include <string>
using namespace std;
class node{
public:
  friend class mesh;
private:
  int id;
  double x,y,z;
};

class element{
public:
  friend class mesh;
  ~element();
//private:
  int id;
  int type;
  int groupid;
  int geoid;
  int nodNum;
  int *nodeid;
};

class mesh{
 public:
  mesh(string filename);
  void showMesh();
  ~mesh();
  int nodesize;
  node *nod;
  int elesize;
  element *ele;
};
 int stringtoint(string a);

And this is the implementation:

#include"myMesh.h"
#include<iostream>
#include<fstream>
using std::ifstream;
#include <sstream>
#include <string>
using namespace std;
element::~element(){
  delete[] nodeid;
}
mesh::~mesh(){
}
mesh::mesh(string filename){
  cout<<"begin to construct..."<<endl;
  string a;
  int nNum;
  int eNum;

  ifstream indata;

  indata.open(filename.c_str());
  do{
    indata>>a;
  }while(a!="$NOD");
  indata>>a;
  nNum=stringtoint(a);
  nodesize=nNum;
  node* nod=new node[nodesize];

  for(int i=0;i<nodesize;i++){
    indata>>a;
    
    nod[i].id=stringtoint(a);
    indata>>a;
    
    nod[i].x=stringtoint(a);
    indata>>a;
    nod[i].y=stringtoint(a);
    indata>>a;
    nod[i].z=stringtoint(a);
  }

  do{
    indata>>a;
  }while(a!="$ELM");
  indata>>a;
  eNum=stringtoint(a);
  elesize=eNum;
  element* ele=new element[elesize];

  for(int i=0;i<elesize;i++){
    indata>>a;
    ele[i].id=stringtoint(a);
    indata>>a;
    ele[i].type=stringtoint(a);
    indata>>a;
    ele[i].groupid=stringtoint(a);
    indata>>a;
    ele[i].geoid=stringtoint(a);
    indata>>a;
    ele[i].nodNum=stringtoint(a);
    ele[i].nodeid=new int(ele[i].nodNum);
    for(int j=0;j<ele[i].nodNum;j++){
      indata>>a;
      ele[i].nodeid[j]=stringtoint(a);    }
  }
 }

void mesh::showMesh(){
	cout<<"NOD"<<endl;
	for(int i=0;i<nodesize;i++){
		cout<<(*this).nod[i].id<<"   "<<(*this).nod[i].x<<"   "<<(*this).nod[i].y<<"   "<<(*this).nod[i].z<<endl;
	}
	cout<<"ELEMENT"<<endl;
	for(int i=0;i<elesize;i++){
		cout<<ele[i].id<<"   "<<ele[i].type<<"   "<<ele[i].groupid<<"   "<<ele[i].geoid<<ele[i].nodNum;
		for(int j=0;j<ele[i].nodNum;j++){
			cout<<"   "<<ele[i].nodeid[j];
		}
		cout<<endl;
	}
}
int stringtoint(string a){
  int b;
  istringstream buffer(a);
  buffer>>b;
  return b;
}

My problem is that it fails when I creat an dynamic array whose elment is nod.id.(the related codes is in red). I can sign the value in the constructor, but when I call the function "showMesh", the array is not created.
could you tell where I am wrong?

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by jonsca
1

"nod" is declared in your constructor which means it is a local variable, and is destroyed once the constructor completes. Declare "nod" as a private member variable, and then you can initialize it in the constructor as you have.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.