Problem with constructor?

This is a discussion on Problem with constructor? within the C++ Programming forums, part of the General Programming Boards category; Ok. Im making a login program mostly as practice with various c++ things, and when i tried to compile the ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    25

    Problem with constructor?

    Ok. Im making a login program mostly as practice with various c++ things, and when i tried to compile the code, it gave me one error I can't figure out. I use MSV C++ 6 for my compiler. This is the error:

    error C2512: 'Students' : no appropriate default constructor available

    The error points to this line:

    Code:
    Students *stdnt = new Students[100];//Students not logged in
    And here is the full source. It's large so be warned. I tried to comment it well to make it easy to read.

    Code:
    // Login Program made for MCC 
    // For now DOS based
    //
    // Future features:
    //
    // 1)Saves data to textfile for backup purposes and
    // informational purposes
    //
    // 2)Windows GUI
    // 
    // 3)Log time students logged in
    
    // Programed by:
    // Justin Allen
    // 02/27/2006
    
    #include <iostream>
    #include <string>
    #include <stdio.h>
    
    using namespace std;
    
    class Students
    {
    private:
    	char firstName[26];
    	char lastName[26];
    	int idNum;
    
    public:
    	Students(char[26], char[26], int);
    	void showData();
    	void newStudent();
    	void login();
    	void logout();
    	void setData(char[26], char[26], int = 0);
    };
    
    //Constructor
    Students::Students(char firstname[26], char lastname[26], int idnum)
    {
    	firstName[26]=firstname[26];
    	lastName[26]=lastname[26];
    	idNum=idnum;
    }
    
    //Display data by admin
    void Students::showData()
    {
    	cout << lastName << " , " << firstName;
    }
    
    //Sets data inside class
    void Students::setData(char firstname[26], char lastname[26], int idnum)
    {
    	firstName[26]=firstname[26];
    	lastName[26]=lastname[26];
    	idNum=idnum;
    }
    
    //Main Function
    int main()
    {
    	//Variable declaration
    	int choice, count = 0, i;	
    	int idnum, pwd;
    	const int PWD = 01103657;//'secret' password
    	char firstname[26];
    	char lastname[26];
    	
    	//Pointer to new data structure in class
    	Students *stdnt = new Students[100];//Students not logged in
    	//Students *stdnt2 = new Students[100];//Student logged in
    
    	do//Infinite(for now) Menu Loop
    	{
    		//Main Menu
    		system("cls");//Clear screen
    		cout << "**** MCC COMPUTER LAB LOGON ****\n";
    		cout << "       !!!PLEASE LOGIN!!!       \n\n";
    		cout << "Please choose:\n";
    		cout << "1. Login\n";
    		cout << "2. Logout\n";
    		cout << "3. New Student\n";
    		cout << "Pleas note that you are a New Student if\n"
    			<< "this is your first time here this semester!!\n\n";
    
    		cin >> choice;//Enters menu choice
    
    		switch(choice)
    		{
    		case 1://Login student(not coded yet)
    			//stdnt[].login();
    			break;
    		
    		case 2://Logout student(not coded yet)
    			//stdnt[].logout();
    			break;
    		
    		case 3://Create new student
    			cout << "Type 'exit' if you mistakenly entered new student prompt\n";
    			cout << "Enter first name: ";
    			cin >> firstname;
    			if((stricmp(firstname, "exit") == 0))//checks for exit
    			{
    				break;
    			}
    
    			else//otherwise continues entering new student info
    			{
    				cout << "Enter last name: ";
    				cin >> lastname;
    				cout << "Enter ID number WITHOUT '@': ";
    				cin >> idnum;
    			}
    			
    			//Calls setData function
    			stdnt[count].setData(firstname, lastname, idnum);
    			count++;
    			
    			cout << "\n\n!!!Thanks for registering, please login now!!!";
    			system("pause");
    			break;
    
    		case 8://Hidden admin display data function
    			cout << "Enter password: ";
    			cin >> pwd;
    			if(pwd==PWD)//checks entered password 
    			{
    				cout << "Students not logged in:n";
    				cout << "Last name, First name\n";
    				if(count==0)
    				{
    					cout << "There are no student records yet";
    				}
    
    				else
    				{
    					for(i=0; i<count; i++)
    					{
    						stdnt[i].showData();//shows Data
    					}
    				}
    				system("pause");
    				break;
    			}
    
    			else
    			{
    				cout << "Wrong password!!";
    				break;
    			}
    		
    		default:
    			//Error Handling
    			cout << "\n\nError!! That option does not exist!!\n"
    				<< "Please enter new choice!";
    			system("pause");
    			break;
    		}
    
    	}while(1);
    
    
    	
    	return 0;
    }
    Thanks in advance!

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    Your constructor expects arguements, but you haven't passed any. You'll need to either allocate the array of Students inside of your class or just make a default constructor.

    Also, in your constructor, you can't assign arrays like this:

    Code:
    firstName[26]=firstname[26]
    You need to use strcpy().
    Last edited by SlyMaelstrom; 02-27-2006 at 08:25 PM.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    1) All objects of a class are created by a constructor.

    2) When you define a constructor for your class, the compiler will not provide a default constructor.


    Here:
    Code:
    Students *stdnt = new Students[100];
    You are creating an array of 100 students and getting its location in memory. Each Student in the array must be created by a constructor. Since you cannot provide arguments to a constructor when you declare an array, the default constructor is called to create each Student in the array. Since you don't have a default constructor, you get an error.

    When you get an error like this:
    error C2512: 'Students' : no appropriate default constructor available
    it's pretty obvious you need a default constructor--even if you don't know why. So, add a default constructor and have it display a message, e.g.
    Code:
    Student()
    { 
         cout<<"default constructor called"<<endl;
    }
    Then run your program again and look at the output to see when your default constructor is being called.
    Last edited by 7stud; 02-27-2006 at 08:54 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ have a constructor call another constructor
    By QuestionC in forum C++ Programming
    Replies: 4
    Last Post: 05-17-2007, 01:59 AM
  2. Replies: 3
    Last Post: 03-25-2006, 11:59 PM
  3. How do I override constructor in inheritance?
    By Loduwijk in forum C++ Programming
    Replies: 13
    Last Post: 03-24-2006, 08:36 AM
  4. Constructor problem
    By Orborde in forum C++ Programming
    Replies: 2
    Last Post: 06-01-2005, 10:41 PM
  5. Need help in classes
    By LBY in forum C++ Programming
    Replies: 11
    Last Post: 11-26-2004, 03:50 AM

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