Classes problem

This is a discussion on Classes problem within the C++ Programming forums, part of the General Programming Boards category; I decided to get into some coding using class, havn't done this in a while. First up I got all ...

  1. #1
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052

    Classes problem

    I decided to get into some coding using class, havn't done this in a while. First up I got all these weird (to me) errors, and I'm sure its something stupid i'm doing. The source is attatched.

    Thanks.
    Attached Files Attached Files
    Please direct all complaints regarding this post to the nearest brick wall Have a nice day.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Code:
    	infile >> numofentries;
    
    	Entry entries[numofentries];
    	
    	for(int i = 0; i < numofentries; i++)
    	{
    		infile >> members[i].fname;
    		infile >> members[i].lname;
    		infile >> members[i].pnumber;
    		infile >> members[i].address;
    	}
    Firstly, you cant declare a varibale length array like this....the size of the array must be constant (like Entry entries[10];). If you want a variable length array, look at std::vector.

    Also...members[i].fname should be entries[i].fname;

  3. #3
    Registered User The Junglist's Avatar
    Join Date
    Nov 2002
    Posts
    42
    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include "member.h"
    
    int main ()
    {
    	int numofentries;
    
    	ifstream infile("data.txt", ios::in);
    
    	infile >> numofentries;
    
    	Entry* members = new Entry[numofentries];
    	
    	for(int i = 0; i < numofentries; i++)
    	{
    		infile >> members[i].fname;
    		infile >> members[i].lname;
    		infile >> members[i].pnumber;
    		infile >> members[i].address;
    	}
    	
    	cout << numofentries;
    	
    	delete [] members;
    	
    	return 0;
    }
    That should work. You cannot declare an array the way you did, you must use a constant.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with classes and pointers
    By Akkernight in forum C++ Programming
    Replies: 18
    Last Post: 02-21-2009, 05:21 AM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Having a problem with Classes
    By FoxTrot in forum C++ Programming
    Replies: 10
    Last Post: 09-06-2007, 07:40 PM
  4. Problem with destructors.
    By Hulag in forum C++ Programming
    Replies: 7
    Last Post: 06-11-2004, 12:30 PM
  5. problem w/ nested templatized classes
    By *ClownPimp* in forum C++ Programming
    Replies: 8
    Last Post: 10-19-2002, 07:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21