LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-27-2011, 01:29 PM   #1
roshyara
LQ Newbie
 
Registered: Jul 2009
Posts: 3

Rep: Reputation: 0
vector of pointers to class object c++


I am trying to read one file
  • having rows with usually a certain fixed number of columns,
  • different comments marked with the start letter #, with some blank lines.
  • Blank lines and comment line should be ignored while reading.
  • The file may also contain by mistake wrong number of columns which should be corrected first as well.
  • Each row gives a class object and it should be saved as an element vector of class object.
For this purpose I have written a code. Everything works fine till end but while terminating the program it comes unknown problem.
Could anyone please help me in finding out mistake? I have marked the possible mistake place as c++ comments.
thank you
  • File to be read:

    #myfile.txt
    1 snp1 1.1 1
    2 snp2 1.2 2
    3 snp3 1.2 3
    # 1
    #2

    #3
    #test

    5 snp5 1.5 5
    #test
    5 snp5 1.5 5
    #2 test
    6 snp6 1.5 6
    #3test
    #test
    #1
    4 snp4 1.4 4
    5 snp5 1.5 5
    #

  • Code to read and save this file:
Code:
#include<iostream>
#include<istream>
#include<string>
#include<fstream>
#include<sstream>
#include<vector>
#include<algorithm>

using namespace std;

struct CSNP{
	friend ostream& operator <<(ostream& out, const CSNP& pCSNP);
	string nchr;
	string  snpName;
	 double cm_pos;
	 int bp;
	static bool three_columns;
	static CSNP* fillUpLocusInfo(const vector<string> lines);
	vector<CSNP*> lociInfo;
	static void readMapFile(const string & fileName, vector<CSNP*>& snpVec);
	CSNP(): nchr(""), snpName(""),  cm_pos(0), bp(0){ }
	~CSNP();	
};
bool CSNP::three_columns =false;
CSNP::~CSNP(){
	for (unsigned int i=0;i<lociInfo.size();++i)
	{
		delete lociInfo[i];
		lociInfo[i]=NULL;
	}
}

void error (const string&  msg){
cerr<<"\n EROOR: " <<msg <<endl;
exit(1);
}
int main (int argc, char** argv){
	if(argv[1]==0)
	error("No file exits. please give argv[1]. ")	;
 CSNP SNP;
SNP.readMapFile(argv[1],SNP.lociInfo);
cout <<"SNP.lociInfo.size(): "<< SNP.lociInfo.size()<<endl;
for (unsigned int i=0; i<SNP.lociInfo.size();++i)
	cout<< *(SNP.lociInfo[i]) << '\n';

SNP.~CSNP();	
return 0;
}
ostream& operator<<(ostream &out, const CSNP &pCSNP)
	  {
	    // This was your display-function. Look up "operator overloading" if you
	    // don't understand what is going on here.
	    return out << "chr No:"<< pCSNP.nchr<< ", SNP ID: "<< pCSNP.snpName
	               << ", cm_pos: " << pCSNP.cm_pos<< " and bp: "<< pCSNP.bp;
	  }

CSNP* CSNP::fillUpLocusInfo(const vector<string> lines){
	CSNP* pCSNP=new CSNP;
	if( !three_columns && lines.size()==3)
		{
		three_columns=true;
		pCSNP->nchr=lines[0];
		pCSNP->snpName=lines[1];
		pCSNP->bp=(long int )atoi(lines[2].c_str());
		}
		else if(!three_columns&& lines.size()==4)
		{
		pCSNP->nchr=lines[0];
		pCSNP->snpName=lines[1];
		pCSNP->cm_pos=atof(lines[2].c_str());
		pCSNP->bp=(long int )atoi(lines[3].c_str());
					
		}
	return pCSNP;	
}	

void CSNP::readMapFile(const string & fileName, vector<CSNP*>& snpVec)
{
	static long int countSNPs=1;
	ifstream in;	in.close(); in.clear();
	ios_base::iostate i= in.rdstate();
	in.open(fileName.c_str(),ios::in);
	if(!in){
	in.setstate(ios_base::failbit);
		in.clear();
	}	
	in>>ws;
	string buffer;
	vector<string> snp;
	while(!in.eof()&& getline(in,buffer, '\n'))
	{
		//getline(in,buffer, '\n');
		if(!buffer.size()|| (buffer.substr(0,1)=="#"))
			continue;
		istringstream line(buffer);
		//vector<string> tokens;
		while(getline(line, buffer, ' '))
			{
				snp.push_back(buffer);
			}
		cout << "snp.size(): "<<snp.size() <<endl;  //for test only
		CSNP* pCSNP;
		pCSNP=CSNP::fillUpLocusInfo(snp);
		cout << *pCSNP<<endl;
		snpVec.push_back(pCSNP);
/* This above line is making problem. If I deactivate this 
above line then it works fine. I think I have problem with either creating vector vector<CSNP*> snpVec or the function 
CSNP::fillUpLocusInfo(const string & lines)
*/
		pCSNP=NULL;
		snp.resize(0);
		++countSNPs;
		
	}
		cout << "i& ios::eofbit()" << (i& ios::eofbit)<<endl;
		cout << "i& ios::failbit()" << (i& ios::failbit)<<endl;
		cout << "i& ios::badbit()" << (i& ios::badbit)<<endl;
		in.clear();
		in.close();
return;}
 
Old 11-28-2011, 05:57 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
It is not necessary to manually call the class destructor; this is done automatically when the object falls out of scope, and hence is destroyed. Thus remove this line of code from your main() function:
Code:
...
SNP.~CSNP();
...
 
Old 11-29-2011, 01:52 AM   #3
roshyara
LQ Newbie
 
Registered: Jul 2009
Posts: 3

Original Poster
Rep: Reputation: 0
I have deleted it but it still crashes. I do not know why. If you could have a look little bit deeply??
 
Old 11-29-2011, 05:59 AM   #4
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by roshyara View Post
I have deleted it but it still crashes. I do not know why. If you could have a look little bit deeply??
I do not know why either. I copy/pasted the code you originally posted, removed the offending line I mentioned earlier, and compiled the program. I then created the data (text) file with the information you supplied, and then ran the program. It completed successfully. I then re-ran the program using valgrind, and it also reported that all allocated memory had been freed.

Thus barring the possibility that you changed more of your code than you are admitting to, I find it hard to believe that the code is still causing an anomaly. Perhaps you failed to recompile the code after you made a change??
 
1 members found this post helpful.
Old 12-11-2011, 06:02 AM   #5
roshyara
LQ Newbie
 
Registered: Jul 2009
Posts: 3

Original Poster
Rep: Reputation: 0
thank you

Quote:
Originally Posted by dwhitney67 View Post
I do not know why either. I copy/pasted the code you originally posted, removed the offending line I mentioned earlier, and compiled the program. I then created the data (text) file with the information you supplied, and then ran the program. It completed successfully. I then re-ran the program using valgrind, and it also reported that all allocated memory had been freed.

Thus barring the possibility that you changed more of your code than you are admitting to, I find it hard to believe that the code is still causing an anomaly. Perhaps you failed to recompile the code after you made a change??
thank you so much dwhitney67! It worked well now. Do you think I can use RAII std::tr1::shared_ptr at this case in stead of delete?
 
Old 12-16-2011, 01:13 AM   #6
ArthurSittler
Member
 
Registered: Jul 2008
Distribution: Slackware
Posts: 124

Rep: Reputation: 31
I do not see any declaration of snpVec in the code. Am I simply missing it, or does it not exist?
If snpVec were a vector of CSNP*, it would provide push_back() of the CSNP* pCSNP to build a list by appending the new pointer at the end of the list.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ - Using class pointers to use class functions? golmschenk Programming 2 04-24-2011 12:41 AM
C++ templated Node class: pointers to different instantated class types jhwilliams Programming 3 08-20-2007 06:20 PM
Implementing a vector class from a list class purefan Programming 9 04-14-2005 10:48 PM
C++ template smart pointers for class hiearchies in a vector push_back error R00ts Programming 7 09-20-2004 01:14 PM
Event driven object-to-object: C++ template class mecanism ( NOT STL or STDC++) bretzeltux Programming 2 12-23-2003 02:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration