www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Create a class

reply Ty Tower <tytower hotmail.com.au> writes:
I am trying to create a Transaction class that I can call a blank transaction
when needed and fill in the fields then write it to a file .Then I  get the
next new one and so on.

The below compiles an object file but gets stuck in the linker with this message
It goes OK if I put a "main" in it but I didn't think I had to have a main in a
class file?
-----------------------------------------------------------------------------------------------------------
[tytower localhost TangoExamples]$ dmd ./Transaction.d
gcc Transaction.o -o Transaction -m32 -Xlinker -L/usr/local/bin/../lib
-ltango-user-dmd -ltango-base-dmd -lpthread -lm
/usr/local/bin/../lib/libtango-base-dmd.a(dmain2.o): In function
`_D6dmain24mainUiPPaZi7runMainMFZv':
dmain2.d:(.text._D6dmain24mainUiPPaZi7runMainMFZv+0x10): undefined reference to
`_Dmain'
/usr/local/bin/../lib/libtango-base-dmd.a(deh2.o): In function
`_D4deh213__eh_finddataFPvZPS4deh213DHandlerTable':
deh2.d:(.text._D4deh213__eh_finddataFPvZPS4deh213DHandlerTable+0x9): undefined
reference to `_deh_beg'
deh2.d:(.text._D4deh213__eh_finddataFPvZPS4deh213DHandlerTable+0xe): undefined
reference to `_deh_beg'
deh2.d:(.text._D4deh213__eh_finddataFPvZPS4deh213DHandlerTable+0x14): undefined
reference to `_deh_end'
deh2.d:(.text._D4deh213__eh_finddataFPvZPS4deh213DHandlerTable+0x37): undefined
reference to `_deh_end'
collect2: ld returned 1 exit status
--- errorlevel 1

------------------------------------------------------------------------------------------------------------
import tango.io.Stdout;
import tango.io.File;
import tango.core.Exception;
import tango.util.Convert;

class Transaction { 
  public int m_Number; 
 
    int getNumber() {
        char[] input = cast(char[])File("number.int").read;
	Stdout.formatln("input array data  = {}",input[]);
	int m_Number = to!(int)(input);
	if(m_Number){
          ++ m_Number  ;
	  Stdout("It's transaction Number is ",m_Number).newline;
	}else {
	   ++ m_Number;
	   Stdout("New, first transaction number", m_Number).newline;}
	return 	m_Number;
			    }

  char[][] readFile() {
	 auto content = cast(char[][]) File("outfile.txt").read;
	Stdout.formatln("Transaction data number is {}",content[0]);
	Stdout.formatln("Transaction date is {}",content[1]);
	Stdout.formatln("Transaction description is {}",content[2]);
	Stdout.formatln("Transaction code is {}",content[3]);
	Stdout.formatln("Transaction debit amount is {}",content[4]);
	Stdout.formatln("Transaction credit amount is {}",content[5]);

	Stdout("Successfully read file 'outfile.txt'").newline;	
	return content;
		 }

  void writeNo(int docno) {
        auto output = new File ("number.int");
	try{	
	void[] y = to!(char[])(docno);
	output.write(y);
	Stdout("Successfully over-wrote to  file 'number.int'").newline;
	}
	  catch (AddressException xy)
	{
	    Stdout("A file exception occured: " ~ xy.toString()).newline;
	}
	 } 


  void writeFile(char[][] content) {
        auto output = new File ("outfile.txt");
	try{	
	output.write(content);
	Stdout("Successfully over-wrote to  file 'outfile.txt'").newline;
	}
	  catch (AddressException xy)
	{
	    Stdout("A file exception occured: " ~ xy.toString()).newline;
	}
	 }
 
    void printArray(char[][]args ){
        Stdout.formatln("In array args index 0= {}",args[0]);
	Stdout.formatln("In array args index 1= {}",args[1]);
	Stdout.formatln("In array args index 2= {}",args[2]);
	Stdout.formatln("In array args index 3= {}",args[3]);
	Stdout.formatln("In array args index 4= {}",args[4]);
	Stdout.formatln("In array args index 5= {}",args[5]); }

    this() {
	char[][] data ;
	char[] docno = to!(char[])(getNumber);
	char[] date  ;
	char[] m_details ;
	char[] m_code  ;
	char[] m_debit ; 
	char[] m_credit;
 
	data ~= docno;     //this is an append operator ~ ?
	data ~= date ;
	data ~= m_details;
	data ~= m_code;
	data ~= m_debit;
	data ~= m_credit;

	Stdout("Created a transaction.").newline;
	printArray(data);
	writeNo(to!(int)(docno)) ;
	writeFile(data);
	}
    ~this() {
        Stdout("Destroyed this transaction.").newline;
	    }
	}
----------------------------------------------------------------------------------------------------------
Feb 01 2008
parent Sergey Gromov <snake.scaly gmail.com> writes:
Ty Tower Wrote:

 I am trying to create a Transaction class that I can call a blank transaction
when needed and fill in the fields then write it to a file .Then I  get the
next new one and so on.
 
 The below compiles an object file but gets stuck in the linker with this
message
 It goes OK if I put a "main" in it but I didn't think I had to have a main in
a class file?
This is called a module, not a class file. To compile a module you must use '-c' switch, just like in C: dmd -c Transaction.d which will give you a Transaction.o (or .obj) file to link with later. Or, if your project is small, you can supply all the files to the compiler: dmd App.d Transaction.d Other.d with only one of the modules containing main(), which results in App (or App.exe). SnakE
Feb 01 2008