2 Replies - 734 Views - Last Post: 25 June 2010 - 06:11 AM Rate Topic: -----

#1 solomon201   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 19-November 07

"ERROR Undefined Reference" Pls help

Posted 23 June 2010 - 11:28 PM

I am having problems compiling this code. first I used arrays in place if vectors but the problem is still there. when i try to compile it tells me
"Linker Error undefined reference to record::record()

I am using Dev Cpp as my compiler.

Pls guys take a look at it and tell we what is wrong.


#include<iostream>
#include<vector>
#include<fstream>
#include<string>
using namespace std;


class record
{
 	private:
		  string name;
		  vector<int>score;
		  int total;
		  double average;
	   	  
	   	  int settotal()
	   	  {
 		    int temp=0;
 		    for(int index=0;index<score.size();index++)
 		    temp+=score[index];
 		    return temp;
		    }
		    
		    double setaverage()
		    {return (total/score.size());}
		    

		    public:
		    		    
		    record();
		    
		    void setdata(string n, vector<int>data)
		    {name=n; 
		    score.resize(data.size());
		    for(int k=0;k<data.size();k++)
		    score[k]=data[k];}
		    
		    
		    void display()
		    {
	   		cout<<name<<" ";
	   		for(int t=0;t<score.size();t++)
	   		cout<<score[t]<<" ";
			}
		    
		    
		    
		    };
		    
		    int main()
		    {
		     	  	  record people;
		     	  	  ifstream readfile;
		     	  	  readfile.open("test.txt");
		     	  	  string tname;
		     	  	  vector<int>tdata;
		     	  	  tdata.resize(3);
		     	  	  readfile>>tname;
		     	  	  {
	     			    
	     			    for(int index=0;index<tdata.size();index++)
	     			    readfile>>tdata[index];
				    }
				    people.setdata(tname,tdata);
				    
				    
				
				    return 0;
				    }
	     



As I said earlier I have tried using arrays in place of vectors but I get the same error.
what I am trying to do is read data from a text file, compute total and average then display all the data;

Your help will greatly be appreciated.

This post has been edited by no2pencil: 23 June 2010 - 11:42 PM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: "ERROR Undefined Reference" Pls help

#2 sarmanu   User is offline

  • D.I.C Lover
  • member icon

Reputation: 967
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: "ERROR Undefined Reference" Pls help

Posted 24 June 2010 - 12:20 AM

The Compiler is complaining about record class constructor. It cannot find it's body. As you can see, you only prototyped it, but you didn't give it a body: record(); => prototype;
record() { } => now it has a body.
As a tip, here:
score.resize(data.size());
for(int k=0;k<data.size();k++)
   score[k]=data[k];


You don't need this, as you could easily use the assignment operator, overloaded in vector class:
// That's all.
score = data;



And get a newer Compiler. DevCPP is not updated anymore, and to be honest, it's not that good. Get Visual C++ 2010.
Was This Post Helpful? 0
  • +
  • -

#3 solomon201   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 19-November 07

Re: "ERROR Undefined Reference" Pls help

Posted 25 June 2010 - 06:11 AM

View Postsolomon201, on 23 June 2010 - 10:28 PM, said:

I am having problems compiling this code. first I used arrays in place if vectors but the problem is still there. when i try to compile it tells me
"Linker Error undefined reference to record::record()

I am using Dev Cpp as my compiler.

Pls guys take a look at it and tell we what is wrong.


#include<iostream>
#include<vector>
#include<fstream>
#include<string>
using namespace std;


class record
{
 	private:
		  string name;
		  vector<int>score;
		  int total;
		  double average;
	   	  
	   	  int settotal()
	   	  {
 		    int temp=0;
 		    for(int index=0;index<score.size();index++)
 		    temp+=score[index];
 		    return temp;
		    }
		    
		    double setaverage()
		    {return (total/score.size());}
		    

		    public:
		    		    
		    record();
		    
		    void setdata(string n, vector<int>data)
		    {name=n; 
		    score.resize(data.size());
		    for(int k=0;k<data.size();k++)
		    score[k]=data[k];}
		    
		    
		    void display()
		    {
	   		cout<<name<<" ";
	   		for(int t=0;t<score.size();t++)
	   		cout<<score[t]<<" ";
			}
		    
		    
		    
		    };
		    
		    int main()
		    {
		     	  	  record people;
		     	  	  ifstream readfile;
		     	  	  readfile.open("test.txt");
		     	  	  string tname;
		     	  	  vector<int>tdata;
		     	  	  tdata.resize(3);
		     	  	  readfile>>tname;
		     	  	  {
	     			    
	     			    for(int index=0;index<tdata.size();index++)
	     			    readfile>>tdata[index];
				    }
				    people.setdata(tname,tdata);
				    
				    
				
				    return 0;
				    }
	     



As I said earlier I have tried using arrays in place of vectors but I get the same error.
what I am trying to do is read data from a text file, compute total and average then display all the data;

Your help will greatly be appreciated.




Thanks a alot I appreciate
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1